跳到主要内容

AsyncWriteExt

搜索

特性 AsyncWriteExt 

Source
pub trait AsyncWriteExt: AsyncWrite {
Show 29 methods // Provided methods fn write<'a>(&'a mut self, src: &'a [u8]) -> Write<'a, Self> where Self: Unpin { ... } fn write_vectored<'a, 'b>( &'a mut self, bufs: &'a [IoSlice<'b>], ) -> WriteVectored<'a, 'b, Self> where Self: Unpin { ... } fn write_buf<'a, B>(&'a mut self, src: &'a mut B) -> WriteBuf<'a, Self, B> where Self: Sized + Unpin, B: Buf { ... } fn write_all_buf<'a, B>( &'a mut self, src: &'a mut B, ) -> WriteAllBuf<'a, Self, B> where Self: Sized + Unpin, B: Buf { ... } fn write_all<'a>(&'a mut self, src: &'a [u8]) -> WriteAll<'a, Self> where Self: Unpin { ... } fn write_u8(&mut self, n: u8) -> WriteU8<&mut Self> where Self: Unpin { ... } fn write_i8(&mut self, n: i8) -> WriteI8<&mut Self> where Self: Unpin { ... } fn write_u16(&mut self, n: u16) -> WriteU16<&mut Self> where Self: Unpin { ... } fn write_i16(&mut self, n: i16) -> WriteI16<&mut Self> where Self: Unpin { ... } fn write_u32(&mut self, n: u32) -> WriteU32<&mut Self> where Self: Unpin { ... } fn write_i32(&mut self, n: i32) -> WriteI32<&mut Self> where Self: Unpin { ... } fn write_u64(&mut self, n: u64) -> WriteU64<&mut Self> where Self: Unpin { ... } fn write_i64(&mut self, n: i64) -> WriteI64<&mut Self> where Self: Unpin { ... } fn write_u128(&mut self, n: u128) -> WriteU128<&mut Self> where Self: Unpin { ... } fn write_i128(&mut self, n: i128) -> WriteI128<&mut Self> where Self: Unpin { ... } fn write_f32(&mut self, n: f32) -> WriteF32<&mut Self> where Self: Unpin { ... } fn write_f64(&mut self, n: f64) -> WriteF64<&mut Self> where Self: Unpin { ... } fn write_u16_le(&mut self, n: u16) -> WriteU16Le<&mut Self> where Self: Unpin { ... } fn write_i16_le(&mut self, n: i16) -> WriteI16Le<&mut Self> where Self: Unpin { ... } fn write_u32_le(&mut self, n: u32) -> WriteU32Le<&mut Self> where Self: Unpin { ... } fn write_i32_le(&mut self, n: i32) -> WriteI32Le<&mut Self> where Self: Unpin { ... } fn write_u64_le(&mut self, n: u64) -> WriteU64Le<&mut Self> where Self: Unpin { ... } fn write_i64_le(&mut self, n: i64) -> WriteI64Le<&mut Self> where Self: Unpin { ... } fn write_u128_le(&mut self, n: u128) -> WriteU128Le<&mut Self> where Self: Unpin { ... } fn write_i128_le(&mut self, n: i128) -> WriteI128Le<&mut Self> where Self: Unpin { ... } fn write_f32_le(&mut self, n: f32) -> WriteF32Le<&mut Self> where Self: Unpin { ... } fn write_f64_le(&mut self, n: f64) -> WriteF64Le<&mut Self> where Self: Unpin { ... } fn flush(&mut self) -> Flush<'_, Self> where Self: Unpin { ... } fn shutdown(&mut self) -> Shutdown<'_, Self> where Self: Unpin { ... }
}
展开描述

将字节写入到接收器(sink)。

实现为一个扩展 trait,为所有 AsyncWrite 类型添加实用方法。 调用方倾向于导入此 trait 而不是 AsyncWrite

use tokio::io::{self, AsyncWriteExt};
use tokio::fs::File;

#[tokio::main]
async fn main() -> io::Result<()> {
    let data = b"some bytes";

    let mut pos = 0;
    let mut buffer = File::create("foo.txt").await?;

    while pos < data.len() {
        let bytes_written = buffer.write(&data[pos..]).await?;
        pos += bytes_written;
    }

    Ok(())
}

