跳到主要内容

ReadBuf

搜索

结构体 ReadBuf 

Source
pub struct ReadBuf<'a> { /* private fields */ }
展开描述

对字节缓冲区的包装,以增量方式填充和初始化。

此类型 可视为“双游标”。 它在缓冲区中跟踪三个区域: 缓冲区开头 已逻辑填充数据的区域、 某时已初始化但 尚未逻辑填充的区域, 以及末尾 可能未初始化的区域。 填充区域保证是初始化区域的子集。

概括而言,缓冲区的内容可以可视化如下:

[             capacity              ]
[ filled |         unfilled         ]
[    initialized    | uninitialized ]

对未初始化区域中的任何字节执行反初始化是未定义行为, 因为仅仅是不清楚该区域是否已初始化; 如果其中某些部分实际上是已初始化的,那么它必须保持已初始化状态。

实现§

Source§

impl<'a> ReadBuf<'a>

Source

pub fn new(buf: &'a mut [u8]) -> ReadBuf<'a>

从一个完全初始化的缓冲区创建一个新的 ReadBuf

Source

pub fn uninit(buf: &'a mut [MaybeUninit<u8>]) -> ReadBuf<'a>

从一个可能未初始化的缓冲区创建一个新的 ReadBuf

内部光标会将整个缓冲区标记为未初始化。 如果已知缓冲区是部分初始化的,那么可以使用 assume_init 来移动内部光标。

Source

pub fn capacity(&self) -> usize

返回缓冲区的总容量。

Source

pub fn filled(&self) -> &[u8]

返回对缓冲区已填充部分的共享引用。

Source

pub fn filled_mut(&mut self) -> &mut [u8]

返回对缓冲区已填充部分的可变引用。

Source

pub fn take(&mut self, n: usize) -> ReadBuf<'_>

返回一个由至多 n 个未填充字节组成的新 ReadBuf

Source

pub fn initialized(&self) -> &[u8]

返回对缓冲区已初始化部分的共享引用。

其中包括已填充的部分。

Source

pub fn initialized_mut(&mut self) -> &mut [u8]

返回对缓冲区已初始化部分的可变引用。

其中包括已填充的部分。

Source

pub unsafe fn inner_mut(&mut self) -> &mut [MaybeUninit<u8>]

返回对整个缓冲区的可变引用,不保证它已被完全初始化。

下标在 0 到 self.filled().len() 之间的元素已被填充, 0 到 self.initialized().len() 之间的元素已被初始化(因此可以转换为 &mut [u8])。

此方法的调用方必须确保这些不变量得到遵守。 例如,如果调用方对缓冲区中部分未初始化部分执行了初始化, 则必须使用已初始化的字节数调用 assume_init

§Safety

调用方不得对已经初始化的缓冲区部分执行反初始化。 其中也包括被 ReadBuf 标记为未初始化区域中的任何字节。

Source

pub unsafe fn unfilled_mut(&mut self) -> &mut [MaybeUninit<u8>]

返回对缓冲区未填充部分的可变引用,不保证它已被完全初始化。

§Safety

调用方不得对已经初始化的缓冲区部分执行反初始化。 其中也包括被 ReadBuf 标记为未初始化区域中的任何字节。

Source

pub fn initialize_unfilled(&mut self) -> &mut [u8]

返回对缓冲区未填充部分的可变引用,确保它已被完全初始化。

由于 ReadBuf 会跟踪缓冲区中已被初始化的区域,因此首次使用之后这实际上是“免费的”。

Source

pub fn initialize_unfilled_to(&mut self, n: usize) -> &mut [u8]

返回对缓冲区未填充部分前 n 个字节的可变引用,确保它们已被完全初始化。

§Panics

如果 self.remaining() 小于 n,则 panic。

Source

pub fn remaining(&self) -> usize

返回切片末尾尚未填充的字节数。

Source

pub fn clear(&mut self)

清空缓冲区,将已填充区域重置为空。

已初始化的字节数保持不变,缓冲区的内容也不会被修改。

Source

pub fn advance(&mut self, n: usize)

推进缓冲区已填充区域的大小。

已初始化的字节数不会改变。

§Panics

如果缓冲区已填充区域变得大于已初始化区域,则 panic。

Source

pub fn set_filled(&mut self, n: usize)

设置缓冲区已填充区域的大小。

已初始化的字节数不会改变。

请注意,除了扩大已填充区域之外, 此方法也可以用来缩小已填充区域(例如,由 AsyncRead 实现原地压缩数据时)。

§Panics

如果缓冲区已填充区域变得大于已初始化区域,则 panic。

Source

pub unsafe fn assume_init(&mut self, n: usize)

断言缓冲区中前 n 个未填充字节已被初始化。

ReadBuf 假定字节永远不会被反初始化, 因此当使用比已知已初始化字节数更少的字节调用此方法时,它不会执行任何操作。

§Safety

调用方必须确保缓冲区中 n 个未填充字节已被初始化。

Source

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

将数据追加到缓冲区,推进已写入位置,并可能也会推进已初始化位置。

§Panics

如果 self.remaining() 小于 buf.len(),则 panic。

Trait 实现§

Source§

impl<'a> BufMut for ReadBuf<'a>

Available on crate feature io-util only.
Source§

fn remaining_mut(&self) -> usize

Returns the number of bytes that can be written from the current position until the end of the buffer is reached. 更多信息
Source§

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

Advance the internal cursor of the BufMut 更多信息
Source§

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

Returns a mutable slice starting at the current BufMut position and of length between 0 and 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

Returns true if there is space in self for more bytes. 更多信息
Source§

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

Transfer bytes into self from src and advance the cursor by the number of bytes written. 更多信息
Source§

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

Transfer bytes into self from src and advance the cursor by the number of bytes written. 更多信息
Source§

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

Put cnt bytes val into self. 更多信息
Source§

fn put_u8(&mut self, n: u8)

Writes an unsigned 8 bit integer to self. 更多信息
Source§

fn put_i8(&mut self, n: i8)

Writes a signed 8 bit integer to self. 更多信息
Source§

fn put_u16(&mut self, n: u16)

Writes an unsigned 16 bit integer to self in big-endian byte order. 更多信息
Source§

fn put_u16_le(&mut self, n: u16)

Writes an unsigned 16 bit integer to self in little-endian byte order. 更多信息
Source§

fn put_u16_ne(&mut self, n: u16)

Writes an unsigned 16 bit integer to self in native-endian byte order. 更多信息
Source§

fn put_i16(&mut self, n: i16)

Writes a signed 16 bit integer to self in big-endian byte order. 更多信息
Source§

fn put_i16_le(&mut self, n: i16)

Writes a signed 16 bit integer to self in little-endian byte order. 更多信息
Source§

fn put_i16_ne(&mut self, n: i16)

Writes a signed 16 bit integer to self in native-endian byte order. 更多信息
Source§

fn put_u32(&mut self, n: u32)

Writes an unsigned 32 bit integer to self in big-endian byte order. 更多信息
Source§

fn put_u32_le(&mut self, n: u32)

Writes an unsigned 32 bit integer to self in little-endian byte order. 更多信息
Source§

fn put_u32_ne(&mut self, n: u32)

Writes an unsigned 32 bit integer to self in native-endian byte order. 更多信息
Source§

fn put_i32(&mut self, n: i32)

Writes a signed 32 bit integer to self in big-endian byte order. 更多信息
Source§

fn put_i32_le(&mut self, n: i32)

Writes a signed 32 bit integer to self in little-endian byte order. 更多信息
Source§

fn put_i32_ne(&mut self, n: i32)

Writes a signed 32 bit integer to self in native-endian byte order. 更多信息
Source§

fn put_u64(&mut self, n: u64)

Writes an unsigned 64 bit integer to self in the big-endian byte order. 更多信息
Source§

fn put_u64_le(&mut self, n: u64)

Writes an unsigned 64 bit integer to self in little-endian byte order. 更多信息
Source§

fn put_u64_ne(&mut self, n: u64)

Writes an unsigned 64 bit integer to self in native-endian byte order. 更多信息
Source§

fn put_i64(&mut self, n: i64)

Writes a signed 64 bit integer to self in the big-endian byte order. 更多信息
Source§

fn put_i64_le(&mut self, n: i64)

Writes a signed 64 bit integer to self in little-endian byte order. 更多信息
Source§

fn put_i64_ne(&mut self, n: i64)

Writes a signed 64 bit integer to self in native-endian byte order. 更多信息
Source§

fn put_u128(&mut self, n: u128)

Writes an unsigned 128 bit integer to self in the big-endian byte order. 更多信息
Source§

fn put_u128_le(&mut self, n: u128)

Writes an unsigned 128 bit integer to self in little-endian byte order. 更多信息
Source§

fn put_u128_ne(&mut self, n: u128)

Writes an unsigned 128 bit integer to self in native-endian byte order. 更多信息
Source§

fn put_i128(&mut self, n: i128)

Writes a signed 128 bit integer to self in the big-endian byte order. 更多信息
Source§

fn put_i128_le(&mut self, n: i128)

Writes a signed 128 bit integer to self in little-endian byte order. 更多信息
Source§

fn put_i128_ne(&mut self, n: i128)

Writes a signed 128 bit integer to self in native-endian byte order. 更多信息
Source§

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

Writes an unsigned n-byte integer to self in big-endian byte order. 更多信息
Source§

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

Writes an unsigned n-byte integer to self in the little-endian byte order. 更多信息
Source§

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

Writes an unsigned n-byte integer to self in the native-endian byte order. 更多信息
Source§

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

Writes low nbytes of a signed integer to self in big-endian byte order. 更多信息
Source§

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

Writes low nbytes of a signed integer to self in little-endian byte order. 更多信息
Source§

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

Writes low nbytes of a signed integer to self in native-endian byte order. 更多信息
Source§

fn put_f32(&mut self, n: f32)

Writes an IEEE754 single-precision (4 bytes) floating point number to self in big-endian byte order. 更多信息
Source§

fn put_f32_le(&mut self, n: f32)

Writes an IEEE754 single-precision (4 bytes) floating point number to self in little-endian byte order. 更多信息
Source§

fn put_f32_ne(&mut self, n: f32)

Writes an IEEE754 single-precision (4 bytes) floating point number to self in native-endian byte order. 更多信息
Source§

fn put_f64(&mut self, n: f64)

Writes an IEEE754 double-precision (8 bytes) floating point number to self in big-endian byte order. 更多信息
Source§

fn put_f64_le(&mut self, n: f64)

Writes an IEEE754 double-precision (8 bytes) floating point number to self in little-endian byte order. 更多信息
Source§

fn put_f64_ne(&mut self, n: f64)

Writes an IEEE754 double-precision (8 bytes) floating point number to self in native-endian byte order. 更多信息
Source§

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

Creates an adaptor which can write at most limit bytes to self. 更多信息
Source§

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

Creates an adaptor which implements the Write trait for self. 更多信息
Source§

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

Creates an adapter which will chain this buffer with another. 更多信息
Source§

impl Debug for ReadBuf<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化器格式化此值。 更多信息

自动 Trait 实现§

§

impl<'a> Freeze for ReadBuf<'a>

§

impl<'a> RefUnwindSafe for ReadBuf<'a>

§

impl<'a> Send for ReadBuf<'a>

§

impl<'a> Sync for ReadBuf<'a>

§

impl<'a> Unpin for ReadBuf<'a>

§

impl<'a> UnsafeUnpin for ReadBuf<'a>

§

impl<'a> !UnwindSafe for ReadBuf<'a>

Blanket 实现§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换的具体行为取决于 From<T> for U 的实现方式。

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。