bump_vec

Macro bump_vec 

Source
macro_rules! bump_vec {
    [in $bump:expr] => { ... };
    [in $bump:expr; $($values:expr),* $(,)?] => { ... };
    [in $bump:expr; $value:expr; $count:expr] => { ... };
    [try in $bump:expr] => { ... };
    [try in $bump:expr; $($values:expr),* $(,)?] => { ... };
    [try in $bump:expr; $value:expr; $count:expr] => { ... };
}
Expand description

This is like vec! but allocates inside a bump allocator, returning a BumpVec.

$bump can be any type that implements BumpAllocatorExt.

§Panics

If used without try, panics on allocation failure.

§Errors

If used with try, errors on allocation failure.

§Examples

There are three forms of this macro:

let vec: BumpVec<i32, _> = bump_vec![in &bump];
assert!(vec.is_empty());
  • Create a BumpVec containing a given list of elements:
let vec = bump_vec![in &bump; 1, 2, 3];
assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
assert_eq!(vec[2], 3);
  • Create a BumpVec from a given element and size:
let vec = bump_vec![in &bump; 1; 3];
assert_eq!(vec, [1, 1, 1]);

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, bump_vec![in &bump; 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.

Also, note that bump_vec![in &bump; expr; 0] is allowed, and produces an empty vector. This will still evaluate expr, however, and immediately drop the resulting value, so be mindful of side effects.