更多详情请参阅 module 文档。

提供方法§

Source

fn write<'a>(&'a mut self, src: &'a [u8]) -> Write<'a, Self>
where Self: Unpin,

将缓冲区写入此写入器,返回写入的字节数。

等价于:

async fn write(&mut self, buf: &[u8]) -> io::Result<usize>;

此函数将尝试写入 buf 的全部内容,但整个写入可能不会成功, 或者写入也可能产生错误。 对 write 的一次调用最多表示对任何包装对象的一次写入尝试。

§Return

如果返回值是 Ok(n),则必须保证 n <= buf.len()。 返回值为 0 通常表示底层对象已经无法再接受字节, 未来可能也无法再接受;或者提供的缓冲区为空。

§Errors

每次对 write 的调用都可能产生 I/O 错误, 表明该操作无法完成。如果返回错误, 那么缓冲区中没有任何字节被写入此写入器。

如果整个缓冲区无法写入此写入器,这被视为错误。

§Cancel safety

此方法是可取消安全的:如果在 tokio::select! 语句中将其作为事件, 而其他分支先完成,则保证不会向此 AsyncWrite 写入任何数据。

§示例
use tokio::io::{self, AsyncWriteExt};
use tokio::fs::File;

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut file = File::create("foo.txt").await?;

    // Writes some prefix of the byte string, not necessarily all of it.
    file.write(b"some bytes").await?;
    file.flush().await?;
    Ok(())
}
Source

fn write_vectored<'a, 'b>( &'a mut self, bufs: &'a [IoSlice<'b>], ) -> WriteVectored<'a, 'b, Self>
where Self: Unpin,

write 类似,但从一组缓冲区中写入数据。

等价于:

async fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize>;

更多详情请参阅 AsyncWrite::poll_write_vectored

§Cancel safety

此方法是可取消安全的:如果在 tokio::select! 语句中将其作为事件, 而其他分支先完成,则保证不会向此 AsyncWrite 写入任何数据。

§示例
use tokio::io::{self, AsyncWriteExt};
use tokio::fs::File;
use std::io::IoSlice;

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut file = File::create("foo.txt").await?;

    let bufs: &[_] = &[
        IoSlice::new(b"hello"),
        IoSlice::new(b" "),
        IoSlice::new(b"world"),
    ];

    file.write_vectored(&bufs).await?;
    file.flush().await?;

    Ok(())
}
Source

fn write_buf<'a, B>(&'a mut self, src: &'a mut B) -> WriteBuf<'a, Self, B>
where Self: Sized + Unpin, B: Buf,

将缓冲区写入此写入器,推进缓冲区的内部光标。

等价于:

async fn write_buf<B: Buf>(&mut self, buf: &mut B) -> io::Result<usize>;

此函数将尝试写入 buf 的全部内容,但整个写入可能不会成功, 或者写入也可能产生错误。 操作完成后,缓冲区的内部光标会按写入的字节数向前推进。 使用同一个 buf 值对 write_buf 的后续调用, 将从上一次 write_buf 调用完成时的位置继续。 对 write_buf 的一次调用最多表示对任何包装对象的一次写入尝试。

§Return

如果返回值是 Ok(n),则必须保证 n <= buf.len()。 返回值为 0 通常表示底层对象已经无法再接受字节, 未来可能也无法再接受;或者提供的缓冲区为空。

§Errors

每次对 write 的调用都可能产生 I/O 错误, 表明该操作无法完成。如果返回错误, 那么缓冲区中没有任何字节被写入此写入器。

如果整个缓冲区无法写入此写入器,这被视为错误。

§Cancel safety

此方法是可取消安全的:如果在 tokio::select! 语句中将其作为事件, 而其他分支先完成,则保证不会向此 AsyncWrite 写入任何数据。

§示例

File 实现了 AsyncWriteCursor<&[u8]> 实现了 Buf

use tokio::io::{self, AsyncWriteExt};
use tokio::fs::File;

use bytes::Buf;
use std::io::Cursor;

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut file = File::create("foo.txt").await?;
    let mut buffer = Cursor::new(b"data to write");

    // Loop until the entire contents of the buffer are written to
    // the file.
    while buffer.has_remaining() {
        // Writes some prefix of the byte string, not necessarily
        // all of it.
        file.write_buf(&mut buffer).await?;
    }
    file.flush().await?;

    Ok(())
}
Source

