ReadBuf

Struct ReadBuf 

Source
pub struct ReadBuf { /* private fields */ }
Expand description

A wrapper around a byte buffer that is incrementally filled and initialized.

This type is a sort of “double cursor”. It tracks three regions in the buffer: a region at the beginning of the buffer that has been logically filled with data, a region that has been initialized at some point but not yet logically filled, and a region at the end that may be uninitialized. The filled region is guaranteed to be a subset of the initialized region.

In summary, the contents of the buffer can be visualized as:

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

It is undefined behavior to de-initialize any bytes from the uninitialized region, since it is merely unknown whether this region is uninitialized or not, and if part of it turns out to be initialized, it must stay initialized.

Implementations§

Source§

impl ReadBuf

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new ReadBuf with the specified capacity.

The returned ReadBuf will be able to hold at least capacity bytes without reallocating.

Source

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

Returns a mutable slice starting at the current BufMut position and of length between 0 and ReadBuf::remaining_mut(). Note that this can be shorter than the whole remainder of the buffer (this allows non-continuous implementation).

Source

pub 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.

This value is greater than or equal to the length of the slice returned by chunk_mut().

Source

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

Advance the internal cursor of the BufMut

The next call to chunk_mut will return a slice starting cnt bytes further into the underlying buffer.

Source

pub fn into_bytes_mut(self, advance: Option<usize>) -> BytesMut

Consume ReadBuf and convert into BytesMut

Source

pub fn into_bytes(self, advance: Option<usize>) -> Bytes

Consume ReadBuf and convert into Bytes

Source

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

Returns a slice starting at the current position and of length between 0 and ReadBuf::remaining(). Note that this can return shorter slice (this allows non-continuous internal representation).

Source

pub fn remaining(&self) -> usize

Returns the number of bytes between the current position and the end of the buffer.

This value is greater than or equal to the length of the slice returned by chunk().

Source

pub fn split_to(&mut self, at: usize) -> BytesMut

Splits the buffer into two at the given index.

Afterwards self contains elements [at, len), and the returned BytesMut contains elements [0, at).

This is an O(1) operation that just increases the reference count and sets a few indices.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V