跳到主要内容

ReadHalf

搜索

结构体 ReadHalf 

Source
pub struct ReadHalf<'a>(/* private fields */);
展开描述

TcpStream 的 borrowed 读半部,由 split 创建。

ReadHalf 读取通常通过 AsyncReadExt trait 上的便捷方法来完成。

实现§

Source§

impl ReadHalf<'_>

Source

pub fn poll_peek( &mut self, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<Result<usize>>

尝试在套接字上接收数据,但不会从队列中移除该数据。如果数据尚不可用,则注册当前任务以便稍后唤醒。

请注意,对于 poll_peekpoll_read 的多次调用,仅会调度传递给最近一次调用的 Context 中的 Waker 接收唤醒。

更多详细信息,请参阅 TcpStream::poll_peek 级别的文档。

§示例
use tokio::io::{self, ReadBuf};
use tokio::net::TcpStream;

use std::future::poll_fn;

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut stream = TcpStream::connect("127.0.0.1:8000").await?;
    let (mut read_half, _) = stream.split();
    let mut buf = [0; 10];
    let mut buf = ReadBuf::new(&mut buf);

    poll_fn(|cx| {
        read_half.poll_peek(cx, &mut buf)
    }).await?;

    Ok(())
}
Source

pub async fn peek(&mut self, buf: &mut [u8]) -> Result<usize>

从套接字所连接的远端地址接收数据,但不会从队列中移除该数据。成功时返回已窥视的字节数。

更多详细信息,请参阅 TcpStream::peek 级别的文档。

§示例
use tokio::net::TcpStream;
use tokio::io::AsyncReadExt;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Connect to a peer
    let mut stream = TcpStream::connect("127.0.0.1:8080").await?;
    let (mut read_half, _) = stream.split();

    let mut b1 = [0; 10];
    let mut b2 = [0; 10];

    // Peek at the data
    let n = read_half.peek(&mut b1).await?;

    // Read the data
    assert_eq!(n, read_half.read(&mut b2[..n]).await?);
    assert_eq!(&b1[..n], &b2[..n]);

    Ok(())
}

read 方法定义在 AsyncReadExt trait 上。

Source

pub async fn ready(&self, interest: Interest) -> Result<Ready>

等待任意一个所请求的就绪状态。

该函数通常与 try_read() 配合使用。它也可代替 readable() 来检查返回的就绪集合中是否包含 Ready::READABLEReady::READ_CLOSED 事件。

函数可能在套接字尚未就绪时完成。这是误报情况,尝试进行操作时将返回 io::ErrorKind::WouldBlock。函数也可能返回空的 Ready 集合,因此应始终检查返回值,若请求的状态尚未设置则可能需要再次等待。

该函数等同于 TcpStream::ready

§Cancel safety

此方法可安全取消。一旦就绪事件发生,该方法将持续立即返回,直到就绪事件被尝试进行读取或写入(且失败返回 WouldBlockPoll::Pending)的操作消耗。

Source

pub async fn readable(&self) -> Result<()>

等待套接字变为可读。

该函数等同于 ready(Interest::READABLE),通常与 try_read() 配合使用。

该函数也等同于 TcpStream::ready

§Cancel safety

此方法可安全取消。一旦就绪事件发生,该方法将持续立即返回,直到就绪事件被尝试进行读取(且失败返回 WouldBlockPoll::Pending)的操作消耗。

Source

pub fn try_read(&self, buf: &mut [u8]) -> Result<usize>

尝试从流读取数据到所提供的缓冲区中,返回读取的字节数。

从套接字接收任何已有数据,但不会等待新数据的到达。成功时返回已读取的字节数。由于 try_read() 是非阻塞的,缓冲区不必由异步任务持有,可以完全存在于栈上。

通常,readable()ready() 与该函数配合使用。

§Return

如果成功读取数据,则返回 Ok(n),其中 n 是已读取的字节数。如果 n0,则可能表示以下两种情况之一:

  1. The stream’s read half is closed and will no longer yield data.
  2. The specified buffer was 0 bytes in length.

如果流尚未准备好读取数据,则返回 Err(io::ErrorKind::WouldBlock)

Source

pub fn try_read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>

尝试从流读取数据到所提供的缓冲区中,返回读取的字节数。

数据依次拷贝填充到每个缓冲区中,最后一个缓冲区可能只被部分填充。此方法等效于对拼接后的缓冲区进行一次 try_read() 调用。

从套接字接收任何已有数据,但不会等待新数据的到达。成功时返回已读取的字节数。由于 try_read_vectored() 是非阻塞的,缓冲区不必由异步任务持有,可以完全存在于栈上。

通常,readable()ready() 与该函数配合使用。

§Return

如果成功读取数据,则返回 Ok(n),其中 n 是已读取的字节数。Ok(0) 表示流的读半部已关闭,并且不再产生数据。如果流尚未准备好读取数据,则返回 Err(io::ErrorKind::WouldBlock)

Source

pub fn try_read_buf<B: BufMut>(&self, buf: &mut B) -> Result<usize>

尝试从流读取数据到所提供的缓冲区中,并推进缓冲区的内部游标,返回读取的字节数。

从套接字接收任何已有数据,但不会等待新数据的到达。成功时返回已读取的字节数。由于 try_read_buf() 是非阻塞的,缓冲区不必由异步任务持有,可以完全存在于栈上。

通常,readable()ready() 与该函数配合使用。

§Return

如果成功读取数据,则返回 Ok(n),其中 n 是已读取的字节数。Ok(0) 表示流的读半部已关闭,并且不再产生数据。如果流尚未准备好读取数据,则返回 Err(io::ErrorKind::WouldBlock)

Source

