Struct OwnedBuf

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

An owned, fixed length buffer of bytes.

An OwnedBuf is created from an existing collection type, it is an abstract wrapper of sorts. See the module docs for details and examples.

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 is fully uninitialized. The filled region is guaranteed to be a subset of the initialized region. The filled part can grow or shrink, the initialized part can only grow, and capacity of the buffer (i.e., its total length) is fixed.

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

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

The API of OwnedBuf is focussed on creation and destruction. To read from the filled part, get a view of the buffer using an OwnedSlice. To write into the unfilled part, get a view of the buffer using and OwnedCursor. Both of these types can be converted back into an OwnedBuf.

Implementations§

Source§

impl OwnedBuf

Source

pub unsafe fn new( data: *mut MaybeUninit<u8>, dtor: unsafe fn(&mut OwnedBuf), user_data: *const (), capacity: usize, filled: usize, init: usize, ) -> OwnedBuf

Create a new OwnedBuf.

§Safety

See module docs for safety requirements.

Source

pub unsafe fn into_vec(self) -> Vec<u8>

Convert this buffer into a Vec of u8.

§Safety

It is only safe to use this method if the buffer was created from a Vec<u8>.

Source

pub unsafe fn into_maybe_uninit_vec(self) -> Vec<MaybeUninit<u8>>

Convert this buffer into a Vec of MaybeUninit<u8>.

§Safety

It is only safe to use this method if the buffer was created from a Vec<MaybeUninit<u8>>.

Source

pub fn capacity(&self) -> usize

Returns the total size of the buffer.

Source

pub fn filled_len(&self) -> usize

Returns the length of the filled part of the buffer.

Source

pub fn init_len(&self) -> usize

Returns the length of the initialized part of the buffer.

Source

pub fn filled(self) -> OwnedSlice

Returns an OwnedSlice covering the filled part of this buffer.

Source

pub fn filled_slice(self, range: impl UsizeRange) -> OwnedSlice

Returns an OwnedSlice covering a slice of the filled part of this buffer.

The supplied range must be within the filled part of the buffer.

§Panics

This function will panic if range is outside the bounds of the filled part of the buffer.

Source

pub fn unfilled(self) -> OwnedCursor

Returns a cursor over the unfilled part of the buffer.

Source

pub fn unfilled_slice(self, range: RangeTo<usize>) -> OwnedCursor

Returns a cursor over a slice of the unfilled part of the buffer.

The supplied range covers the whole buffer, not just the unfilled part. The upper bound of the range must be within the unfilled part.

§Examples

Where buf has a capacity (total length) of 16 bytes and the first 4 bytes are filled, buf.unfilled_slice(..8) will return a cursor over the first 4 unfilled bytes.

Source

pub fn clear(&mut self) -> &mut Self

Clears the buffer, resetting the filled region to empty.

The number of initialized bytes is not changed, and the contents of the buffer are not modified.

Source

pub unsafe fn set_init(&mut self, n: usize) -> &mut Self

Asserts that the first n bytes of the buffer are initialized.

OwnedBuf assumes that bytes are never de-initialized, so this method does nothing when called with fewer bytes than are already known to be initialized.

§Safety

The caller must ensure that the first n unfilled bytes of the buffer have already been initialized.

Source

pub fn into_raw_parts( self, ) -> (*mut MaybeUninit<u8>, unsafe fn(&mut OwnedBuf), *const (), usize, usize, usize)

Decomposes this OwnedBuf into its raw components.

Returns (data, dtor, filled, init, capacity) where data is a pointer to the buffer’s data in memory, dtor is the buffer’s destructor function, filled is the number of bytes which are filled with data, init is the number of bytes which have been initialised (i.e., which are safe to treat as u8 rather than MaybeUninit<u8>), and capacity is the total length of the buffer. filled <= init <= capacity.

§Safety

See module docs for safety requirements on the returned destructor function.

Trait Implementations§

Source§

impl Drop for OwnedBuf

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<Vec<MaybeUninit<u8>>> for OwnedBuf

Source§

fn from(v: Vec<MaybeUninit<u8>>) -> OwnedBuf

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for OwnedBuf

Source§

fn from(v: Vec<u8>) -> OwnedBuf

Converts to this type from the input type.
Source§

impl Into<OwnedCursor> for OwnedBuf

Source§

fn into(self) -> OwnedCursor

Converts this type into the (usually inferred) input type.
Source§

impl Into<OwnedSlice> for OwnedBuf

Source§

fn into(self) -> OwnedSlice

Converts this type into the (usually inferred) input type.

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