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 写入会推进游标位置。
最简单的 BufMut 是 Vec<u8>。
use bytes::BufMut;
let mut buf = vec![];
buf.put(&b"hello world"[..]);
assert_eq!(buf, b"hello world");必需方法§
Sourcefn remaining_mut(&self) -> usize
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 可能返回比实际可用空间更小的值。
Sourceunsafe fn advance_mut(&mut self, cnt: usize)
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,且应为无操作。
Sourcefn chunk_mut(&mut self) -> &mut UninitSlice
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() 将返回空切片。
若此函数尝试分配内存但失败,可能会触发内存不足中止。
提供方法§
Sourcefn has_remaining_mut(&self) -> bool
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());Sourcefn put_slice(&mut self, src: &[u8])
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);Sourcefn put_bytes(&mut self, val: u8, cnt: usize)
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。
Sourcefn put_u16_le(&mut self, n: u16)
fn put_u16_le(&mut self, n: u16)
Sourcefn put_u16_ne(&mut self, n: u16)
fn put_u16_ne(&mut self, n: u16)
Sourcefn put_i16_le(&mut self, n: i16)
fn put_i16_le(&mut self, n: i16)
Sourcefn put_i16_ne(&mut self, n: i16)
fn put_i16_ne(&mut self, n: i16)
Sourcefn put_u32_le(&mut self, n: u32)
fn put_u32_le(&mut self, n: u32)
Sourcefn put_u32_ne(&mut self, n: u32)
fn put_u32_ne(&mut self, n: u32)
Sourcefn put_i32_le(&mut self, n: i32)
fn put_i32_le(&mut self, n: i32)
Sourcefn put_i32_ne(&mut self, n: i32)
fn put_i32_ne(&mut self, n: i32)
Sourcefn put_u64_le(&mut self, n: u64)
fn put_u64_le(&mut self, n: u64)
Sourcefn put_u64_ne(&mut self, n: u64)
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。
Sourcefn put_i64_le(&mut self, n: i64)
fn put_i64_le(&mut self, n: i64)
Sourcefn put_i64_ne(&mut self, n: i64)
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。
Sourcefn put_u128_le(&mut self, n: u128)
fn put_u128_le(&mut self, n: u128)
Sourcefn put_u128_ne(&mut self, n: u128)
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。
Sourcefn put_i128_le(&mut self, n: i128)
fn put_i128_le(&mut self, n: i128)
Sourcefn put_i128_ne(&mut self, n: i128)
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。
Sourcefn put_uint_le(&mut self, n: u64, nbytes: usize)
fn put_uint_le(&mut self, n: u64, nbytes: usize)
Sourcefn put_uint_ne(&mut self, n: u64, nbytes: usize)
fn put_uint_ne(&mut self, n: u64, nbytes: usize)
Sourcefn put_int_le(&mut self, n: i64, nbytes: usize)
fn put_int_le(&mut self, n: i64, nbytes: usize)
Sourcefn put_int_ne(&mut self, n: i64, nbytes: usize)
fn put_int_ne(&mut self, n: i64, nbytes: usize)
Sourcefn put_f32_le(&mut self, n: f32)
fn put_f32_le(&mut self, n: f32)
Sourcefn put_f32_ne(&mut self, n: f32)
fn put_f32_ne(&mut self, n: f32)
Sourcefn put_f64_le(&mut self, n: f64)
fn put_f64_le(&mut self, n: f64)
Sourcefn put_f64_ne(&mut self, n: f64)
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。
Sourcefn limit(self, limit: usize) -> Limit<Self>where
Self: Sized,
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);Sourcefn writer(self) -> Writer<Self> ⓘwhere
Self: Sized,
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"[..]);Sourcefn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U>where
Self: Sized,
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");