pub struct ReadBuf<'a> { /* private fields */ }展开描述
对字节缓冲区的包装,以增量方式填充和初始化。
此类型 可视为“双游标”。 它在缓冲区中跟踪三个区域: 缓冲区开头 已逻辑填充数据的区域、 某时已初始化但 尚未逻辑填充的区域, 以及末尾 可能未初始化的区域。 填充区域保证是初始化区域的子集。
概括而言,缓冲区的内容可以可视化如下:
[ capacity ]
[ filled | unfilled ]
[ initialized | uninitialized ]对未初始化区域中的任何字节执行反初始化是未定义行为, 因为仅仅是不清楚该区域是否已初始化; 如果其中某些部分实际上是已初始化的,那么它必须保持已初始化状态。
实现§
Source§impl<'a> ReadBuf<'a>
impl<'a> ReadBuf<'a>
Sourcepub fn uninit(buf: &'a mut [MaybeUninit<u8>]) -> ReadBuf<'a>
pub fn uninit(buf: &'a mut [MaybeUninit<u8>]) -> ReadBuf<'a>
从一个可能未初始化的缓冲区创建一个新的 ReadBuf。
内部光标会将整个缓冲区标记为未初始化。
如果已知缓冲区是部分初始化的,那么可以使用 assume_init 来移动内部光标。
Sourcepub fn filled_mut(&mut self) -> &mut [u8] ⓘ
pub fn filled_mut(&mut self) -> &mut [u8] ⓘ
返回对缓冲区已填充部分的可变引用。
Sourcepub fn initialized(&self) -> &[u8] ⓘ
pub fn initialized(&self) -> &[u8] ⓘ
返回对缓冲区已初始化部分的共享引用。
其中包括已填充的部分。
Sourcepub fn initialized_mut(&mut self) -> &mut [u8] ⓘ
pub fn initialized_mut(&mut self) -> &mut [u8] ⓘ
返回对缓冲区已初始化部分的可变引用。
其中包括已填充的部分。
Sourcepub unsafe fn inner_mut(&mut self) -> &mut [MaybeUninit<u8>]
pub unsafe fn inner_mut(&mut self) -> &mut [MaybeUninit<u8>]
返回对整个缓冲区的可变引用,不保证它已被完全初始化。
下标在 0 到 self.filled().len() 之间的元素已被填充,
0 到 self.initialized().len() 之间的元素已被初始化(因此可以转换为 &mut [u8])。
此方法的调用方必须确保这些不变量得到遵守。
例如,如果调用方对缓冲区中部分未初始化部分执行了初始化,
则必须使用已初始化的字节数调用 assume_init。
§Safety
调用方不得对已经初始化的缓冲区部分执行反初始化。
其中也包括被 ReadBuf 标记为未初始化区域中的任何字节。
Sourcepub unsafe fn unfilled_mut(&mut self) -> &mut [MaybeUninit<u8>]
pub unsafe fn unfilled_mut(&mut self) -> &mut [MaybeUninit<u8>]
Sourcepub fn initialize_unfilled(&mut self) -> &mut [u8] ⓘ
pub fn initialize_unfilled(&mut self) -> &mut [u8] ⓘ
返回对缓冲区未填充部分的可变引用,确保它已被完全初始化。
由于 ReadBuf 会跟踪缓冲区中已被初始化的区域,因此首次使用之后这实际上是“免费的”。
Sourcepub fn initialize_unfilled_to(&mut self, n: usize) -> &mut [u8] ⓘ
pub fn initialize_unfilled_to(&mut self, n: usize) -> &mut [u8] ⓘ
Sourcepub fn set_filled(&mut self, n: usize)
pub fn set_filled(&mut self, n: usize)
设置缓冲区已填充区域的大小。
已初始化的字节数不会改变。
请注意,除了扩大已填充区域之外,
此方法也可以用来缩小已填充区域(例如,由 AsyncRead 实现原地压缩数据时)。
§Panics
如果缓冲区已填充区域变得大于已初始化区域,则 panic。
Sourcepub unsafe fn assume_init(&mut self, n: usize)
pub unsafe fn assume_init(&mut self, n: usize)
断言缓冲区中前 n 个未填充字节已被初始化。
ReadBuf 假定字节永远不会被反初始化,
因此当使用比已知已初始化字节数更少的字节调用此方法时,它不会执行任何操作。
§Safety
调用方必须确保缓冲区中 n 个未填充字节已被初始化。
Trait 实现§
Source§impl<'a> BufMut for ReadBuf<'a>
Available on crate feature io-util only.
impl<'a> BufMut for ReadBuf<'a>
io-util only.Source§fn remaining_mut(&self) -> usize
fn remaining_mut(&self) -> usize
Source§unsafe fn advance_mut(&mut self, cnt: usize)
unsafe fn advance_mut(&mut self, cnt: usize)
Source§fn chunk_mut(&mut self) -> &mut UninitSlice
fn chunk_mut(&mut self) -> &mut UninitSlice
BufMut::remaining_mut(). Note that this can be shorter than the
whole remainder of the buffer (this allows non-continuous implementation). 更多信息Source§fn has_remaining_mut(&self) -> bool
fn has_remaining_mut(&self) -> bool
self for more bytes. 更多信息Source§fn put_u16(&mut self, n: u16)
fn put_u16(&mut self, n: u16)
self in big-endian byte order. 更多信息Source§fn put_u16_le(&mut self, n: u16)
fn put_u16_le(&mut self, n: u16)
self in little-endian byte order. 更多信息Source§fn put_u16_ne(&mut self, n: u16)
fn put_u16_ne(&mut self, n: u16)
self in native-endian byte order. 更多信息Source§fn put_i16(&mut self, n: i16)
fn put_i16(&mut self, n: i16)
self in big-endian byte order. 更多信息Source§fn put_i16_le(&mut self, n: i16)
fn put_i16_le(&mut self, n: i16)
self in little-endian byte order. 更多信息Source§fn put_i16_ne(&mut self, n: i16)
fn put_i16_ne(&mut self, n: i16)
self in native-endian byte order. 更多信息Source§fn put_u32(&mut self, n: u32)
fn put_u32(&mut self, n: u32)
self in big-endian byte order. 更多信息Source§fn put_u32_le(&mut self, n: u32)
fn put_u32_le(&mut self, n: u32)
self in little-endian byte order. 更多信息Source§fn put_u32_ne(&mut self, n: u32)
fn put_u32_ne(&mut self, n: u32)
self in native-endian byte order. 更多信息Source§fn put_i32(&mut self, n: i32)
fn put_i32(&mut self, n: i32)
self in big-endian byte order. 更多信息Source§fn put_i32_le(&mut self, n: i32)
fn put_i32_le(&mut self, n: i32)
self in little-endian byte order. 更多信息Source§fn put_i32_ne(&mut self, n: i32)
fn put_i32_ne(&mut self, n: i32)
self in native-endian byte order. 更多信息Source§fn put_u64(&mut self, n: u64)
fn put_u64(&mut self, n: u64)
self in the big-endian byte order. 更多信息Source§fn put_u64_le(&mut self, n: u64)
fn put_u64_le(&mut self, n: u64)
self in little-endian byte order. 更多信息Source§fn put_u64_ne(&mut self, n: u64)
fn put_u64_ne(&mut self, n: u64)
self in native-endian byte order. 更多信息Source§fn put_i64(&mut self, n: i64)
fn put_i64(&mut self, n: i64)
self in the big-endian byte order. 更多信息Source§fn put_i64_le(&mut self, n: i64)
fn put_i64_le(&mut self, n: i64)
self in little-endian byte order. 更多信息Source§fn put_i64_ne(&mut self, n: i64)
fn put_i64_ne(&mut self, n: i64)
self in native-endian byte order. 更多信息Source§fn put_u128(&mut self, n: u128)
fn put_u128(&mut self, n: u128)
self in the big-endian byte order. 更多信息Source§fn put_u128_le(&mut self, n: u128)
fn put_u128_le(&mut self, n: u128)
self in little-endian byte order. 更多信息Source§fn put_u128_ne(&mut self, n: u128)
fn put_u128_ne(&mut self, n: u128)
self in native-endian byte order. 更多信息Source§fn put_i128(&mut self, n: i128)
fn put_i128(&mut self, n: i128)
self in the big-endian byte order. 更多信息Source§fn put_i128_le(&mut self, n: i128)
fn put_i128_le(&mut self, n: i128)
self in little-endian byte order. 更多信息Source§fn put_i128_ne(&mut self, n: i128)
fn put_i128_ne(&mut self, n: i128)
self in native-endian byte order. 更多信息Source§fn put_uint(&mut self, n: u64, nbytes: usize)
fn put_uint(&mut self, n: u64, nbytes: usize)
self in big-endian byte order. 更多信息Source§fn put_uint_le(&mut self, n: u64, nbytes: usize)
fn put_uint_le(&mut self, n: u64, nbytes: usize)
self in the little-endian byte order. 更多信息Source§fn put_uint_ne(&mut self, n: u64, nbytes: usize)
fn put_uint_ne(&mut self, n: u64, nbytes: usize)
self in the native-endian byte order. 更多信息Source§fn put_int_le(&mut self, n: i64, nbytes: usize)
fn put_int_le(&mut self, n: i64, nbytes: usize)
Source§fn put_int_ne(&mut self, n: i64, nbytes: usize)
fn put_int_ne(&mut self, n: i64, nbytes: usize)
Source§fn put_f32(&mut self, n: f32)
fn put_f32(&mut self, n: f32)
self in big-endian byte order. 更多信息Source§fn put_f32_le(&mut self, n: f32)
fn put_f32_le(&mut self, n: f32)
self in little-endian byte order. 更多信息Source§fn put_f32_ne(&mut self, n: f32)
fn put_f32_ne(&mut self, n: f32)
self in native-endian byte order. 更多信息Source§fn put_f64(&mut self, n: f64)
fn put_f64(&mut self, n: f64)
self in big-endian byte order. 更多信息Source§fn put_f64_le(&mut self, n: f64)
fn put_f64_le(&mut self, n: f64)
self in little-endian byte order. 更多信息Source§fn put_f64_ne(&mut self, n: f64)
fn put_f64_ne(&mut self, n: f64)
self in native-endian byte order. 更多信息