Macro slice_ring_buffer::sdeq

source ·
macro_rules! sdeq {
    ($elem:expr; $n:expr) => { ... };
    () => { ... };
    ($($x:expr),*) => { ... };
    ($($x:expr,)*) => { ... };
}
Expand description

Creates a SliceRingBuffer containing the arguments.

sdeq! allows SliceRingBuffers to be defined with the same syntax as array expressions. There are two forms of this macro:

let v: SliceRingBuffer<i32> = sdeq![1, 2, 3];
assert_eq!(v[0], 1);
assert_eq!(v[1], 2);
assert_eq!(v[2], 3);
let v = sdeq![7; 3];
assert_eq!(v, [7, 7, 7]);

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, sdeq![Rc::new(1); 5] will create a deque of five references to the same boxed integer value, not five references pointing to independently boxed integers.

let v = sdeq![Rc::new(1_i32); 5];
let ptr: *const i32 = &*v[0] as *const i32;
for i in v.iter() {
    assert_eq!(Rc::into_raw(i.clone()), ptr);
}