macro_rules! stack_vec {
() => { ... };
($ty: ty; $cap: literal) => { ... };
($elem:expr; $n:expr) => { ... };
($cap:literal# $elem:expr; $n:expr) => { ... };
($($x:expr),+ $(,)?) => { ... };
($cap:literal# $($x:expr),+ $(,)?) => { ... };
}
Expand description
Creates a StackVec
containing the arguments.
stack_vec!
allows StackVec
s to be defined with the same syntax as array expressions.
There are two forms of this macro:
- Creates a empty
StackVec
:
use stack_buf::{StackVec, stack_vec};
let vec: StackVec<i32, 8> = stack_vec![];
assert!(vec.is_empty());
assert_eq!(vec.capacity(), 8);
let vec = stack_vec![i32; 16];
assert!(vec.is_empty());
assert_eq!(vec.capacity(), 16);
- Creates a
StackVec
containing a given list of elements:
use stack_buf::{StackVec, stack_vec};
let vec = stack_vec![128#1, 2, 3];
assert_eq!(vec.capacity(), 128);
assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
assert_eq!(vec[2], 3);
let vec = stack_vec![1, 2, 3];
assert_eq!(vec.capacity(), 3);
- Creates a
StackVec
from a given element and size:
use stack_buf::{StackVec, stack_vec};
let v = stack_vec![0x8000#1; 3];
assert_eq!(v.as_slice(), [1, 1, 1]);
assert_eq!(v.capacity(), 0x8000);
let v = stack_vec![1; 3];
assert_eq!(v.capacity(), 3);
Note that unlike array expressions this syntax supports all elements
which implement Clone
and the number of elements doesn’t have to be
a constant.
This will use clone
to duplicate an expression, so one should be careful
using this with types having a nonstandard Clone
implementation. For
example, stack_vec![Rc::new(1); 5]
will create a vector of five references
to the same boxed integer value, not five references pointing to independently
boxed integers.