pub struct ConstVec<T, const MAX_SIZE: usize = DEFAULT_MAX_SIZE> { /* private fields */ }Expand description
A version of Vec that is usable in const contexts.
ConstVec has a fixed maximum size, but it can grow and shrink within that
size limit as needed.
§Example
const VEC: ConstVec<u8> = {
let mut vec = ConstVec::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);
assert!(vec.pop().unwrap() == 4);
vec
};
assert_eq!(VEC.as_ref(), &[1, 2, 3]);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 ONE: ConstVec<u8> = {
let mut vec = ConstVec::new();
vec.push(1);
vec
};
assert_eq!(ONE.get(0), Some(&1));Sourcepub const fn at(&self, index: usize) -> Twhere
T: Copy,
pub const fn at(&self, index: usize) -> Twhere
T: Copy,
Get a copy of the value at the given index.
This panics if index is out of bounds.
Sourcepub const fn as_ref(&self) -> &[T]
pub const fn as_ref(&self) -> &[T]
Get a reference to the underlying slice.
§Example
const ONE: ConstVec<u8> = {
let mut vec = ConstVec::new();
vec.push(1);
vec
};
assert_eq!(ONE.as_ref(), &[1]);Sourcepub const fn swap(&mut self, first: usize, second: usize)where
T: Copy,
pub const fn swap(&mut self, first: usize, second: usize)where
T: Copy,
Swap the values at the given indices.
§Example
const THREE: ConstVec<u8> = {
let mut vec = ConstVec::new();
vec.push(1);
vec.push(2);
vec.swap(0, 1);
vec
};
assert_eq!(THREE.as_ref(), &[2, 1]);Sourcepub const fn remove(&mut self, index: usize) -> Option<T>where
T: Copy,
pub const fn remove(&mut self, index: usize) -> Option<T>where
T: Copy,
Remove the value at the given index.
§Example
const REMOVED: (ConstVec<u8>, Option<u8>) = {
let mut vec = ConstVec::new();
vec.push(1);
vec.push(2);
vec.push(3);
let removed = vec.remove(1);
(vec, removed)
};
assert_eq!(REMOVED.0.as_ref(), &[1, 3]);
assert_eq!(REMOVED.1.unwrap(), 2);Sourcepub const fn set(&mut self, index: usize, value: T)
pub const fn set(&mut self, index: usize, value: T)
Set the value at the given index.
§Example
const TWO: ConstVec<u8> = {
let mut vec = ConstVec::new();
vec.push(1);
vec.set(0, 2);
vec
};
assert_eq!(TWO.as_ref(), &[2]);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 TWO: ConstVec<u8> = {
let mut vec = ConstVec::new();
vec.push(1);
vec.push(2);
vec
};
const READ: ConstReadBuffer = TWO.read();
assert_eq!(READ.as_ref(), &[1, 2]);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>
impl<T, const MAX_SIZE: usize> RefUnwindSafe for ConstVec<T, MAX_SIZE>
impl<T, const MAX_SIZE: usize> Send for ConstVec<T, MAX_SIZE>
impl<T, const MAX_SIZE: usize> Sync for ConstVec<T, MAX_SIZE>
impl<T, const MAX_SIZE: usize> Unpin for ConstVec<T, MAX_SIZE>
impl<T, const MAX_SIZE: usize> UnsafeUnpin for ConstVec<T, MAX_SIZE>
impl<T, const MAX_SIZE: usize> UnwindSafe for ConstVec<T, MAX_SIZE>
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