跳到主要内容

AsyncBufRead

搜索

特性 AsyncBufRead 

Source
pub trait AsyncBufRead: AsyncRead {
    // Required methods
    fn poll_fill_buf(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<&[u8]>>;
    fn consume(self: Pin<&mut Self>, amt: usize);
}
展开描述

异步读取字节。

此 trait 类似于 std::io::BufRead,但与异步任务系统集成。 特别地,poll_fill_buf 方法与 BufRead::fill_buf 不同, 当数据尚不可用时,它会自动将当前任务排入唤醒队列并立即返回, 而不是阻塞调用线程。

用于处理 AsyncBufRead 值的工具方法由 AsyncBufReadExt 提供。

必需方法§

Source

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

尝试返回内部缓冲区的内容,如果内部缓冲区为空,则从内部读取器填充更多数据。

成功时,返回 Poll::Ready(Ok(buf))

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

此函数是较低级别的调用。要使其正常工作,需要与 consume 方法配对使用。 调用此方法时,其中的内容不会被视为已“读”, 因此随后调用 poll_read 仍可能返回相同的内容。 因此,必须使用从此缓冲区消费的字节数调用 consume, 以确保相同的字节不会被返回两次。

返回空缓冲区表示流已达到 EOF。

Source

fn consume(self: Pin<&mut Self>, amt: usize)

通知此缓冲区已有 amt 个字节被消费, 因此后续调用 poll_read 时不应再返回这些字节。

此函数是较低级别的调用。要使其正常工作,需要与 poll_fill_buf 方法配对使用。 此函数不执行任何 I/O 操作,它只是通知此对象: 从 poll_fill_buf 返回的缓冲区中已有部分被消费, 不应再返回这些内容。因此,如果在调用此函数之前没有调用 poll_fill_buf,则可能产生异常行为。

amt 必须 <=poll_fill_buf 返回的缓冲区中的字节数。

外部类型的实现§

Source§

impl AsyncBufRead for &[u8]

Source§

fn poll_fill_buf( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll<Result<&[u8]>>

Source§

fn consume(self: Pin<&mut Self>, amt: usize)

Source§

impl<P> AsyncBufRead for Pin<P>

Source§

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

Source§

fn consume(self: Pin<&mut Self>, amt: usize)

Source§

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

Source§

fn poll_fill_buf( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll<Result<&[u8]>>

Source§

fn consume(self: Pin<&mut Self>, amt: usize)

Source§

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

Source§

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

Source§

fn consume(self: Pin<&mut Self>, amt: usize)

Source§

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

Source§

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

Source§

fn consume(self: Pin<&mut Self>, amt: usize)

实现者§