pub trait AsyncReadExt: AsyncRead {
Show 29 methods
// Provided methods
fn chain<R>(self, next: R) -> Chain<Self, R>
where Self: Sized,
R: AsyncRead { ... }
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>
where Self: Unpin { ... }
fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
where Self: Unpin,
B: BufMut + ?Sized { ... }
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>
where Self: Unpin { ... }
fn read_u8(&mut self) -> ReadU8<&mut Self>
where Self: Unpin { ... }
fn read_i8(&mut self) -> ReadI8<&mut Self>
where Self: Unpin { ... }
fn read_u16(&mut self) -> ReadU16<&mut Self>
where Self: Unpin { ... }
fn read_i16(&mut self) -> ReadI16<&mut Self>
where Self: Unpin { ... }
fn read_u32(&mut self) -> ReadU32<&mut Self>
where Self: Unpin { ... }
fn read_i32(&mut self) -> ReadI32<&mut Self>
where Self: Unpin { ... }
fn read_u64(&mut self) -> ReadU64<&mut Self>
where Self: Unpin { ... }
fn read_i64(&mut self) -> ReadI64<&mut Self>
where Self: Unpin { ... }
fn read_u128(&mut self) -> ReadU128<&mut Self>
where Self: Unpin { ... }
fn read_i128(&mut self) -> ReadI128<&mut Self>
where Self: Unpin { ... }
fn read_f32(&mut self) -> ReadF32<&mut Self>
where Self: Unpin { ... }
fn read_f64(&mut self) -> ReadF64<&mut Self>
where Self: Unpin { ... }
fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>
where Self: Unpin { ... }
fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>
where Self: Unpin { ... }
fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>
where Self: Unpin { ... }
fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>
where Self: Unpin { ... }
fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>
where Self: Unpin { ... }
fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>
where Self: Unpin { ... }
fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>
where Self: Unpin { ... }
fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>
where Self: Unpin { ... }
fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>
where Self: Unpin { ... }
fn read_f64_le(&mut self) -> ReadF64Le<&mut Self>
where Self: Unpin { ... }
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEnd<'a, Self>
where Self: Unpin { ... }
fn read_to_string<'a>(
&'a mut self,
dst: &'a mut String,
) -> ReadToString<'a, Self>
where Self: Unpin { ... }
fn take(self, limit: u64) -> Take<Self>
where Self: Sized { ... }
}展开描述
从一个数据源异步地读取字节。
实现为一个扩展 trait,为所有
AsyncRead 类型添加实用方法。
调用方倾向于导入此 trait 而不是
AsyncRead。
use tokio::fs::File;
use tokio::io::{self, AsyncReadExt};
#[tokio::main]
async fn main() -> io::Result<()> {
let mut f = File::open("foo.txt").await?;
let mut buffer = [0; 10];
// The `read` method is defined by this trait.
let n = f.read(&mut buffer[..]).await?;
Ok(())
}更多详情请参阅 module 文档。
提供方法§
Sourcefn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
创建一个新的 AsyncRead 实例,将此流与 next 链接起来。
返回的 AsyncRead 实例将首先从此对象读取所有字节,直到遇到 EOF。
之后其输出与 next 的输出等价。
§示例
File 实现了 AsyncRead:
use tokio::fs::File;
use tokio::io::{self, AsyncReadExt};
#[tokio::main]
async fn main() -> io::Result<()> {
let f1 = File::open("foo.txt").await?;
let f2 = File::open("bar.txt").await?;
let mut handle = f1.chain(f2);
let mut buffer = String::new();
// read the value into a String. We could use any AsyncRead
// method here, this is just one example.
handle.read_to_string(&mut buffer).await?;
Ok(())
}Sourcefn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
从此源读取一些字节到指定的缓冲区,返回读取的字节数。
等价于:
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>;此方法不保证是立即完成还是异步完成。
§Return
如果此方法的返回值是 Ok(n),那么必须保证 0 <= n <= buf.len()。
非零的 n 值表示缓冲区 buf 已从该源填充了 n 个字节的数据。
如果 n 是 0,则可能表示以下两种情况之一:
- This reader has reached its “end of file” and will likely no longer be able to produce bytes. Note that this does not mean that the reader will always no longer be able to produce bytes.
- The buffer specified was 0 bytes in length.
此函数被调用时,buf 的内容没有任何保证,
实现不能依赖 buf 内容的任何属性。
建议实现只将数据写入 buf,而不要读取其内容。
相应地,调用方也不能假设实现如何使用 buf 的任何保证。
本应写入缓冲区的代码也可能读取它。
在调用 read 之前,你有责任确保 buf 已初始化。
§Errors
如果此函数遇到任何形式的 I/O 或其它错误,将返回一个错误变体。 如果返回错误,则必须保证没有读取任何字节。
§Cancel safety
此方法是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可以保证没有数据被读取。
§示例
File 实现了 Read:
use tokio::fs::File;
use tokio::io::{self, AsyncReadExt};
#[tokio::main]
async fn main() -> io::Result<()> {
let mut f = File::open("foo.txt").await?;
let mut buffer = [0; 10];
// read up to 10 bytes
let n = f.read(&mut buffer[..]).await?;
println!("The bytes: {:?}", &buffer[..n]);
Ok(())
}Sourcefn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
从此源读取一些字节到指定的缓冲区,推进缓冲区的内部光标。
等价于:
async fn read_buf<B: BufMut>(&mut self, buf: &mut B) -> io::Result<usize>;通常,即使提供的缓冲区有更多空间,也只会发出单个 read 系统调用。
此方法不保证是立即完成还是异步完成。
§Return
非零的 n 值表示缓冲区 buf 已从该源填充了 n 个字节的数据。
如果 n 是 0,则可能表示以下两种情况之一:
- This reader has reached its “end of file” and will likely no longer be able to produce bytes. Note that this does not mean that the reader will always no longer be able to produce bytes.
- The buffer specified had a remaining capacity of zero.
§Errors
如果此函数遇到任何形式的 I/O 或其它错误,将返回一个错误变体。 如果返回错误,则必须保证没有读取任何字节。
§Cancel safety
此方法是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可以保证没有数据被读取。
§示例
File 实现了 Read,BytesMut 实现了 BufMut:
use tokio::fs::File;
use tokio::io::{self, AsyncReadExt};
use bytes::BytesMut;
#[tokio::main]
async fn main() -> io::Result<()> {
let mut f = File::open("foo.txt").await?;
let mut buffer = BytesMut::with_capacity(10);
assert!(buffer.is_empty());
assert!(buffer.capacity() >= 10);
// note that the return value is not needed to access the data
// that was read as `buffer`'s internal cursor is updated.
//
// this might read more than 10 bytes if the capacity of `buffer`
// is larger than 10.
f.read_buf(&mut buffer).await?;
println!("The bytes: {:?}", &buffer[..]);
Ok(())
}Sourcefn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
读取精确数量的字节以填满 buf。
等价于:
async fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<usize>;此函数读取必要的字节数,以完全填满指定的缓冲区 buf。
§Errors
如果在完全填满缓冲区之前遇到“文件结尾”,
它将返回 ErrorKind::UnexpectedEof 类型的错误。
在这种情况下,buf 的内容是未指定的。
如果遇到任何其它读取错误,则操作立即返回。
在这种情况下,buf 的内容未指定。
如果此操作返回错误,则已读取的字节数未指定, 但它绝不会读取超过填满缓冲区所需的字节数。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能已经有一些数据被读入 buf。
§示例
File 实现了 Read:
use tokio::fs::File;
use tokio::io::{self, AsyncReadExt};
#[tokio::main]
async fn main() -> io::Result<()> {
let mut f = File::open("foo.txt").await?;
let len = 10;
let mut buffer = vec![0; len];
// read exactly 10 bytes
f.read_exact(&mut buffer).await?;
Ok(())
}Sourcefn read_u8(&mut self) -> ReadU8<&mut Self>where
Self: Unpin,
fn read_u8(&mut self) -> ReadU8<&mut Self>where
Self: Unpin,
从底层读取器读取一个无符号 8 位整数。
等价于:
async fn read_u8(&mut self) -> io::Result<u8>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则保证不会读取任何数据。
§示例
从 AsyncRead 读取无符号 8 位整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![2, 5]);
assert_eq!(2, reader.read_u8().await?);
assert_eq!(5, reader.read_u8().await?);
Ok(())Sourcefn read_i8(&mut self) -> ReadI8<&mut Self>where
Self: Unpin,
fn read_i8(&mut self) -> ReadI8<&mut Self>where
Self: Unpin,
从底层读取器读取一个有符号 8 位整数。
等价于:
async fn read_i8(&mut self) -> io::Result<i8>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则保证不会读取任何数据。
§示例
从 AsyncRead 读取无符号 8 位整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0x02, 0xfb]);
assert_eq!(2, reader.read_i8().await?);
assert_eq!(-5, reader.read_i8().await?);
Ok(())Sourcefn read_u16(&mut self) -> ReadU16<&mut Self>where
Self: Unpin,
fn read_u16(&mut self) -> ReadU16<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个无符号 16-位整数。
等价于:
async fn read_u16(&mut self) -> io::Result<u16>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取无符号 16 位大端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![2, 5, 3, 0]);
assert_eq!(517, reader.read_u16().await?);
assert_eq!(768, reader.read_u16().await?);
Ok(())Sourcefn read_i16(&mut self) -> ReadI16<&mut Self>where
Self: Unpin,
fn read_i16(&mut self) -> ReadI16<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个有符号 16-位整数。
等价于:
async fn read_i16(&mut self) -> io::Result<i16>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取有符号 16 位大端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0x00, 0xc1, 0xff, 0x7c]);
assert_eq!(193, reader.read_i16().await?);
assert_eq!(-132, reader.read_i16().await?);
Ok(())Sourcefn read_u32(&mut self) -> ReadU32<&mut Self>where
Self: Unpin,
fn read_u32(&mut self) -> ReadU32<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个无符号 32-位整数。
等价于:
async fn read_u32(&mut self) -> io::Result<u32>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取无符号 32 位大端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0x00, 0x00, 0x01, 0x0b]);
assert_eq!(267, reader.read_u32().await?);
Ok(())Sourcefn read_i32(&mut self) -> ReadI32<&mut Self>where
Self: Unpin,
fn read_i32(&mut self) -> ReadI32<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个有符号 32-位整数。
等价于:
async fn read_i32(&mut self) -> io::Result<i32>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取有符号 32 位大端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0xff, 0xff, 0x7a, 0x33]);
assert_eq!(-34253, reader.read_i32().await?);
Ok(())Sourcefn read_u64(&mut self) -> ReadU64<&mut Self>where
Self: Unpin,
fn read_u64(&mut self) -> ReadU64<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个无符号 64-位整数。
等价于:
async fn read_u64(&mut self) -> io::Result<u64>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取无符号 64 位大端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![
0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83
]);
assert_eq!(918733457491587, reader.read_u64().await?);
Ok(())Sourcefn read_i64(&mut self) -> ReadI64<&mut Self>where
Self: Unpin,
fn read_i64(&mut self) -> ReadI64<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个有符号 64-位整数。
等价于:
async fn read_i64(&mut self) -> io::Result<i64>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取有符号 64 位大端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0]);
assert_eq!(i64::MIN, reader.read_i64().await?);
Ok(())Sourcefn read_u128(&mut self) -> ReadU128<&mut Self>where
Self: Unpin,
fn read_u128(&mut self) -> ReadU128<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个无符号 128-位整数。
等价于:
async fn read_u128(&mut self) -> io::Result<u128>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取无符号 128 位大端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![
0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83,
0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83
]);
assert_eq!(16947640962301618749969007319746179, reader.read_u128().await?);
Ok(())Sourcefn read_i128(&mut self) -> ReadI128<&mut Self>where
Self: Unpin,
fn read_i128(&mut self) -> ReadI128<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个有符号 128-位整数。
等价于:
async fn read_i128(&mut self) -> io::Result<i128>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取有符号 128 位大端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![
0x80, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
]);
assert_eq!(i128::MIN, reader.read_i128().await?);
Ok(())Sourcefn read_f32(&mut self) -> ReadF32<&mut Self>where
Self: Unpin,
fn read_f32(&mut self) -> ReadF32<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个 32-位浮点类型。
等价于:
async fn read_f32(&mut self) -> io::Result<f32>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取 32-位浮点类型:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0xff, 0x7f, 0xff, 0xff]);
assert_eq!(f32::MIN, reader.read_f32().await?);
Ok(())Sourcefn read_f64(&mut self) -> ReadF64<&mut Self>where
Self: Unpin,
fn read_f64(&mut self) -> ReadF64<&mut Self>where
Self: Unpin,
从底层读取器以大端字节序读取一个 64-位浮点类型。
等价于:
async fn read_f64(&mut self) -> io::Result<f64>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取 64-位浮点类型:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![
0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
]);
assert_eq!(f64::MIN, reader.read_f64().await?);
Ok(())Sourcefn read_u16_le(&mut self) -> ReadU16Le<&mut Self>where
Self: Unpin,
fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个无符号 16-位整数。
等价于:
async fn read_u16_le(&mut self) -> io::Result<u16>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取无符号 16 位小端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![2, 5, 3, 0]);
assert_eq!(1282, reader.read_u16_le().await?);
assert_eq!(3, reader.read_u16_le().await?);
Ok(())Sourcefn read_i16_le(&mut self) -> ReadI16Le<&mut Self>where
Self: Unpin,
fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个有符号 16-位整数。
等价于:
async fn read_i16_le(&mut self) -> io::Result<i16>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取有符号 16 位小端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0x00, 0xc1, 0xff, 0x7c]);
assert_eq!(-16128, reader.read_i16_le().await?);
assert_eq!(31999, reader.read_i16_le().await?);
Ok(())Sourcefn read_u32_le(&mut self) -> ReadU32Le<&mut Self>where
Self: Unpin,
fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个无符号 32-位整数。
等价于:
async fn read_u32_le(&mut self) -> io::Result<u32>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取无符号 32 位小端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0x00, 0x00, 0x01, 0x0b]);
assert_eq!(184614912, reader.read_u32_le().await?);
Ok(())Sourcefn read_i32_le(&mut self) -> ReadI32Le<&mut Self>where
Self: Unpin,
fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个有符号 32-位整数。
等价于:
async fn read_i32_le(&mut self) -> io::Result<i32>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取有符号 32 位小端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0xff, 0xff, 0x7a, 0x33]);
assert_eq!(863698943, reader.read_i32_le().await?);
Ok(())Sourcefn read_u64_le(&mut self) -> ReadU64Le<&mut Self>where
Self: Unpin,
fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个无符号 64-位整数。
等价于:
async fn read_u64_le(&mut self) -> io::Result<u64>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取无符号 64 位小端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![
0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83
]);
assert_eq!(9477368352180732672, reader.read_u64_le().await?);
Ok(())Sourcefn read_i64_le(&mut self) -> ReadI64Le<&mut Self>where
Self: Unpin,
fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个有符号 64-位整数。
等价于:
async fn read_i64_le(&mut self) -> io::Result<i64>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取有符号 64 位小端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0]);
assert_eq!(128, reader.read_i64_le().await?);
Ok(())Sourcefn read_u128_le(&mut self) -> ReadU128Le<&mut Self>where
Self: Unpin,
fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个无符号 128-位整数。
等价于:
async fn read_u128_le(&mut self) -> io::Result<u128>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取无符号 128 位小端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![
0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83,
0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83
]);
assert_eq!(174826588484952389081207917399662330624, reader.read_u128_le().await?);
Ok(())Sourcefn read_i128_le(&mut self) -> ReadI128Le<&mut Self>where
Self: Unpin,
fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个有符号 128-位整数。
等价于:
async fn read_i128_le(&mut self) -> io::Result<i128>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取有符号 128 位小端整数:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![
0x80, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
]);
assert_eq!(128, reader.read_i128_le().await?);
Ok(())Sourcefn read_f32_le(&mut self) -> ReadF32Le<&mut Self>where
Self: Unpin,
fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个 32-位浮点类型。
等价于:
async fn read_f32_le(&mut self) -> io::Result<f32>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取 32-位浮点类型:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![0xff, 0xff, 0x7f, 0xff]);
assert_eq!(f32::MIN, reader.read_f32_le().await?);
Ok(())Sourcefn read_f64_le(&mut self) -> ReadF64Le<&mut Self>where
Self: Unpin,
fn read_f64_le(&mut self) -> ReadF64Le<&mut Self>where
Self: Unpin,
从底层读取器以小端字节序读取一个 64-位浮点类型。
等价于:
async fn read_f64_le(&mut self) -> io::Result<f64>;建议使用缓冲读取器以避免过多的系统调用。
§Errors
此方法返回的错误与 AsyncReadExt::read_exact 相同。
§Cancel safety
此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件,
而其他分支先完成,则可能会丢失部分数据。
§示例
从 AsyncRead 读取 64-位浮点类型:
use tokio::io::{self, AsyncReadExt};
use std::io::Cursor;
let mut reader = Cursor::new(vec![
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff
]);
assert_eq!(f64::MIN, reader.read_f64_le().await?);
Ok(())Sourcefn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
读取此源中直到 EOF 的所有字节,将它们放入 buf。
等价于:
async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize>;从此源读取的所有字节都将追加到指定的缓冲区 buf 中。
此函数会持续调用 read() 以向 buf 追加更多数据,
直到 read() 返回 Ok(0)。
如果成功,则返回读取的总字节数。
§Errors
如果遇到读取错误,则 read_to_end 操作立即完成。
已经读取的任何字节都将追加到 buf 中。
§示例
File 实现了 Read:
use tokio::io::{self, AsyncReadExt};
use tokio::fs::File;
#[tokio::main]
async fn main() -> io::Result<()> {
let mut f = File::open("foo.txt").await?;
let mut buffer = Vec::new();
// read the whole file
f.read_to_end(&mut buffer).await?;
Ok(())
}(另请参阅便捷函数 tokio::fs::read 用于从文件读取。)
Sourcefn read_to_string<'a>(
&'a mut self,
dst: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
dst: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
读取此源中直到 EOF 的所有字节,将它们追加到 buf。
等价于:
async fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize>;如果成功,则返回读取并追加到 buf 中的字节数。
§Errors
如果此流中的数据不是有效的 UTF-8,则返回错误且 buf 不变。
有关其它错误语义,请参阅 read_to_end。
§示例
File 实现了 Read:
use tokio::io::{self, AsyncReadExt};
use tokio::fs::File;
#[tokio::main]
async fn main() -> io::Result<()> {
let mut f = File::open("foo.txt").await?;
let mut buffer = String::new();
f.read_to_string(&mut buffer).await?;
Ok(())
}(另请参阅便捷函数 crate::fs::read_to_string 用于从文件读取。)
Sourcefn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
创建一个适配器,最多从中读取 limit 个字节。
此函数返回一个新的 AsyncRead 实例,
它最多读取 limit 个字节,
之后将始终返回 EOF(Ok(0))。
任何读取错误都不会计入已读取的字节数,
后续对 read() 的调用仍可能成功。
§示例
File 实现了 Read:
use tokio::io::{self, AsyncReadExt};
use tokio::fs::File;
#[tokio::main]
async fn main() -> io::Result<()> {
let f = File::open("foo.txt").await?;
let mut buffer = [0; 5];
// read at most five bytes
let mut handle = f.take(5);
handle.read(&mut buffer).await?;
Ok(())
}动态兼容性§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.