Macro stack

Source
macro_rules! stack {
    ($($q:tt)? type $name:ident $into_iter_name:ident $n:literal) => { ... };
    ($t:ty; $n:literal) => { ... };
    ($n:literal) => { ... };
}
Expand description

Create an ad-hoc sized Vec-like array on the stack.

§Usage

let sv = stack![usize; 100];
assert_eq!(sv.cap(), 100);

Can be used mostly just like Vec<T>, except the size must be a literal.

let mut sv = stack![12];
for x in 0..12 {
 sv.push(vec![x,x]);
}
assert_eq!(sv.into_iter().map(|x| x.into_iter().product::<i32>()).sum::<i32>(), (0..12).map(|x| x*x).sum::<i32>());

§Defining StackVec types

You can also use this macro to define your own transparent StackVec types

stack!(pub type StackVec10 StackVec10IntoIter 10);

let sv: StackVec10<_> = [1i32; 10].iter().copied().collect();
let sv: StackVec10IntoIter<_> = sv.into_iter();
assert_eq!(sv.sum::<i32>(), 10);

See one of the StackVec structs defined here for more information.