跳到主要内容

BufMut

搜索

特性 BufMut 

Source
pub unsafe trait BufMut {
Show 48 methods // Required methods fn remaining_mut(&self) -> usize; unsafe fn advance_mut(&mut self, cnt: usize); fn chunk_mut(&mut self) -> &mut UninitSlice; // Provided methods fn has_remaining_mut(&self) -> bool { ... } fn put<T: Buf>(&mut self, src: T) where Self: Sized { ... } fn put_slice(&mut self, src: &[u8]) { ... } fn put_bytes(&mut self, val: u8, cnt: usize) { ... } fn put_u8(&mut self, n: u8) { ... } fn put_i8(&mut self, n: i8) { ... } fn put_u16(&mut self, n: u16) { ... } fn put_u16_le(&mut self, n: u16) { ... } fn put_u16_ne(&mut self, n: u16) { ... } fn put_i16(&mut self, n: i16) { ... } fn put_i16_le(&mut self, n: i16) { ... } fn put_i16_ne(&mut self, n: i16) { ... } fn put_u32(&mut self, n: u32) { ... } fn put_u32_le(&mut self, n: u32) { ... } fn put_u32_ne(&mut self, n: u32) { ... } fn put_i32(&mut self, n: i32) { ... } fn put_i32_le(&mut self, n: i32) { ... } fn put_i32_ne(&mut self, n: i32) { ... } fn put_u64(&mut self, n: u64) { ... } fn put_u64_le(&mut self, n: u64) { ... } fn put_u64_ne(&mut self, n: u64) { ... } fn put_i64(&mut self, n: i64) { ... } fn put_i64_le(&mut self, n: i64) { ... } fn put_i64_ne(&mut self, n: i64) { ... } fn put_u128(&mut self, n: u128) { ... } fn put_u128_le(&mut self, n: u128) { ... } fn put_u128_ne(&mut self, n: u128) { ... } fn put_i128(&mut self, n: i128) { ... } fn put_i128_le(&mut self, n: i128) { ... } fn put_i128_ne(&mut self, n: i128) { ... } fn put_uint(&mut self, n: u64, nbytes: usize) { ... } fn put_uint_le(&mut self, n: u64, nbytes: usize) { ... } fn put_uint_ne(&mut self, n: u64, nbytes: usize) { ... } fn put_int(&mut self, n: i64, nbytes: usize) { ... } fn put_int_le(&mut self, n: i64, nbytes: usize) { ... } fn put_int_ne(&mut self, n: i64, nbytes: usize) { ... } fn put_f32(&mut self, n: f32) { ... } fn put_f32_le(&mut self, n: f32) { ... } fn put_f32_ne(&mut self, n: f32) { ... } fn put_f64(&mut self, n: f64) { ... } fn put_f64_le(&mut self, n: f64) { ... } fn put_f64_ne(&mut self, n: f64) { ... } fn limit(self, limit: usize) -> Limit<Self> where Self: Sized { ... } fn writer(self) -> Writer<Self> where Self: Sized { ... } fn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U> where Self: Sized { ... }
}
展开描述

为值提供对字节的顺序写访问的 trait。

向缓冲区写入字节

缓冲区将字节存储在内存中,以便写入操作不会失败。底层存储可能连续也可能不连续。BufMut 值是指向缓冲区的游标。向 BufMut 写入会推进游标位置。

最简单的 BufMutVec<u8>

use bytes::BufMut;

let mut buf = vec![];

buf.put(&b"hello world"[..]);

assert_eq!(buf, b"hello world");

必需方法§

Source

fn remaining_mut(&self) -> usize

返回从当前位置到缓冲区末尾可写入的字节数。

此值大于或等于 chunk_mut() 返回切片的长度。

BufMut 写入可能涉及按需分配更多内存。若遇到分配失败,实现可能在该方法指示的字节数之前就失败。

§示例
use bytes::BufMut;

let mut dst = [0; 10];
let mut buf = &mut dst[..];

