Struct SinkWriter

struct SinkWriter<S> { ... }

Convert a Sink of byte chunks into an AsyncWrite.

Whenever you write to this SinkWriter, the supplied bytes are forwarded to the inner Sink. When shutdown is called on this SinkWriter, the inner sink is closed.

This adapter takes a Sink<&[u8]> and provides an AsyncWrite impl for it. Because of the lifetime, this trait is relatively rarely implemented. The main ways to get a Sink<&[u8]> that you can use with this type are:

The opposite conversion of implementing Sink<_> for an AsyncWrite is done using the codec module.

Example

use bytes::Bytes;
use futures_util::SinkExt;
use std::io::{Error, ErrorKind};
use tokio::io::AsyncWriteExt;
use tokio_util::io::{SinkWriter, CopyToBytes};
use tokio_util::sync::PollSender;

# #[tokio::main(flavor = "current_thread")]
# async fn main() -> Result<(), Error> {
// We use an mpsc channel as an example of a `Sink<Bytes>`.
let (tx, mut rx) = tokio::sync::mpsc::channel::<Bytes>(1);
let sink = PollSender::new(tx).sink_map_err(|_| Error::from(ErrorKind::BrokenPipe));

// Wrap it in `CopyToBytes` to get a `Sink<&[u8]>`.
let mut writer = SinkWriter::new(CopyToBytes::new(sink));

// Write data to our interface...
let data: [u8; 4] = [1, 2, 3, 4];
let _ = writer.write(&data).await?;

// ... and receive it.
assert_eq!(data.as_slice(), &*rx.recv().await.unwrap());
# Ok(())
# }

Implementations

impl<S> SinkWriter<S>

fn new(sink: S) -> Self

Creates a new SinkWriter.

fn get_ref(self: &Self) -> &S

Gets a reference to the underlying sink.

fn get_mut(self: &mut Self) -> &mut S

Gets a mutable reference to the underlying sink.

fn into_inner(self: Self) -> S

Consumes this SinkWriter, returning the underlying sink.

impl<'__pin, S> Unpin for SinkWriter<S>

impl<R> AsyncReadExt for SinkWriter<S>

impl<S> Freeze for SinkWriter<S>

impl<S> RefUnwindSafe for SinkWriter<S>

impl<S> Send for SinkWriter<S>

impl<S> Sync for SinkWriter<S>

impl<S> UnsafeUnpin for SinkWriter<S>

impl<S> UnwindSafe for SinkWriter<S>

impl<S, E> AsyncWrite for SinkWriter<S>

fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, Error>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>>
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>>

impl<S, T, E> TryStream for SinkWriter<S>

fn try_poll_next(self: Pin<&mut S>, cx: &mut Context<'_>) -> Poll<Option<Result<<S as TryStream>::Ok, <S as TryStream>::Error>>>

impl<S: $crate::fmt::Debug> Debug for SinkWriter<S>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<S: AsyncRead> AsyncRead for SinkWriter<S>

fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<Result<()>>

impl<S: Stream> Stream for SinkWriter<S>

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<<Self as >::Item>>

impl<T> Any for SinkWriter<S>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for SinkWriter<S>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for SinkWriter<S>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for SinkWriter<S>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for SinkWriter<S>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for SinkWriter<S>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for SinkWriter<S>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>

impl<W> AsyncWriteExt for SinkWriter<S>