Trait AsyncBufRead
pub trait AsyncBufRead: AsyncRead
Read bytes asynchronously.
This trait is analogous to the std::io::BufRead trait, but integrates
with the asynchronous task system. In particular, the poll_fill_buf
method, unlike BufRead::fill_buf, will automatically queue the current task
for wakeup and return if data is not yet available, rather than blocking
the calling thread.
Required Methods
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>>Attempt to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty.
On success, returns
Poll::Ready(Ok(buf)).If no data is available for reading, the method returns
Poll::Pendingand arranges for the current task (viacx.waker().wake_by_ref()) to receive a notification when the object becomes readable or is closed.This function is a lower-level call. It needs to be paired with the
consumemethod to function properly. When calling this method, none of the contents will be "read" in the sense that later callingpoll_readmay return the same contents. As such,consumemust be called with the number of bytes that are consumed from this buffer to ensure that the bytes are never returned twice.An empty buffer returned indicates that the stream has reached EOF.
Implementation
This function may not return errors of kind
WouldBlockorInterrupted. Implementations must convertWouldBlockintoPoll::Pendingand either internally retry or convertInterruptedinto another error kind.fn consume(self: Pin<&mut Self>, amt: usize)Tells this buffer that
amtbytes have been consumed from the buffer, so they should no longer be returned in calls topoll_read.This function is a lower-level call. It needs to be paired with the
poll_fill_bufmethod to function properly. This function does not perform any I/O, it simply informs this object that some amount of its buffer, returned frompoll_fill_buf, has been consumed and should no longer be returned. As such, this function may do odd things ifpoll_fill_bufisn't called before calling it.The
amtmust be<=the number of bytes in the buffer returned bypoll_fill_buf.
Implementors
impl AsyncBufRead for &[u8]impl<P> AsyncBufRead for Pin<P> where P: DerefMut + Unpin, P::Target: AsyncBufRead,impl<T: ?Sized + AsyncBufRead + Unpin> AsyncBufRead for &mut Timpl<T: ?Sized + AsyncBufRead + Unpin> AsyncBufRead for Box<T>