A simple crate for a stack-allocated stack. Useful for when you want a small
Vec
of items with a known size bound and want to avoid dynamic allocation.
Design
Stack
implements a basic fixed-size, stack-allocated, FIFO data structure.
Uses const
generics in order to make the typing more ergonomic.
To account for overflows, any method that increases stack size returns a Result
containing any values over capacity inside the Err
variant. There is no
dynamic allocation whatsoever, even when going over-capacity.
Note
Obviously this is similar to smallvec
,
and frankly, you should probably just use smallvec
. The
devs did great work. I just wanted something a little closer to my design
preferences, and it was a fun weekend project.
Example
use ;
//Manual creation
let mut s1 = ;
//Pushing returns a result
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
//We can ergonomically ignore the #[must_use] warning if needed with `Result::ok()`
s1.push.ok;
s1.push.ok;
assert_eq!;
//Overflows return return the extra value(s) in a `Result::Err()`
assert_eq!;
//Creation using a list of values and a capacity
let s2 = stack!;
assert_eq!;
assert_eq!;
//Repeating a value of `3` 4x times with a capacity of 5
let s3 = stack!;
assert_eq!;
assert_eq!;
assert_eq!;