StackVec

Struct StackVec 

Source
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>

Source

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.

Source

pub const fn new_full(buf: &'mem mut [T]) -> Self
where T: Copy,

Creates a StackVec from a slice of already initialized data.

The vector will be full and all elements will be considered live.

Source

pub fn to_slice(self) -> &'mem mut [MaybeUninit<T>]

Converts the StackVec back into the backing slice.

Source

pub fn set_other<'b>(&self, other: &mut StackVec<'b, T>) -> Result<(), usize>
where T: Clone,

Clones the content of this vector to another StackVec.

The other vector will be cleared before the copy.

Source§

impl<T> StackVec<'_, T>

Source

pub fn len(&self) -> usize

Returns the number of elements in the vector.

Source

pub unsafe fn set_len(&mut self, len: usize)

this is a weird mix of alloc and free it is mainly used for checkpoints

§Safety
  1. the length must be in bounds
  2. if elements are alloced same as alloc
Source

pub fn capacity(&self) -> usize

Source

pub fn get_base(&self) -> *mut T

Returns a raw pointer to the base of the vector’s buffer.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

Source

pub fn room_left(&self) -> usize

Returns the number of additional elements the vector can hold.

Source§

impl<'me, T> StackVec<'me, T>

Source

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.

Source

pub fn pop(&mut self) -> Option<T>

Removes the last element from the vector and returns it.

Returns None if the vector is empty.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub unsafe fn alloc(&mut self, len: usize) -> Option<()>

reserves len uninitialised slots (UB to read them).

§Safety
  1. the elements must eventually be inilized before a drop
  2. no pops or peeks of these slots may happen
Source

pub fn flush(&mut self, len: usize) -> Option<()>

Drops len values from the top (destructors run).

Source

pub fn free(&mut self, len: usize) -> Option<()>

Discards len values from the top (no destructors).

Source

pub fn peek(&self) -> Option<&T>

Returns a reference to the last element of the vector.

Source

pub fn peek_many(&self, n: usize) -> Option<&[T]>

Returns a slice of the last n elements of the vector.

Source

pub fn peek_all<'b>(&'b self) -> &'b [T]

Returns a slice containing all elements in the vector.

Source

pub fn peek_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the last element of the vector.

Source

pub fn peek_many_mut(&mut self, n: usize) -> Option<&mut [T]>

Returns a mutable slice of the last n elements of the vector.

Source

pub fn peek_all_mut<'b>(&'b mut self) -> &'b mut [T]

Returns a mutable slice of all elements in the vector.

Source

pub fn peek_raw(&self) -> Option<*mut T>

Raw pointer to the current top (no lifetime).

Source

pub fn spot(&mut self, n: usize) -> Option<&mut T>

Mutable ref to the n-th value below the top (0 == top).

Source

pub fn spot_raw(&self, n: usize) -> Option<*mut T>

Raw pointer variant (no lifetime).

Source

pub fn get(&self, id: usize) -> Option<&T>

Mutable ref to the n-th value from the start (start==0).

Source

pub fn get_mut(&mut self, id: usize) -> Option<&mut T>

Mutable ref to the n-th value from the start (start==0).

Source

pub fn get_raw(&self, id: usize) -> Option<*mut T>

Pointer to the n-th value from the start (start==0).

Source

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)

Source

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.

Source

pub fn split_raw<'b>(&'b mut self) -> (*mut T, StackVec<'b, T>)

Raw pointer to live slice + empty right-hand StackVec (no borrow).

Trait Implementations§

Source§

impl<T> Drop for StackVec<'_, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Index<usize> for StackVec<'_, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, id: usize) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for StackVec<'_, T>

Source§

fn index_mut(&mut self, id: usize) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Write for StackVec<'_, u8>

Source§

fn write_str(&mut self, s: &str) -> Result<(), Error>

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more
Source§

impl<'m, T: Send> Send for StackVec<'m, T>

Source§

impl<'m, T: Sync> Sync for StackVec<'m, T>

Auto Trait Implementations§

§

impl<'mem, T> Freeze for StackVec<'mem, T>

§

impl<'mem, T> RefUnwindSafe for StackVec<'mem, T>
where T: RefUnwindSafe,

§

impl<'mem, T> Unpin for StackVec<'mem, T>

§

impl<'mem, T> !UnwindSafe for StackVec<'mem, T>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.