let original_remaining = buf.remaining_mut();
buf.put(&b"hello"[..]);

assert_eq!(original_remaining - 5, buf.remaining_mut());
§Implementer notes

remaining_mut 的实现应确保返回值不会改变,除非调用了 advance_mut 或任何其他文档明确会改变 BufMut’s 当前位置的函数。

§Note

remaining_mut 可能返回比实际可用空间更小的值。

Source

unsafe fn advance_mut(&mut self, cnt: usize)

推进 BufMut 的内部游标

下一次调用 chunk_mut() 时将返回一个从底层缓冲区再深入 cnt 字节开始的切片。

§Safety

调用方必须确保 chunk 的下 cnt 字节已初始化。

§示例
use bytes::BufMut;

let mut buf = Vec::with_capacity(16);

// Write some data
buf.chunk_mut()[0..2].copy_from_slice(b"he");
unsafe { buf.advance_mut(2) };

// write more bytes
buf.chunk_mut()[0..3].copy_from_slice(b"llo");

unsafe { buf.advance_mut(3); }

assert_eq!(5, buf.len());
assert_eq!(buf, b"hello");
§Panics

cnt > self.remaining_mut(),此函数可能会 panic。

§Implementer notes

建议 advance_mut 的实现在 cnt > self.remaining_mut() 时 panic。若实现不 panic,则调用行为必须如同 cnt == self.remaining_mut()

调用 cnt == 0 时绝不应 panic,且应为无操作。

Source

fn chunk_mut(&mut self) -> &mut UninitSlice

返回从当前 BufMut 位置开始、长度介于 0 和 BufMut::remaining_mut() 之间的可变切片。注意这可能比缓冲区剩余总量更短(允许非连续的内部实现)。

这是一个较低级别的函数。大多数操作通过其他函数完成。

返回的字节切片可能表示未初始化的内存。

§示例
use bytes::BufMut;

let mut buf = Vec::with_capacity(16);

unsafe {
    // MaybeUninit::as_mut_ptr
    buf.chunk_mut()[0..].as_mut_ptr().write(b'h');
    buf.chunk_mut()[1..].as_mut_ptr().write(b'e');

    buf.advance_mut(2);

    buf.chunk_mut()[0..].as_mut_ptr().write(b'l');
    buf.chunk_mut()[1..].as_mut_ptr().write(b'l');
    buf.chunk_mut()[2..].as_mut_ptr().write(b'o');

    buf.advance_mut(3);
}

assert_eq!(5, buf.len());
assert_eq!(buf, b"hello");
§Implementer notes

此函数不应 panic。chunk_mut() 返回空切片当且仅当 remaining_mut() 返回 0。换言之,chunk_mut() 返回空切片意味着 remaining_mut() 将返回 0,而 remaining_mut() 返回 0 意味着 chunk_mut() 将返回空切片。

若此函数尝试分配内存但失败,可能会触发内存不足中止。

提供方法§

Source

fn has_remaining_mut(&self) -> bool

若 self 中还有更多字节的空间则返回 true。

这等价于 self.remaining_mut() != 0

§示例
use bytes::BufMut;

let mut dst = [0; 5];
let mut buf = &mut dst[..];

assert!(buf.has_remaining_mut());

buf.put(&b"hello"[..]);

assert!(!buf.has_remaining_mut());
Source

fn put<T: Buf>(&mut self, src: T)
where Self: Sized,

src 的字节转移到 self 中,并按已写入字节数推进游标。

§示例
use bytes::BufMut;

let mut buf = vec![];

buf.put_u8(b'h');
buf.put(&b"ello"[..]);
buf.put(&b" world"[..]);

assert_eq!(buf, b"hello world");
§Panics

若 self 没有足够容量容纳 src,则 panic。

Source

fn put_slice(&mut self, src: &[u8])

src 中的字节传入 self,并将游标 前移已写入的字节数。

self 的剩余容量必须足以容纳整个 src

use bytes::BufMut;

let mut dst = [0; 6];

