headervec

Struct HeaderVec

Source
pub struct HeaderVec<T, U, const N: usize> { /* private fields */ }
Expand description

Implements a Vec-like type for building structures with a fixed-sized prefix before a dynamic number of elements.

To avoid allocations in common cases, the header and elements are stored internally without allocating until the element count would exceed the statically determined capacity.

Only a small portion of the Vec interface is supported. Additional methods can be added as needed.

The data managed by this type must be Copy. This simplifies the resource management and should be sufficient for most use cases.

§Example

#[derive(Copy, Clone)]
struct Header { x: u32 }
let mut v = HeaderVec::<Header, u8, 10>::new(Header{ x: 1234 });
v.push_tail(5);
v.push_tail(6);
assert_eq!(v.head.x, 1234);
assert_eq!(&v.tail, &[5, 6]);

Implementations§

Source§

impl<T: Copy, U: Copy, const N: usize> HeaderVec<T, U, N>

Source

pub fn new(head: T) -> Self

Constructs a new HeaderVec with a header of head and no tail elements.

Source

pub fn with_capacity(head: T, cap: usize) -> Self

Constructs a new HeaderVec with a header of head and no tail elements, but with a dynamically allocated capacity for cap elements.

Source

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

Reserves capacity for at least n additional tail elements.

Source

pub fn spare_tail_capacity_mut(&mut self) -> &mut [MaybeUninit<U>]

Returns the remaining spare capacity of the tail as a slice of MaybeUninit<U>.

The returned slice can be used to fill the tail with data before marking the data as initialized using Self::set_tail_len.

Source

pub fn push_tail(&mut self, val: U)

Pushes a tail element, reallocating if necessary.

Source

pub fn extend_tail_from_slice(&mut self, other: &[U])

Extends the tail elements from the given slice.

Source

pub fn as_ptr(&self) -> *const T

Retrieves a pointer to the head. The tail is guaranteed to immediately after the head (with appropriate padding).

Source

pub fn as_mut_ptr(&mut self) -> *mut T

Retrieves a mutable pointer to the head. The tail is guaranteed to immediately after the head (with appropriate padding).

Source

pub fn tail_capacity(&self) -> usize

Returns the number of tail elements that can be stored without reallocating.

Source

pub fn clear_tail(&mut self)

Sets the number of tail elements to 0.

Source

pub fn truncate_tail(&mut self, len: usize)

Truncates the tail to len elements. Has no effect if there are already fewer than len tail elements.

Source

pub unsafe fn set_tail_len(&mut self, len: usize)

Sets the number of tail elements.

Panics if len is greater than the capacity.

§Safety

The caller must ensure that all len elements have been initialized.

Source

pub fn total_byte_len(&self) -> usize

Returns the total contiguous byte length of the structure, including both the head and tail elements.

Source

pub fn total_byte_capacity(&self) -> usize

Returns the total contiguous byte length of the structure, including both the head and tail elements, including the tail’s capacity.

Trait Implementations§

Source§

impl<T: Debug, U: Debug, const N: usize> Debug for HeaderVec<T, U, N>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Copy + Default, U: Copy, const N: usize> Default for HeaderVec<T, U, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, U, const N: usize> Deref for HeaderVec<T, U, N>

Source§

type Target = HeaderSlice<T, [U]>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T, U, const N: usize> DerefMut for HeaderVec<T, U, N>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: Copy, U: Copy, const N: usize> Extend<U> for HeaderVec<T, U, N>

Source§

fn extend<I: IntoIterator<Item = U>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations§

§

impl<T, U, const N: usize> Freeze for HeaderVec<T, U, N>
where T: Freeze, U: Freeze,

§

impl<T, U, const N: usize> RefUnwindSafe for HeaderVec<T, U, N>

§

impl<T, U, const N: usize> Send for HeaderVec<T, U, N>
where T: Send, U: Send,

§

impl<T, U, const N: usize> Sync for HeaderVec<T, U, N>
where T: Sync, U: Sync,

§

impl<T, U, const N: usize> Unpin for HeaderVec<T, U, N>
where T: Unpin, U: Unpin,

§

impl<T, U, const N: usize> UnwindSafe for HeaderVec<T, U, N>
where T: UnwindSafe, U: UnwindSafe,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.