pub fn peer_addr(&self) -> Result<SocketAddr>

返回该流连接到的远端地址。

Source

pub fn local_addr(&self) -> Result<SocketAddr>

返回该流绑定的本地地址。

Trait 实现§

Source§

impl AsRef<TcpStream> for ReadHalf<'_>

Source§

fn as_ref(&self) -> &TcpStream

将此类型转换为输入类型的共享引用(通常自动推导)。
Source§

impl AsyncRead for ReadHalf<'_>

Source§

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

Attempts to read from the AsyncRead into buf. 更多信息
Source§

impl<'a> Debug for ReadHalf<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化器格式化此值。 更多信息

自动 Trait 实现§

§

impl<'a> Freeze for ReadHalf<'a>

§

impl<'a> RefUnwindSafe for ReadHalf<'a>

§

impl<'a> Send for ReadHalf<'a>

§

impl<'a> Sync for ReadHalf<'a>

§

impl<'a> Unpin for ReadHalf<'a>

§

impl<'a> UnsafeUnpin for ReadHalf<'a>

§

impl<'a> UnwindSafe for ReadHalf<'a>

Blanket 实现§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

impl<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where Self: Sized, R: AsyncRead,

Creates a new AsyncRead instance that chains this stream with next. 更多信息
Source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>
where Self: Unpin,

Pulls some bytes from this source into the specified buffer, returning how many bytes were read. 更多信息
Source§

fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
where Self: Unpin, B: BufMut + ?Sized,

Pulls some bytes from this source into the specified buffer, advancing the buffer’s internal cursor. 更多信息
Source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>
where Self: Unpin,

Reads the exact number of bytes required to fill buf. 更多信息
Source§

fn read_u8(&mut self) -> ReadU8<&mut Self>
where Self: Unpin,

Reads an unsigned 8 bit integer from the underlying reader. 更多信息
Source§

fn read_i8(&mut self) -> ReadI8<&mut Self>
where Self: Unpin,

Reads a signed 8 bit integer from the underlying reader. 更多信息
Source§

fn read_u16(&mut self) -> ReadU16<&mut Self>
where Self: Unpin,

Reads an unsigned 16-bit integer in big-endian order from the underlying reader. 更多信息
Source§

fn read_i16(&mut self) -> ReadI16<&mut Self>
where Self: Unpin,

Reads a signed 16-bit integer in big-endian order from the underlying reader. 更多信息
Source§

fn read_u32(&mut self) -> ReadU32<&mut Self>
where Self: Unpin,

Reads an unsigned 32-bit integer in big-endian order from the underlying reader. 更多信息
Source§

fn read_i32(&mut self) -> ReadI32<&mut Self>
where Self: Unpin,

Reads a signed 32-bit integer in big-endian order from the underlying reader. 更多信息
Source§

fn read_u64(&mut self) -> ReadU64<&mut Self>
where Self: Unpin,

Reads an unsigned 64-bit integer in big-endian order from the underlying reader. 更多信息
Source§

fn read_i64(&mut self) -> ReadI64<&mut Self>
where Self: Unpin,

Reads an signed 64-bit integer in big-endian order from the underlying reader. 更多信息
Source§

fn read_u128(&mut self) -> ReadU128<&mut Self>
where Self: Unpin,

Reads an unsigned 128-bit integer in big-endian order from the underlying reader. 更多信息
Source§

fn read_i128(&mut self) -> ReadI128<&mut Self>
where Self: Unpin,

Reads an signed 128-bit integer in big-endian order from the underlying reader. 更多信息
Source§

fn read_f32(&mut self) -> ReadF32<&mut Self>
where Self: Unpin,

Reads an 32-bit floating point type in big-endian order from the underlying reader. 更多信息
Source§

fn read_f64(&mut self) -> ReadF64<&mut Self>
where Self: Unpin,

Reads an 64-bit floating point type in big-endian order from the underlying reader. 更多信息
Source§

fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>
where Self: Unpin,

Reads an unsigned 16-bit integer in little-endian order from the underlying reader. 更多信息
Source§

fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>
where Self: Unpin,

Reads a signed 16-bit integer in little-endian order from the underlying reader. 更多信息
Source§

fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>
where Self: Unpin,

Reads an unsigned 32-bit integer in little-endian order from the underlying reader. 更多信息
Source§

fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>
where Self: Unpin,

Reads a signed 32-bit integer in little-endian order from the underlying reader. 更多信息
Source§

fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>
where Self: Unpin,

Reads an unsigned 64-bit integer in little-endian order from the underlying reader. 更多信息
Source§

fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>
where Self: Unpin,

Reads an signed 64-bit integer in little-endian order from the underlying reader. 更多信息
Source§

fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>
where Self: Unpin,

Reads an unsigned 128-bit integer in little-endian order from the underlying reader. 更多信息
Source§

fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>
where Self: Unpin,

Reads an signed 128-bit integer in little-endian order from the underlying reader. 更多信息
Source§

fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>
where Self: Unpin,

Reads an 32-bit floating point type in little-endian order from the underlying reader. 更多信息
Source§

fn read_f64_le(&mut self) -> ReadF64Le<&mut Self>
where Self: Unpin,

Reads an 64-bit floating point type in little-endian order from the underlying reader. 更多信息
Source§

fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>
where Self: Unpin,

Reads all bytes until EOF in this source, placing them into buf. 更多信息
Source§

fn read_to_string<'a>( &'a mut self, dst: &'a mut String, ) -> ReadToString<'a, Self>
where Self: Unpin,

Reads all bytes until EOF in this source, appending them to buf. 更多信息
Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adaptor which reads at most limit bytes from it. 更多信息
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换的具体行为取决于 From<T> for U 的实现方式。

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。