fn write_all_buf<'a, B>( &'a mut self, src: &'a mut B, ) -> WriteAllBuf<'a, Self, B>
where Self: Sized + Unpin, B: Buf,

尝试将整个缓冲区写入此写入器。

等价于:

async fn write_all_buf(&mut self, buf: impl Buf) -> Result<(), io::Error> {
    while buf.has_remaining() {
        self.write_buf(&mut buf).await?;
    }
    Ok(())
}

此方法会持续调用 write, 直到 buf.has_remaining() 返回 false。 此方法在缓冲区被完全写入或发生错误之前不会返回。 返回生成的第一个错误。

缓冲区在每次成功写入一块后都会推进。 失败后,src.chunk() 将返回写入失败的那个块。

§Cancel safety

如果在 tokio::select! 语句中将 write_all_buf 作为事件, 而其他分支先完成,那么提供的缓冲区中的数据可能被部分写入。 但可以保证提供的缓冲区已推进了已被部分写入的字节数。

§示例

File 实现了 AsyncWriteCursor<&[u8]> 实现了 Buf

use tokio::io::{self, AsyncWriteExt};
use tokio::fs::File;

use std::io::Cursor;

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut file = File::create("foo.txt").await?;
    let mut buffer = Cursor::new(b"data to write");

    file.write_all_buf(&mut buffer).await?;
    file.flush().await?;
    Ok(())
}
Source

fn write_all<'a>(&'a mut self, src: &'a [u8]) -> WriteAll<'a, Self>
where Self: Unpin,

尝试将整个缓冲区写入此写入器。

等价于:

async fn write_all(&mut self, buf: &[u8]) -> io::Result<()>;

此方法会持续调用 write, 直到没有更多数据可写。 此方法在缓冲区被完全写入或发生错误之前不会返回。 返回此方法生成的第一个错误。

§Cancel safety

此方法不是可取消安全的。如果在 tokio::select! 语句中将其作为事件, 而其他分支先完成,则提供的缓冲区可能已被部分写入, 但下次调用 write_all 将从缓冲区的开头重新开始。

§Errors

此函数将返回 write 返回的第一个错误。

§示例
use tokio::io::{self, AsyncWriteExt};
use tokio::fs::File;

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut file = File::create("foo.txt").await?;

    file.write_all(b"some bytes").await?;
    file.flush().await?;
    Ok(())
}
Source

fn write_u8(&mut self, n: u8) -> WriteU8<&mut Self>
where Self: Unpin,

向底层写入器写入一个无符号 8 位整数。

等价于:

async fn write_u8(&mut self, n: u8) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入无符号 8 位整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_u8(2).await?;
writer.write_u8(5).await?;

assert_eq!(writer, b"\x02\x05");
Ok(())
Source

fn write_i8(&mut self, n: i8) -> WriteI8<&mut Self>
where Self: Unpin,

向底层写入器写入一个有符号 8 位整数。

等价于:

async fn write_i8(&mut self, n: i8) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入有符号 8 位整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_i8(-2).await?;
writer.write_i8(126).await?;

assert_eq!(writer, b"\xFE\x7E");
Ok(())
Source

fn write_u16(&mut self, n: u16) -> WriteU16<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个无符号 16-位整数。

等价于:

async fn write_u16(&mut self, n: u16) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入无符号 16 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_u16(517).await?;
writer.write_u16(768).await?;

assert_eq!(writer, b"\x02\x05\x03\x00");
Ok(())
Source

fn write_i16(&mut self, n: i16) -> WriteI16<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个有符号 16-位整数。

等价于:

async fn write_i16(&mut self, n: i16) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入有符号 16 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_i16(193).await?;
writer.write_i16(-132).await?;

assert_eq!(writer, b"\x00\xc1\xff\x7c");
Ok(())
Source

fn write_u32(&mut self, n: u32) -> WriteU32<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个无符号 32-位整数。

等价于:

async fn write_u32(&mut self, n: u32) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入无符号 32 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_u32(267).await?;
writer.write_u32(1205419366).await?;

assert_eq!(writer, b"\x00\x00\x01\x0b\x47\xd9\x3d\x66");
Ok(())
Source

