Skip to main content

mut_bump_vec

Macro mut_bump_vec 

Source
macro_rules! mut_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 MutBumpVec.

$bump can be any type that implements MutBumpAllocatorTyped.

§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: MutBumpVec<i32, _> = mut_bump_vec![in &mut bump];
assert!(vec.is_empty());
  • Create a MutBumpVec containing a given list of elements:
let vec = mut_bump_vec![in &mut bump; 1, 2, 3];
assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
assert_eq!(vec[2], 3);
  • Create a MutBumpVec from a given element and size:
let vec = mut_bump_vec![in &mut 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, mut_bump_vec![in &mut 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 mut_bump_vec![in &mut 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.