{
    let mut buf = &mut dst[..];
    buf.put_slice(b"hello");

    assert_eq!(1, buf.remaining_mut());
}

assert_eq!(b"hello\0", &dst);
Source

fn put_bytes(&mut self, val: u8, cnt: usize)

self 写入 cnt 个字节 val

逻辑上等价于连续调用 self.put_u8(val)cnt 次,但可能更快。

self 的剩余容量必须至少为 cnt

use bytes::BufMut;

let mut dst = [0; 6];

{
    let mut buf = &mut dst[..];
    buf.put_bytes(b'a', 4);

    assert_eq!(2, buf.remaining_mut());
}

assert_eq!(b"aaaa\0\0", &dst);
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u8(&mut self, n: u8)

将无符号 8 位整数写入 self。

当前位置前进 1 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u8(0x01);
assert_eq!(buf, b"\x01");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i8(&mut self, n: i8)

将有符号 8 位整数写入 self。

当前位置前进 1 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i8(0x01);
assert_eq!(buf, b"\x01");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u16(&mut self, n: u16)

以大端字节序将无符号 16 位整数写入 self。

当前位置前进 2 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u16(0x0809);
assert_eq!(buf, b"\x08\x09");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u16_le(&mut self, n: u16)

以小端字节序将无符号 16 位整数写入 self。

当前位置前进 2 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u16_le(0x0809);
assert_eq!(buf, b"\x09\x08");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u16_ne(&mut self, n: u16)

以原生字节序将无符号 16 位整数写入 self。

