Trait AsyncWrite

Source
pub trait AsyncWrite {
    // Required methods
    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_close(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Error>>;

    // Provided method
    fn poll_write_vectored(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bufs: &[IoSlice<'_>],
    ) -> Poll<Result<usize, Error>> { ... }
}
Available on crate feature io only.
Expand description

Write bytes asynchronously.

This trait is analogous to the std::io::Write trait, but integrates with the asynchronous task system. In particular, the poll_write method, unlike Write::write, will automatically queue the current task for wakeup and return if the writer cannot take more data, rather than blocking the calling thread.

Required Methodsยง

Source

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

Attempt to write bytes from buf into the object.

On success, returns Poll::Ready(Ok(num_bytes_written)).

If the object is not ready for writing, the method returns Poll::Pending and arranges for the current task (via cx.waker().wake_by_ref()) to receive a notification when the object becomes writable or is closed.

ยงImplementation

This function may not return errors of kind WouldBlock or Interrupted. Implementations must convert WouldBlock into Poll::Pending and either internally retry or convert Interrupted into another error kind.

poll_write must try to make progress by flushing the underlying object if that is the only way the underlying object can become writable again.

Source

fn poll_flush( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Attempt to flush the object, ensuring that any buffered data reach their destination.

On success, returns Poll::Ready(Ok(())).

If flushing cannot immediately complete, this method returns Poll::Pending and arranges for the current task (via cx.waker().wake_by_ref()) to receive a notification when the object can make progress towards flushing.

ยงImplementation

This function may not return errors of kind WouldBlock or Interrupted. Implementations must convert WouldBlock into Poll::Pending and either internally retry or convert Interrupted into another error kind.

It only makes sense to do anything here if you actually buffer data.

Source

fn poll_close( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Attempt to close the object.

On success, returns Poll::Ready(Ok(())).

If closing cannot immediately complete, this function returns Poll::Pending and arranges for the current task (via cx.waker().wake_by_ref()) to receive a notification when the object can make progress towards closing.

ยงImplementation

This function may not return errors of kind WouldBlock or Interrupted. Implementations must convert WouldBlock into Poll::Pending and either internally retry or convert Interrupted into another error kind.

Provided Methodsยง

Source

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Attempt to write bytes from bufs into the object using vectored IO operations.

This method is similar to poll_write, but allows data from multiple buffers to be written using a single operation.

On success, returns Poll::Ready(Ok(num_bytes_written)).

If the object is not ready for writing, the method returns Poll::Pending and arranges for the current task (via cx.waker().wake_by_ref()) to receive a notification when the object becomes writable or is closed.

By default, this method delegates to using poll_write on the first nonempty buffer in bufs, or an empty one if none exists. Objects which support vectored IO should override this method.

ยงImplementation

This function may not return errors of kind WouldBlock or Interrupted. Implementations must convert WouldBlock into Poll::Pending and either internally retry or convert Interrupted into another error kind.

Implementations on Foreign Typesยง

Sourceยง

impl AsyncWrite for Vec<u8>

Sourceยง

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

Sourceยง

fn poll_write_vectored( self: Pin<&mut Vec<u8>>, _: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Sourceยง

fn poll_flush( self: Pin<&mut Vec<u8>>, _: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Sourceยง

fn poll_close( self: Pin<&mut Vec<u8>>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Sourceยง

impl<P> AsyncWrite for Pin<P>
where P: DerefMut + Unpin, <P as Deref>::Target: AsyncWrite,

Sourceยง

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

Sourceยง

fn poll_write_vectored( self: Pin<&mut Pin<P>>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Sourceยง

fn poll_flush( self: Pin<&mut Pin<P>>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Sourceยง

fn poll_close( self: Pin<&mut Pin<P>>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Sourceยง

impl<T> AsyncWrite for &mut T
where T: AsyncWrite + Unpin + ?Sized,

Sourceยง

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

Sourceยง

fn poll_write_vectored( self: Pin<&mut &mut T>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Sourceยง

fn poll_flush( self: Pin<&mut &mut T>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Sourceยง

fn poll_close( self: Pin<&mut &mut T>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Sourceยง

impl<T> AsyncWrite for Box<T>
where T: AsyncWrite + Unpin + ?Sized,

Sourceยง

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

Sourceยง

fn poll_write_vectored( self: Pin<&mut Box<T>>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Sourceยง

fn poll_flush( self: Pin<&mut Box<T>>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Sourceยง

fn poll_close( self: Pin<&mut Box<T>>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Implementorsยง