跳到主要内容

AsyncRead

搜索

特性 AsyncRead 

Source
pub trait AsyncRead {
    // Required method
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<Result<()>>;
}
展开描述

从源读取字节。

此 trait 类似于 std::io::Read trait,但与 异步任务系统集成在一起。 特别的,与 Read::read 不同, poll_read 方法 会在数据还不可用时自动将当前任务加入唤醒队列并返回, 而不是阻塞调用线程。

具体来说,这意味着 poll_read 函数将返回以下之一:

  • Poll::Ready(Ok(())) 表示数据已立即读取并放入输出缓冲区。 读取的数据量可以通过 ReadBuf::filled 返回的切片长度的增量来确定。 如果增量为 0,则要么已到达 EOF,要么输出缓冲区的容量为零(即 buf.remaining() == 0)。

  • Poll::Pending 表示没有数据被读入提供的 缓冲区。 该 I/O 对象当前不可读,但将来可能变为可读。 最重要的是,当该对象可读时,当前 future 的任务会被安排 unpark。 这意味着,与 Future::poll 一样, 当 I/O 对象再次变为可读时, 你将收到通知。

  • 其它错误的 Poll::Ready(Err(e)) 是来自底层对象的标准 I/O 错误。

此 trait 重要的含义是:read 方法仅在 future 任务的上下文中工作。 如果在任务外使用该对象,则可能会 panic。

用于处理 AsyncRead 值的工具方法由 AsyncReadExt 提供。

必需方法§

Source

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

尝试从 AsyncRead 读取到 buf

成功时,返回 Poll::Ready(Ok(())) 并将数据放入 buf 的未填充部分。 如果没有读取到数据(buf.filled().len() 不变), 则意味着已到达 EOF,或者输出缓冲区的容量为零(即 buf.remaining() == 0)。

如果没有可读取的数据,此方法返回 Poll::Pending, 并安排当前任务(通过 cx.waker())在该对象变为可读或被关闭时收到通知。

外部类型的实现§

Source§

impl AsyncRead for &[u8]

Source§

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

Source§

impl<P> AsyncRead for Pin<P>
where P: DerefMut, P::Target: AsyncRead,

Source§

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

Source§

impl<T: AsRef<[u8]> + Unpin> AsyncRead for Cursor<T>

Source§

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

Source§

impl<T: ?Sized + AsyncRead + Unpin> AsyncRead for &mut T

Source§

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

Source§

impl<T: ?Sized + AsyncRead + Unpin> AsyncRead for Box<T>

Source§

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

实现者§