当前位置前进 2 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u16_ne(0x0809);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x08\x09");
} else {
    assert_eq!(buf, b"\x09\x08");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i16(&mut self, n: i16)

以大端字节序将有符号 16 位整数写入 self。

当前位置前进 2 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i16(0x0809);
assert_eq!(buf, b"\x08\x09");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i16_le(&mut self, n: i16)

以小端字节序将有符号 16 位整数写入 self。

当前位置前进 2 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i16_le(0x0809);
assert_eq!(buf, b"\x09\x08");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i16_ne(&mut self, n: i16)

以原生字节序将有符号 16 位整数写入 self。

当前位置前进 2 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i16_ne(0x0809);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x08\x09");
} else {
    assert_eq!(buf, b"\x09\x08");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u32(&mut self, n: u32)

以大端字节序将无符号 32 位整数写入 self。

当前位置前进 4 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u32(0x0809A0A1);
assert_eq!(buf, b"\x08\x09\xA0\xA1");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u32_le(&mut self, n: u32)

以小端字节序将无符号 32 位整数写入 self。

当前位置前进 4 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u32_le(0x0809A0A1);
assert_eq!(buf, b"\xA1\xA0\x09\x08");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u32_ne(&mut self, n: u32)

以原生字节序将无符号 32 位整数写入 self。

当前位置前进 4 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u32_ne(0x0809A0A1);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x08\x09\xA0\xA1");
} else {
    assert_eq!(buf, b"\xA1\xA0\x09\x08");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i32(&mut self, n: i32)

以大端字节序将有符号 32 位整数写入 self。

当前位置前进 4 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i32(0x0809A0A1);
assert_eq!(buf, b"\x08\x09\xA0\xA1");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i32_le(&mut self, n: i32)

以小端字节序将有符号 32 位整数写入 self。

当前位置前进 4 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i32_le(0x0809A0A1);
assert_eq!(buf, b"\xA1\xA0\x09\x08");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i32_ne(&mut self, n: i32)

以原生字节序将有符号 32 位整数写入 self。

当前位置前进 4 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i32_ne(0x0809A0A1);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x08\x09\xA0\xA1");
} else {
    assert_eq!(buf, b"\xA1\xA0\x09\x08");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u64(&mut self, n: u64)

以大端字节序将无符号 64 位整数写入 self。

当前位置前进 8 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u64(0x0102030405060708);
assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u64_le(&mut self, n: u64)

以小端字节序将无符号 64 位整数写入 self。

当前位置前进 8 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u64_le(0x0102030405060708);
assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u64_ne(&mut self, n: u64)

以原生字节序将无符号 64 位整数写入 self。

当前位置前进 8 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u64_ne(0x0102030405060708);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
} else {
    assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i64(&mut self, n: i64)

以大端字节序将有符号 64 位整数写入 self。

当前位置前进 8 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i64(0x0102030405060708);
assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i64_le(&mut self, n: i64)

以小端字节序将有符号 64 位整数写入 self。

当前位置前进 8 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i64_le(0x0102030405060708);
assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i64_ne(&mut self, n: i64)

以原生字节序将有符号 64 位整数写入 self。

当前位置前进 8 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i64_ne(0x0102030405060708);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08");
} else {
    assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u128(&mut self, n: u128)

以大端字节序将无符号 128 位整数写入 self。

当前位置前进 16 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u128(0x01020304050607080910111213141516);
assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u128_le(&mut self, n: u128)

以小端字节序将无符号 128 位整数写入 self。

当前位置前进 16 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u128_le(0x01020304050607080910111213141516);
assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_u128_ne(&mut self, n: u128)

以原生字节序将无符号 128 位整数写入 self。

当前位置前进 16 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_u128_ne(0x01020304050607080910111213141516);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
} else {
    assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i128(&mut self, n: i128)

以大端字节序将有符号 128 位整数写入 self。

当前位置前进 16 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i128(0x01020304050607080910111213141516);
assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i128_le(&mut self, n: i128)

以小端字节序将有符号 128 位整数写入 self。

当前位置前进 16 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i128_le(0x01020304050607080910111213141516);
assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_i128_ne(&mut self, n: i128)

以原生字节序将有符号 128 位整数写入 self。

当前位置前进 16 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_i128_ne(0x01020304050607080910111213141516);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16");
} else {
    assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_uint(&mut self, n: u64, nbytes: usize)

以大端字节序将无符号 n 字节整数写入 self。

当前位置推进 nbytes 字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_uint(0x010203, 3);
assert_eq!(buf, b"\x01\x02\x03");
§Panics

若 self 中没有足够剩余容量,或 nbytes 大于 8,此函数会 panic。

Source

fn put_uint_le(&mut self, n: u64, nbytes: usize)

以小端字节序将无符号 n 字节整数写入 self。

当前位置推进 nbytes 字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_uint_le(0x010203, 3);
assert_eq!(buf, b"\x03\x02\x01");
§Panics

若 self 中没有足够剩余容量,或 nbytes 大于 8,此函数会 panic。

Source

fn put_uint_ne(&mut self, n: u64, nbytes: usize)

以原生字节序将无符号 n 字节整数写入 self。

当前位置推进 nbytes 字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_uint_ne(0x010203, 3);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x01\x02\x03");
} else {
    assert_eq!(buf, b"\x03\x02\x01");
}
§Panics

若 self 中没有足够剩余容量,或 nbytes 大于 8,此函数会 panic。

Source

fn put_int(&mut self, n: i64, nbytes: usize)

以大端字节序将有符号整数的低 nbytes 位写入 self。

当前位置推进 nbytes 字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_int(0x0504010203, 3);
assert_eq!(buf, b"\x01\x02\x03");
§Panics

若 self 中没有足够剩余容量,或 nbytes 大于 8,此函数会 panic。

Source

fn put_int_le(&mut self, n: i64, nbytes: usize)

以小端字节序将有符号整数的低 nbytes 位写入 self。

当前位置推进 nbytes 字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_int_le(0x0504010203, 3);
assert_eq!(buf, b"\x03\x02\x01");
§Panics

若 self 中没有足够剩余容量,或 nbytes 大于 8,此函数会 panic。

Source

fn put_int_ne(&mut self, n: i64, nbytes: usize)

以原生字节序将有符号整数的低 nbytes 位写入 self。

