Struct SinkWriter

pub struct SinkWriter<S> { /* private fields */ }

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) -> &S

Gets a reference to the underlying sink.

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

Gets a mutable reference to the underlying sink.

fn into_inner(self) -> S

Consumes this SinkWriter, returning the underlying sink.

Trait Implementations

impl<'__pin, S> Unpin for SinkWriter<S> where PinnedFieldsOf<__Origin<'__pin, S>>: Unpin,

impl<S, E> AsyncWrite for SinkWriter<S> where for<'a> S: Sink<&'a [u8], Error = E>, E: Into<Error>,

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: AsyncRead> AsyncRead for SinkWriter<S>

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

impl<S: Debug> Debug for SinkWriter<S>

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

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

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

Auto Trait Implementations

impl<S> Freeze for SinkWriter<S> where S: Freeze,

impl<S> RefUnwindSafe for SinkWriter<S> where S: RefUnwindSafe,

impl<S> Send for SinkWriter<S> where S: Send,

impl<S> Sync for SinkWriter<S> where S: Sync,

impl<S> UnsafeUnpin for SinkWriter<S> where S: UnsafeUnpin,

impl<S> UnwindSafe for SinkWriter<S> where S: UnwindSafe,

Blanket Implementations

impl<R> AsyncReadExt for SinkWriter<S> where R: AsyncRead + ?Sized,

impl<S, T, E> TryStream for SinkWriter<S> where S: Stream<Item = Result<T, E>> + ?Sized,

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

impl<T> Any for SinkWriter<S> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for SinkWriter<S> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for SinkWriter<S> where T: ?Sized,

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

impl<T> From<T> for SinkWriter<S>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for SinkWriter<S> where U: From<T>,

fn into(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<U> for SinkWriter<S> where U: Into<T>,

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

impl<T, U> TryInto<U> for SinkWriter<S> where U: TryFrom<T>,

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

impl<W> AsyncWriteExt for SinkWriter<S> where W: AsyncWrite + ?Sized,