pub struct FixedCapacityElements<Element, const CAPACITY: usize>where
Element: TrivialType,{ /* private fields */ }Expand description
Container for storing a number of elements limited by the specified fixed capacity.
See also FixedCapacityBytes and FixedCapacityString.
In contrast to VariableElements, which can store arbitrary number of elements and can
change the capacity, this container has fixed predefined capacity and occupies it regardless of
how many elements are actually stored inside. This might seem limiting but allows implementing
TrivialType trait, enabling its use for fields in data structures that derive
TrivialType themselves, which isn’t the case with VariableElements.
The length (the number of stored elements) is a little-endian u32 in the first 4 bytes,
followed by zero bytes up to TrivialType::ALIGNMENT of elements if it is larger than 4
bytes, and CAPACITY elements after that. As a result, the container has the same alignment as
elements and no padding, regardless of the alignment of elements.
CAPACITY must not exceed u32::MAX, and the size of the container must be smaller than
2^32.
Parsing rejects a length above CAPACITY, but doesn’t check the rest of the length prefix,
which is set to zero bytes whenever the length is set. The unsafe methods of TrivialType
that skip parsing can create an instance with a larger length: methods don’t panic then, but
such an instance has no accessible elements and nothing can be appended to it until a valid
length is set with Self::truncate() or Self::copy_from().
Elements beyond the length are not part of the value. Default and Self::try_from_slice()
fill them with default elements, while Self::truncate() and Self::copy_from() leave
them as they are, so bytes of instances with the same elements can differ. Parsing checks all
CAPACITY elements, they are values of Element too.
Implementations§
Source§impl<Element, const CAPACITY: usize> FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> FixedCapacityElements<Element, CAPACITY>
Sourcepub fn try_from_slice(elements: &[Element]) -> Option<Self>where
Element: Default,
pub fn try_from_slice(elements: &[Element]) -> Option<Self>where
Element: Default,
Try to create an instance from provided elements.
Unused capacity is filled with default elements.
Returns None if provided elements do not fit into the capacity.
Sourcepub fn get_elements(&self) -> &[Element]
pub fn get_elements(&self) -> &[Element]
Access to stored elements.
Returns no elements if the number of elements exceeds CAPACITY, which is only possible for
an instance created from invalid bytes.
Sourcepub fn get_elements_mut(&mut self) -> &mut [Element]
pub fn get_elements_mut(&mut self) -> &mut [Element]
Exclusive access to stored elements.
Returns no elements if the number of elements exceeds CAPACITY, which is only possible for
an instance created from invalid bytes.
Sourcepub const fn len(&self) -> u32
pub const fn len(&self) -> u32
Number of stored elements.
May exceed CAPACITY for an instance created from invalid bytes.
Sourcepub fn append(&mut self, elements: &[Element]) -> bool
pub fn append(&mut self, elements: &[Element]) -> bool
Append some elements.
true is returned on success, but if there isn’t enough capacity left, false is.
Sourcepub const fn truncate(&mut self, new_len: u32) -> bool
pub const fn truncate(&mut self, new_len: u32) -> bool
Truncate stored elements to this length.
Returns true on success or false if new_len is larger than Self::len().
Trait Implementations§
Source§impl<Element, const CAPACITY: usize> Clone for FixedCapacityElements<Element, CAPACITY>where
Element: TrivialType + Clone,
impl<Element, const CAPACITY: usize> Clone for FixedCapacityElements<Element, CAPACITY>where
Element: TrivialType + Clone,
Source§fn clone(&self) -> FixedCapacityElements<Element, CAPACITY>
fn clone(&self) -> FixedCapacityElements<Element, CAPACITY>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<Element, const CAPACITY: usize> Copy for FixedCapacityElements<Element, CAPACITY>where
Element: TrivialType + Copy,
Source§impl<Element, const CAPACITY: usize> Debug for FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> Debug for FixedCapacityElements<Element, CAPACITY>
Source§impl<Element, const CAPACITY: usize> Default for FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> Default for FixedCapacityElements<Element, CAPACITY>
Source§impl<Element, const CAPACITY: usize> TrivialType for FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> TrivialType for FixedCapacityElements<Element, CAPACITY>
Source§const METADATA: &[u8]
const METADATA: &[u8]
IoTypeMetadataKind for encoding details.Source§fn try_from_bytes_unaligned(bytes: &[u8]) -> Option<(&Unaligned<Self>, &[u8])>
fn try_from_bytes_unaligned(bytes: &[u8]) -> Option<(&Unaligned<Self>, &[u8])>
bytes, which don’t need to be aligned. Read moreconst SIZE: u32 = _
Source§final fn try_from_bytes(bytes: &[u8]) -> Option<(&Self, &[u8])>
final fn try_from_bytes(bytes: &[u8]) -> Option<(&Self, &[u8])>
bytes, returns the value and the remaining bytes. Read moreSource§final fn try_from_bytes_exact(bytes: &[u8]) -> Option<&Self>
final fn try_from_bytes_exact(bytes: &[u8]) -> Option<&Self>
Self::try_from_bytes(), but returns None if there are more bytes than the valueSource§final fn try_from_bytes_mut(bytes: &mut [u8]) -> Option<(&mut Self, &mut [u8])>
final fn try_from_bytes_mut(bytes: &mut [u8]) -> Option<(&mut Self, &mut [u8])>
Self::try_from_bytes(), but returns an exclusive reference. Read moreSource§final fn try_read_unaligned(bytes: &[u8]) -> Option<(Self, &[u8])>
final fn try_read_unaligned(bytes: &[u8]) -> Option<(Self, &[u8])>
bytes, which don’t need to be aligned, returns the value
and the remaining bytesSource§final fn try_read_unaligned_exact(bytes: &[u8]) -> Option<Self>
final fn try_read_unaligned_exact(bytes: &[u8]) -> Option<Self>
Self::try_read_unaligned(), but returns None if there are more bytes than the
valueSource§final unsafe fn read_unaligned(bytes: &[u8]) -> Option<Self>
final unsafe fn read_unaligned(bytes: &[u8]) -> Option<Self>
Source§final unsafe fn read_unaligned_unchecked(bytes: &[u8]) -> Self
final unsafe fn read_unaligned_unchecked(bytes: &[u8]) -> Self
Self::read_unaligned(), but doesn’t do any checks at all. Read moreSource§final unsafe fn from_bytes(bytes: &[u8]) -> Option<&Self>
final unsafe fn from_bytes(bytes: &[u8]) -> Option<&Self>
Source§final unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
final unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
Self::from_bytes(), but doesn’t do any checks at all. Read moreSource§final unsafe fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
final unsafe fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
Source§final unsafe fn from_bytes_mut_unchecked(bytes: &mut [u8]) -> &mut Self
final unsafe fn from_bytes_mut_unchecked(bytes: &mut [u8]) -> &mut Self
Self::from_bytes_mut(), but doesn’t do any checks at all. Read moreAuto Trait Implementations§
impl<Element, const CAPACITY: usize> Freeze for FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> RefUnwindSafe for FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> Send for FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> Sync for FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> Unpin for FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> UnsafeUnpin for FixedCapacityElements<Element, CAPACITY>
impl<Element, const CAPACITY: usize> UnwindSafe for FixedCapacityElements<Element, CAPACITY>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IoType for Twhere
T: TrivialType,
impl<T> IoType for Twhere
T: TrivialType,
Source§const METADATA: &'static [u8] = const METADATA: &'_ [u8] = T::METADATA;
const METADATA: &'static [u8] = const METADATA: &'_ [u8] = T::METADATA;
IoTypeMetadataKind for encoding detailsSource§type PointerType = T
type PointerType = T
IoType representsSource§unsafe fn from_ptr<'a>(
ptr: &'a NonNull<<T as IoType>::PointerType>,
size: &'a u32,
capacity: u32,
) -> impl Deref<Target = T> + 'a
unsafe fn from_ptr<'a>( ptr: &'a NonNull<<T as IoType>::PointerType>, size: &'a u32, capacity: u32, ) -> impl Deref<Target = T> + 'a
Source§unsafe fn from_mut_ptr<'a>(
ptr: &'a mut NonNull<<T as IoType>::PointerType>,
_size: &'a mut u32,
capacity: u32,
) -> impl DerefMut + 'a
unsafe fn from_mut_ptr<'a>( ptr: &'a mut NonNull<<T as IoType>::PointerType>, _size: &'a mut u32, capacity: u32, ) -> impl DerefMut + 'a
Source§unsafe fn try_from_ptr<'a>(
ptr: &'a NonNull<<T as IoType>::PointerType>,
size: &'a u32,
capacity: u32,
) -> Option<impl Deref<Target = T> + 'a>
unsafe fn try_from_ptr<'a>( ptr: &'a NonNull<<T as IoType>::PointerType>, size: &'a u32, capacity: u32, ) -> Option<impl Deref<Target = T> + 'a>
Source§unsafe fn try_from_mut_ptr<'a>(
ptr: &'a mut NonNull<<T as IoType>::PointerType>,
size: &'a mut u32,
capacity: u32,
) -> Option<impl DerefMut + 'a>
unsafe fn try_from_mut_ptr<'a>( ptr: &'a mut NonNull<<T as IoType>::PointerType>, size: &'a mut u32, capacity: u32, ) -> Option<impl DerefMut + 'a>
Self::try_from_ptr(), but returns an exclusive reference. Read more