跳到主要内容

AsyncBufRead

搜索

trait AsyncBufRead 

源代码
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,但与异步任务系统集成。特别地,与 BufRead::fill_buf 不同,poll_fill_buf 方法将在数据尚未可用时自动将当前任务排队等待唤醒并返回,而不是阻塞调用线程。

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

必需方法§

源代码

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

尝试返回内部缓冲区的内容,如果为空,则使用内部 reader 中的更多数据填充它。

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

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

此函数是较低级别的调用。它需要与 consume 方法配对才能正常工作。调用此方法时,不会"读取"任何内容,因为稍后调用 poll_read 可能会返回相同的内容。因此,必须使用从此缓冲区消费的字节数调用 consume,以确保字节永远不会被返回两次。

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

源代码

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 返回的缓冲区中的字节数。

外部类型的实现§

源代码§

impl AsyncBufRead for &[u8]

源代码§

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

源代码§

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

源代码§

impl<P> AsyncBufRead for Pin<P>

源代码§

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

源代码§

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

源代码§

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

源代码§

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

源代码§

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

源代码§

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

源代码§

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

源代码§

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

源代码§

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

源代码§

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

源代码§

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

实现者§