pub struct HeaderVec<T, U: FixedArray> { /* 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(5);
v.push(6);
assert_eq!(v.x, 1234);
assert_eq!(&v[..], &[5, 6]);
Implementations§
source§impl<T: Copy, U: FixedArray> HeaderVec<T, U>
impl<T: Copy, U: FixedArray> HeaderVec<T, U>
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.
pub fn reserve(&mut self, n: usize)
sourcepub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<U::Element>]
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<U::Element>]
Returns the remaining spare capacity of the tail as a slice of
MaybeUninit<U::Element>
.
The returned slice can be used to fill the tail with data before marking the data as initialized using `Self::set_len.
sourcepub fn extend_from_slice(&mut self, other: &[U::Element])
pub fn extend_from_slice(&mut self, other: &[U::Element])
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 as_mut_slice(&mut self) -> &mut [U::Element]
pub fn as_mut_slice(&mut self) -> &mut [U::Element]
Returns a mutable slice of the tail elements.
pub fn capacity(&self) -> usize
sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&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_len(&mut self, len: usize)
pub unsafe fn set_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: FixedArray> Extend<<U as FixedArray>::Element> for HeaderVec<T, U>
impl<T: Copy, U: FixedArray> Extend<<U as FixedArray>::Element> for HeaderVec<T, U>
source§fn extend<I: IntoIterator<Item = U::Element>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = U::Element>>(&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
)