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>
impl<T: Copy, U: Copy, const N: usize> HeaderVec<T, U, N>
Sourcepub fn new(head: T) -> Self
pub fn new(head: T) -> Self
Constructs a new HeaderVec with a header of head and no tail
elements.
Sourcepub fn with_capacity(head: T, cap: usize) -> Self
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.
Sourcepub fn reserve_tail(&mut self, n: usize)
pub fn reserve_tail(&mut self, n: usize)
Reserves capacity for at least n additional tail elements.
Sourcepub fn spare_tail_capacity_mut(&mut self) -> &mut [MaybeUninit<U>]
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.
Sourcepub fn extend_tail_from_slice(&mut self, other: &[U])
pub fn extend_tail_from_slice(&mut self, other: &[U])
Extends the tail elements from the given slice.
Sourcepub fn as_ptr(&self) -> *const T
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).
Sourcepub fn as_mut_ptr(&mut self) -> *mut T
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).
Sourcepub fn tail_capacity(&self) -> usize
pub fn tail_capacity(&self) -> usize
Returns the number of tail elements that can be stored without reallocating.
Sourcepub fn clear_tail(&mut self)
pub fn clear_tail(&mut self)
Sets the number of tail elements to 0.
Sourcepub fn truncate_tail(&mut self, len: usize)
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.
Sourcepub unsafe fn set_tail_len(&mut self, len: usize)
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.
Sourcepub fn total_byte_len(&self) -> usize
pub fn total_byte_len(&self) -> usize
Returns the total contiguous byte length of the structure, including both the head and tail elements.
Sourcepub fn total_byte_capacity(&self) -> usize
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: Copy, U: Copy, const N: usize> Extend<U> for HeaderVec<T, U, N>
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)
fn extend<I: IntoIterator<Item = U>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)