fn write_i32(&mut self, n: i32) -> WriteI32<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个有符号 32-位整数。

等价于:

async fn write_i32(&mut self, n: i32) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入有符号 32 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_i32(267).await?;
writer.write_i32(1205419366).await?;

assert_eq!(writer, b"\x00\x00\x01\x0b\x47\xd9\x3d\x66");
Ok(())
Source

fn write_u64(&mut self, n: u64) -> WriteU64<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个无符号 64-位整数。

等价于:

async fn write_u64(&mut self, n: u64) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入无符号 64 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_u64(918733457491587).await?;
writer.write_u64(143).await?;

assert_eq!(writer, b"\x00\x03\x43\x95\x4d\x60\x86\x83\x00\x00\x00\x00\x00\x00\x00\x8f");
Ok(())
Source

fn write_i64(&mut self, n: i64) -> WriteI64<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个有符号 64-位整数。

等价于:

async fn write_i64(&mut self, n: i64) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入有符号 64 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_i64(i64::MIN).await?;
writer.write_i64(i64::MAX).await?;

assert_eq!(writer, b"\x80\x00\x00\x00\x00\x00\x00\x00\x7f\xff\xff\xff\xff\xff\xff\xff");
Ok(())
Source

fn write_u128(&mut self, n: u128) -> WriteU128<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个无符号 128-位整数。

等价于:

async fn write_u128(&mut self, n: u128) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入无符号 128 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_u128(16947640962301618749969007319746179).await?;

assert_eq!(writer, vec![
    0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83,
    0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83
]);
Ok(())
Source

fn write_i128(&mut self, n: i128) -> WriteI128<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个有符号 128-位整数。

等价于:

async fn write_i128(&mut self, n: i128) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入有符号 128 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_i128(i128::MIN).await?;

assert_eq!(writer, vec![
    0x80, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0
]);
Ok(())
Source

fn write_f32(&mut self, n: f32) -> WriteF32<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个 32-位浮点类型。

等价于:

async fn write_f32(&mut self, n: f32) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入 32-位浮点类型:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_f32(f32::MIN).await?;

assert_eq!(writer, vec![0xff, 0x7f, 0xff, 0xff]);
Ok(())
Source

fn write_f64(&mut self, n: f64) -> WriteF64<&mut Self>
where Self: Unpin,

以大端字节序向底层写入器写入一个 64-位浮点类型。

等价于:

async fn write_f64(&mut self, n: f64) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入 64-位浮点类型:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_f64(f64::MIN).await?;

assert_eq!(writer, vec![
    0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
]);
Ok(())
Source

fn write_u16_le(&mut self, n: u16) -> WriteU16Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个无符号 16-位整数。

等价于:

async fn write_u16_le(&mut self, n: u16) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入无符号 16 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_u16_le(517).await?;
writer.write_u16_le(768).await?;

assert_eq!(writer, b"\x05\x02\x00\x03");
Ok(())
Source

fn write_i16_le(&mut self, n: i16) -> WriteI16Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个有符号 16-位整数。

等价于:

async fn write_i16_le(&mut self, n: i16) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入有符号 16 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_i16_le(193).await?;
writer.write_i16_le(-132).await?;

assert_eq!(writer, b"\xc1\x00\x7c\xff");
Ok(())
Source

fn write_u32_le(&mut self, n: u32) -> WriteU32Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个无符号 32-位整数。

等价于:

async fn write_u32_le(&mut self, n: u32) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入无符号 32 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_u32_le(267).await?;
writer.write_u32_le(1205419366).await?;

assert_eq!(writer, b"\x0b\x01\x00\x00\x66\x3d\xd9\x47");
Ok(())
Source

fn write_i32_le(&mut self, n: i32) -> WriteI32Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个有符号 32-位整数。

等价于:

async fn write_i32_le(&mut self, n: i32) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入有符号 32 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_i32_le(267).await?;
writer.write_i32_le(1205419366).await?;

assert_eq!(writer, b"\x0b\x01\x00\x00\x66\x3d\xd9\x47");
Ok(())
Source

fn write_u64_le(&mut self, n: u64) -> WriteU64Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个无符号 64-位整数。

等价于:

async fn write_u64_le(&mut self, n: u64) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入无符号 64 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_u64_le(918733457491587).await?;
writer.write_u64_le(143).await?;

