pub struct ConstVec<T, const MAX_SIZE: usize = DEFAULT_MAX_SIZE> { /* private fields */ }
Expand description
ConstVec
is a version of Vec
that is usable in const contexts. It has
a fixed maximum size, but it can can grow and shrink within that size limit
as needed.
§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
// Methods that mutate the vector will return a new vector
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.push(3);
const FOUR: ConstVec<u8> = THREE.push(4);
// If a value is also returned, that will be placed in a tuple in the return value
// along with the new vector
const POPPED: (ConstVec<u8>, Option<u8>) = FOUR.pop();
assert_eq!(POPPED.0, THREE);
assert_eq!(POPPED.1.unwrap(), 4);
Implementations§
Source§impl<T, const MAX_SIZE: usize> ConstVec<T, MAX_SIZE>
impl<T, const MAX_SIZE: usize> ConstVec<T, MAX_SIZE>
Sourcepub const fn new_with_max_size() -> Self
pub const fn new_with_max_size() -> Self
Sourcepub const fn get(&self, index: usize) -> Option<&T>
pub const fn get(&self, index: usize) -> Option<&T>
Get a reference to the value at the given index
§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
assert_eq!(ONE.get(0), Some(&1));
Sourcepub const fn as_ref(&self) -> &[T]
pub const fn as_ref(&self) -> &[T]
Get a reference to the underlying slice
§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
assert_eq!(ONE.as_ref(), &[1]);
Sourcepub const fn swap(self, first: usize, second: usize) -> Selfwhere
T: Copy,
pub const fn swap(self, first: usize, second: usize) -> Selfwhere
T: Copy,
Swap the values at the given indices
§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.swap(0, 1);
assert_eq!(THREE.as_ref(), &[2, 1]);
Sourcepub const fn pop(self) -> (Self, Option<T>)where
T: Copy,
pub const fn pop(self) -> (Self, Option<T>)where
T: Copy,
Pop a value off the end of the ConstVec
§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.push(3);
const POPPED: (ConstVec<u8>, Option<u8>) = THREE.pop();
assert_eq!(POPPED.0, TWO);
assert_eq!(POPPED.1.unwrap(), 3);
Sourcepub const fn remove(self, index: usize) -> (Self, Option<T>)where
T: Copy,
pub const fn remove(self, index: usize) -> (Self, Option<T>)where
T: Copy,
Remove the value at the given index
§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.push(3);
const REMOVED: (ConstVec<u8>, Option<u8>) = THREE.remove(1);
assert_eq!(REMOVED.0.as_ref(), &[1, 3]);
assert_eq!(REMOVED.1.unwrap(), 2);
Sourcepub const fn set(self, index: usize, value: T) -> Self
pub const fn set(self, index: usize, value: T) -> Self
Set the value at the given index
§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.set(0, 2);
assert_eq!(TWO.as_ref(), &[2]);
Sourcepub const fn split_at(&self, index: usize) -> (Self, Self)where
T: Copy,
pub const fn split_at(&self, index: usize) -> (Self, Self)where
T: Copy,
Split the ConstVec
into two at the given index
§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const THREE: ConstVec<u8> = TWO.push(3);
const SPLIT: (ConstVec<u8>, ConstVec<u8>) = THREE.split_at(1);
assert_eq!(SPLIT.0.as_ref(), &[1]);
assert_eq!(SPLIT.1.as_ref(), &[2, 3]);
Source§impl<const MAX_SIZE: usize> ConstVec<u8, MAX_SIZE>
impl<const MAX_SIZE: usize> ConstVec<u8, MAX_SIZE>
Sourcepub const fn read(&self) -> ConstReadBuffer<'_>
pub const fn read(&self) -> ConstReadBuffer<'_>
Convert the ConstVec
into a ConstReadBuffer
§Example
const EMPTY: ConstVec<u8> = ConstVec::new();
const ONE: ConstVec<u8> = EMPTY.push(1);
const TWO: ConstVec<u8> = ONE.push(2);
const READ: ConstReadBuffer = TWO.read();
Trait Implementations§
impl<T: Copy, const MAX_SIZE: usize> Copy for ConstVec<T, MAX_SIZE>
Auto Trait Implementations§
impl<T, const MAX_SIZE: usize> Freeze for ConstVec<T, MAX_SIZE>where
T: Freeze,
impl<T, const MAX_SIZE: usize> RefUnwindSafe for ConstVec<T, MAX_SIZE>where
T: RefUnwindSafe,
impl<T, const MAX_SIZE: usize> Send for ConstVec<T, MAX_SIZE>where
T: Send,
impl<T, const MAX_SIZE: usize> Sync for ConstVec<T, MAX_SIZE>where
T: Sync,
impl<T, const MAX_SIZE: usize> Unpin for ConstVec<T, MAX_SIZE>where
T: Unpin,
impl<T, const MAX_SIZE: usize> UnwindSafe for ConstVec<T, MAX_SIZE>where
T: UnwindSafe,
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
Mutably borrows from an owned value. Read more