Skip to main content

PrefixArena

Struct PrefixArena 

Source
pub struct PrefixArena<'buf> { /* private fields */ }
Expand description

A bump-style arena over caller-provided byte storage.

PrefixArena hands out slices from the front of the remaining buffer and keeps the rest available for later use. It only manages buffer boundaries; it does not track which bytes are initialized.

Implementations§

Source§

impl<'buf> PrefixArena<'buf>

Source

pub const fn new(arena: &'buf mut [u8]) -> Self

Source

pub const fn from_uninit(arena: &'buf mut [MaybeUninit<u8>]) -> Self

Source

pub const fn len(&self) -> usize

Returns the number of bytes still available in the arena.

Source

pub const fn is_empty(&self) -> bool

Returns true when no bytes remain available.

Source

pub const fn view<'arena>(&'arena mut self) -> ArenaView<'arena, 'buf>

Borrows the currently remaining arena space through a temporary view.

Source

pub fn take_prefix(&self, n: usize) -> &'buf mut [MaybeUninit<u8>]

Removes the first n bytes from the remaining arena and returns them.

The returned slice is uninitialized storage. The caller must initialize it before reading from it.

§Panics

Panics if n > self.len().

Source

pub unsafe fn take_prefix_unchecked(&self, n: usize) -> &'buf mut [u8]

Removes the first n bytes from the remaining arena and returns them as u8.

§Safety

The caller must ensure both of the following:

  • n <= self.len().
  • Every returned byte is initialized before it is read as u8.
Source

pub fn take_remaining(self) -> &'buf mut [MaybeUninit<u8>]

Returns all bytes that still remain in the arena and consumes self.

The returned slice is uninitialized storage. The caller must initialize it before reading from it.

Source

pub fn init_prefix_with<F, E>(self, f: F) -> Result<&'buf mut [u8], E>
where F: FnOnce(&mut [u8]) -> Result<usize, E>,

Exposes the remaining arena as &mut [u8], lets f initialize a prefix, and then detaches that initialized prefix from the arena.

If f returns Err, the arena remains unchanged.

§Safety

f must return the length of a prefix that it actually initialized.

§Panics

Panics if f returns a length greater than the currently remaining size.

Trait Implementations§

Source§

impl<'buf> From<&'buf mut [MaybeUninit<u8>]> for PrefixArena<'buf>

Source§

fn from(buffer: &'buf mut [MaybeUninit<u8>]) -> Self

Converts to this type from the input type.
Source§

impl<'buf, const N: usize> From<&'buf mut [MaybeUninit<u8>; N]> for PrefixArena<'buf>

Source§

fn from(buffer: &'buf mut [MaybeUninit<u8>; N]) -> Self

Converts to this type from the input type.
Source§

impl<'buf> From<&'buf mut [u8]> for PrefixArena<'buf>

Source§

fn from(buffer: &'buf mut [u8]) -> Self

Converts to this type from the input type.
Source§

impl<'buf, const N: usize> From<&'buf mut [u8; N]> for PrefixArena<'buf>

Source§

fn from(buffer: &'buf mut [u8; N]) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'buf> !Freeze for PrefixArena<'buf>

§

impl<'buf> !RefUnwindSafe for PrefixArena<'buf>

§

impl<'buf> Send for PrefixArena<'buf>

§

impl<'buf> !Sync for PrefixArena<'buf>

§

impl<'buf> Unpin for PrefixArena<'buf>

§

impl<'buf> UnsafeUnpin for PrefixArena<'buf>

§

impl<'buf> !UnwindSafe for PrefixArena<'buf>

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.