assert_eq!(writer, b"\x83\x86\x60\x4d\x95\x43\x03\x00\x8f\x00\x00\x00\x00\x00\x00\x00");
Ok(())
Source

fn write_i64_le(&mut self, n: i64) -> WriteI64Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个有符号 64-位整数。

等价于:

async fn write_i64_le(&mut self, n: i64) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入有符号 64 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_i64_le(i64::MIN).await?;
writer.write_i64_le(i64::MAX).await?;

assert_eq!(writer, b"\x00\x00\x00\x00\x00\x00\x00\x80\xff\xff\xff\xff\xff\xff\xff\x7f");
Ok(())
Source

fn write_u128_le(&mut self, n: u128) -> WriteU128Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个无符号 128-位整数。

等价于:

async fn write_u128_le(&mut self, n: u128) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入无符号 128 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_u128_le(16947640962301618749969007319746179).await?;

assert_eq!(writer, vec![
    0x83, 0x86, 0x60, 0x4d, 0x95, 0x43, 0x03, 0x00,
    0x83, 0x86, 0x60, 0x4d, 0x95, 0x43, 0x03, 0x00,
]);
Ok(())
Source

fn write_i128_le(&mut self, n: i128) -> WriteI128Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个有符号 128-位整数。

等价于:

async fn write_i128_le(&mut self, n: i128) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入有符号 128 位大端整数:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_i128_le(i128::MIN).await?;

assert_eq!(writer, vec![
    0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0x80
]);
Ok(())
Source

fn write_f32_le(&mut self, n: f32) -> WriteF32Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个 32-位浮点类型。

等价于:

async fn write_f32_le(&mut self, n: f32) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入 32-位浮点类型:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_f32_le(f32::MIN).await?;

assert_eq!(writer, vec![0xff, 0xff, 0x7f, 0xff]);
Ok(())
Source

fn write_f64_le(&mut self, n: f64) -> WriteF64Le<&mut Self>
where Self: Unpin,

以小端字节序向底层写入器写入一个 64-位浮点类型。

等价于:

async fn write_f64_le(&mut self, n: f64) -> io::Result<()>;

建议使用缓冲写入器以避免过多的系统调用。

§Errors

此方法返回的错误与 AsyncWriteExt::write_all 相同。

§示例

AsyncWrite 写入 64-位浮点类型:

use tokio::io::{self, AsyncWriteExt};

let mut writer = Vec::new();

writer.write_f64_le(f64::MIN).await?;

assert_eq!(writer, vec![
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff
]);
Ok(())
Source

fn flush(&mut self) -> Flush<'_, Self>
where Self: Unpin,

刷新此输出流,确保所有中间缓冲的内容都到达目的地。

等价于:

async fn flush(&mut self) -> io::Result<()>;
§Errors

如果由于 I/O 错误或到达 EOF 而无法写入所有字节,则视为错误。

§Cancel safety

此方法是可取消安全的。

如果在 tokio::select! 语句中将 flush 作为事件, 而其他分支先完成,那么此 AsyncWrite 缓冲区中的数据可能被部分刷新。 但可以保证缓冲区已按已被部分刷新的字节数推进。

§示例
use tokio::io::{self, BufWriter, AsyncWriteExt};
use tokio::fs::File;

#[tokio::main]
async fn main() -> io::Result<()> {
    let f = File::create("foo.txt").await?;
    let mut buffer = BufWriter::new(f);

    buffer.write_all(b"some bytes").await?;
    buffer.flush().await?;
    Ok(())
}
Source

fn shutdown(&mut self) -> Shutdown<'_, Self>
where Self: Unpin,

关闭输出流,确保该值能被干净地丢弃。

等价于:

async fn shutdown(&mut self) -> io::Result<()>;

flush 类似,所有中间缓冲的内容都会写入到底层流。 操作完成后,调用方不应再尝试向流写入数据。 例如,TcpStream 的实现会发出 shutdown(Write) 系统调用。

§示例
use tokio::io::{self, BufWriter, AsyncWriteExt};
use tokio::fs::File;

#[tokio::main]
async fn main() -> io::Result<()> {
    let f = File::create("foo.txt").await?;
    let mut buffer = BufWriter::new(f);

    buffer.write_all(b"some bytes").await?;
    buffer.shutdown().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.

实现者§