pub struct StackVec<'mem, T> { /* private fields */ }Expand description
A Vec-like implementation on a fixed-size buffer that grows upwards.
StackVec provides a familiar Vec-like interface for managing a contiguous
array, but it operates on a pre-allocated slice of memory instead of the heap.
It is a LIFO stack that grows from a low memory address towards a higher one.
This is useful for creating dynamic-length collections in environments where heap allocation is not available or desirable.
§Layout (high address at the top):
base (low addr)
│
▼
[ item1, item2, ... itemN, ... free space ... ]
▲
└─ len points here (one-past-end)§Example
use frame_mem_utils::stack::{StackVec, make_storage};
use core::mem::MaybeUninit;
let mut buffer: [MaybeUninit<u8>; 16] = make_storage();
let mut vec = StackVec::from_slice(&mut buffer);
vec.push(10u8).unwrap();
vec.push_slice(&[20, 30]).unwrap();
assert_eq!(vec.len(), 3);
assert_eq!(vec.peek_all(), &[10, 20, 30]);
assert_eq!(vec.pop(), Some(30));Implementations§
Source§impl<'mem, T> StackVec<'mem, T>
impl<'mem, T> StackVec<'mem, T>
Sourcepub const fn from_slice(buf: &'mem mut [MaybeUninit<T>]) -> Self
pub const fn from_slice(buf: &'mem mut [MaybeUninit<T>]) -> Self
Creates an empty StackVec from a given slice of MaybeUninit<T>.
The vector will use this slice as its backing storage.
Sourcepub const fn new_full(buf: &'mem mut [T]) -> Selfwhere
T: Copy,
pub const fn new_full(buf: &'mem mut [T]) -> Selfwhere
T: Copy,
Creates a StackVec from a slice of already initialized data.
The vector will be full and all elements will be considered live.
Sourcepub fn to_slice(self) -> &'mem mut [MaybeUninit<T>]
pub fn to_slice(self) -> &'mem mut [MaybeUninit<T>]
Converts the StackVec back into the backing slice.
Source§impl<T> StackVec<'_, T>
impl<T> StackVec<'_, T>
Source§impl<'me, T> StackVec<'me, T>
impl<'me, T> StackVec<'me, T>
Sourcepub fn push(&mut self, v: T) -> Result<(), T>
pub fn push(&mut self, v: T) -> Result<(), T>
Appends an element to the end of the vector.
Returns Err(v) if the vector is full.
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element from the vector and returns it.
Returns None if the vector is empty.
Sourcepub fn push_n<const N: usize>(&mut self, arr: [T; N]) -> Result<(), [T; N]>
pub fn push_n<const N: usize>(&mut self, arr: [T; N]) -> Result<(), [T; N]>
Appends an array of N elements to the end of the vector.
Returns Err(arr) if there is not enough room.
Sourcepub fn pop_n<const N: usize>(&mut self) -> Option<[T; N]>
pub fn pop_n<const N: usize>(&mut self) -> Option<[T; N]>
Pops an array of N elements from the end of the vector.
Returns None if there are not enough elements.
Sourcepub fn pop_many<'b>(&'b mut self, n: usize) -> Option<RefBox<'b, [T]>>
pub fn pop_many<'b>(&'b mut self, n: usize) -> Option<RefBox<'b, [T]>>
Pops n elements from the vector and returns them as a RefBox<[T]>.
Returns None if there are not enough elements.
Sourcepub fn push_slice(&mut self, src: &[T]) -> Option<()>where
T: Clone,
pub fn push_slice(&mut self, src: &[T]) -> Option<()>where
T: Clone,
Appends a slice of elements to the end of the vector.
Returns None if there is not enough room.
Sourcepub unsafe fn alloc(&mut self, len: usize) -> Option<()>
pub unsafe fn alloc(&mut self, len: usize) -> Option<()>
reserves len uninitialised slots (UB to read them).
§Safety
- the elements must eventually be inilized before a drop
- no pops or peeks of these slots may happen
Sourcepub fn flush(&mut self, len: usize) -> Option<()>
pub fn flush(&mut self, len: usize) -> Option<()>
Drops len values from the top (destructors run).
Sourcepub fn free(&mut self, len: usize) -> Option<()>
pub fn free(&mut self, len: usize) -> Option<()>
Discards len values from the top (no destructors).
Sourcepub fn peek_many(&self, n: usize) -> Option<&[T]>
pub fn peek_many(&self, n: usize) -> Option<&[T]>
Returns a slice of the last n elements of the vector.
Sourcepub fn peek_all<'b>(&'b self) -> &'b [T]
pub fn peek_all<'b>(&'b self) -> &'b [T]
Returns a slice containing all elements in the vector.
Sourcepub fn peek_mut(&mut self) -> Option<&mut T>
pub fn peek_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the last element of the vector.
Sourcepub fn peek_many_mut(&mut self, n: usize) -> Option<&mut [T]>
pub fn peek_many_mut(&mut self, n: usize) -> Option<&mut [T]>
Returns a mutable slice of the last n elements of the vector.
Sourcepub fn peek_all_mut<'b>(&'b mut self) -> &'b mut [T]
pub fn peek_all_mut<'b>(&'b mut self) -> &'b mut [T]
Returns a mutable slice of all elements in the vector.
Sourcepub fn spot(&mut self, n: usize) -> Option<&mut T>
pub fn spot(&mut self, n: usize) -> Option<&mut T>
Mutable ref to the n-th value below the top (0 == top).
Sourcepub fn get(&self, id: usize) -> Option<&T>
pub fn get(&self, id: usize) -> Option<&T>
Mutable ref to the n-th value from the start (start==0).
Sourcepub fn get_mut(&mut self, id: usize) -> Option<&mut T>
pub fn get_mut(&mut self, id: usize) -> Option<&mut T>
Mutable ref to the n-th value from the start (start==0).
Sourcepub fn get_raw(&self, id: usize) -> Option<*mut T>
pub fn get_raw(&self, id: usize) -> Option<*mut T>
Pointer to the n-th value from the start (start==0).
Sourcepub fn split<'b>(&'b mut self) -> (&'b mut [T], StackVec<'b, T>)
pub fn split<'b>(&'b mut self) -> (&'b mut [T], StackVec<'b, T>)
Splits the vector into a slice of its elements and an empty StackVec.
(live slice on the left, empty StackVec on the right)
Sourcepub fn split_consume(self) -> (&'me mut [T], StackVec<'me, T>)
pub fn split_consume(self) -> (&'me mut [T], StackVec<'me, T>)
Consumes the vector and returns a slice of its elements and an empty StackVec.