当前位置推进 nbytes 字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_int_ne(0x010203, 3);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x01\x02\x03");
} else {
    assert_eq!(buf, b"\x03\x02\x01");
}
§Panics

若 self 中没有足够剩余容量,或 nbytes 大于 8,此函数会 panic。

Source

fn put_f32(&mut self, n: f32)

以大端字节序将 IEEE754 单精度(4 字节)浮点数写入 self。

当前位置前进 4 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_f32(1.2f32);
assert_eq!(buf, b"\x3F\x99\x99\x9A");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_f32_le(&mut self, n: f32)

以小端字节序将 IEEE754 单精度(4 字节)浮点数写入 self。

当前位置前进 4 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_f32_le(1.2f32);
assert_eq!(buf, b"\x9A\x99\x99\x3F");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_f32_ne(&mut self, n: f32)

以原生字节序将 IEEE754 单精度(4 字节)浮点数写入 self。

当前位置前进 4 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_f32_ne(1.2f32);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x3F\x99\x99\x9A");
} else {
    assert_eq!(buf, b"\x9A\x99\x99\x3F");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_f64(&mut self, n: f64)

以大端字节序将 IEEE754 双精度(8 字节)浮点数写入 self。

当前位置前进 8 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_f64(1.2f64);
assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_f64_le(&mut self, n: f64)

以小端字节序将 IEEE754 双精度(8 字节)浮点数写入 self。

当前位置前进 8 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_f64_le(1.2f64);
assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F");
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn put_f64_ne(&mut self, n: f64)

以原生字节序将 IEEE754 双精度(8 字节)浮点数写入 self。

当前位置前进 8 个字节。

§示例
use bytes::BufMut;

let mut buf = vec![];
buf.put_f64_ne(1.2f64);
if cfg!(target_endian = "big") {
    assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33");
} else {
    assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F");
}
§Panics

若 self 中没有足够剩余容量,此函数会 panic。

Source

fn limit(self, limit: usize) -> Limit<Self>
where Self: Sized,

创建一个适配器,向 self 写入最多 limit 字节。

§示例
use bytes::BufMut;

let arr = &mut [0u8; 128][..];
assert_eq!(arr.remaining_mut(), 128);

let dst = arr.limit(10);
assert_eq!(dst.remaining_mut(), 10);
Source

fn writer(self) -> Writer<Self>
where Self: Sized,

创建一个为 self 实现 Write trait 的适配器。

此函数返回一个新值,通过将 Write trait 函数适配到 BufMut trait 函数来实现 Write。由于 BufMut 操作是 infallible 的,Write 函数都不会返回 Err

§示例
use bytes::BufMut;
use std::io::Write;

let mut buf = vec![].writer();

let num = buf.write(&b"hello world"[..]).unwrap();
assert_eq!(11, num);

let buf = buf.into_inner();

assert_eq!(*buf, b"hello world"[..]);
Source

fn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U>
where Self: Sized,

创建一个将此缓冲区与另一个缓冲区链接起来的适配器。

返回的 BufMut 实例将先写入 self 中的所有字节。之后将写入 next

§示例
use bytes::BufMut;

let mut a = [0u8; 5];
let mut b = [0u8; 6];

let mut chain = (&mut a[..]).chain_mut(&mut b[..]);

chain.put_slice(b"hello world");

assert_eq!(&a[..], b"hello");
assert_eq!(&b[..], b" world");

Implementations on Foreign Types§

Source§

impl BufMut for &mut [u8]

Source§

fn remaining_mut(&self) -> usize

Source§

fn chunk_mut(&mut self) -> &mut UninitSlice

Source§

unsafe fn advance_mut(&mut self, cnt: usize)

Source§

fn put_slice(&mut self, src: &[u8])

Source§

fn put_bytes(&mut self, val: u8, cnt: usize)

Source§

impl BufMut for &mut [MaybeUninit<u8>]

Source§

fn remaining_mut(&self) -> usize

Source§

fn chunk_mut(&mut self) -> &mut UninitSlice

Source§

unsafe fn advance_mut(&mut self, cnt: usize)

Source§

fn put_slice(&mut self, src: &[u8])

Source§

fn put_bytes(&mut self, val: u8, cnt: usize)

Source§

impl BufMut for Vec<u8>

Source§

fn remaining_mut(&self) -> usize

Source§

unsafe fn advance_mut(&mut self, cnt: usize)

Source§

fn chunk_mut(&mut self) -> &mut UninitSlice

Source§

fn put<T: Buf>(&mut self, src: T)
where Self: Sized,

Source§

fn put_slice(&mut self, src: &[u8])

Source§

fn put_bytes(&mut self, val: u8, cnt: usize)

Source§

impl<T: BufMut + ?Sized> BufMut for &mut T

Source§

fn remaining_mut(&self) -> usize

Source§

fn chunk_mut(&mut self) -> &mut UninitSlice

Source§

unsafe fn advance_mut(&mut self, cnt: usize)

Source§

fn put_slice(&mut self, src: &[u8])

Source§

fn put_u8(&mut self, n: u8)

Source§

fn put_i8(&mut self, n: i8)

Source§

fn put_u16(&mut self, n: u16)

Source§

fn put_u16_le(&mut self, n: u16)

Source§

fn put_u16_ne(&mut self, n: u16)

Source§

fn put_i16(&mut self, n: i16)

Source§

fn put_i16_le(&mut self, n: i16)

Source§

fn put_i16_ne(&mut self, n: i16)

Source§

fn put_u32(&mut self, n: u32)

Source§

fn put_u32_le(&mut self, n: u32)

Source§

fn put_u32_ne(&mut self, n: u32)

Source§

fn put_i32(&mut self, n: i32)

Source§

fn put_i32_le(&mut self, n: i32)

Source§

fn put_i32_ne(&mut self, n: i32)

Source§

fn put_u64(&mut self, n: u64)

Source§

fn put_u64_le(&mut self, n: u64)

Source§

fn put_u64_ne(&mut self, n: u64)

Source§

fn put_i64(&mut self, n: i64)

Source§

fn put_i64_le(&mut self, n: i64)

Source§

fn put_i64_ne(&mut self, n: i64)

Source§

impl<T: BufMut + ?Sized> BufMut for Box<T>

Source§

fn remaining_mut(&self) -> usize

Source§

fn chunk_mut(&mut self) -> &mut UninitSlice

Source§

unsafe fn advance_mut(&mut self, cnt: usize)

Source§

fn put_slice(&mut self, src: &[u8])

Source§

fn put_u8(&mut self, n: u8)

Source§

fn put_i8(&mut self, n: i8)

Source§

fn put_u16(&mut self, n: u16)

Source§

fn put_u16_le(&mut self, n: u16)

Source§

fn put_u16_ne(&mut self, n: u16)

Source§

fn put_i16(&mut self, n: i16)

Source§

fn put_i16_le(&mut self, n: i16)

Source§

fn put_i16_ne(&mut self, n: i16)

Source§

fn put_u32(&mut self, n: u32)

Source§

fn put_u32_le(&mut self, n: u32)

Source§

fn put_u32_ne(&mut self, n: u32)

Source§

fn put_i32(&mut self, n: i32)

Source§

fn put_i32_le(&mut self, n: i32)

Source§

fn put_i32_ne(&mut self, n: i32)

Source§

fn put_u64(&mut self, n: u64)

Source§

fn put_u64_le(&mut self, n: u64)

Source§

fn put_u64_ne(&mut self, n: u64)

Source§

fn put_i64(&mut self, n: i64)

Source§

fn put_i64_le(&mut self, n: i64)

Source§

fn put_i64_ne(&mut self, n: i64)

实现者§

Source§

impl BufMut for BytesMut

Source§

impl<T, U> BufMut for Chain<T, U>
where T: BufMut, U: BufMut,

Source§

impl<T: BufMut> BufMut for Limit<T>