Macro coca::index_type
source · [−]macro_rules! index_type {
( $(#[$attrs:meta])* $v:vis $name:ident: $repr:ty ) => { ... };
( $(#[$attrs:meta])* $v:vis $name:ident: $repr:ty ; $($rest:tt)* ) => { ... };
() => { ... };
}
Expand description
Generates one or more new types wrapping an implementor of Capacity
.
This can help in avoiding use of the wrong index with a Vec
.
Examples
ⓘ
use coca::{index_type, vec::Vec};
use core::mem::MaybeUninit;
index_type! {
pub IndexA: u8;
IndexB: u8;
};
let mut backing_a = [MaybeUninit::<u32>::uninit(); 20];
let mut backing_b = [MaybeUninit::<u32>::uninit(); 30];
let mut vec_a = Vec::<_, _, IndexA>::from(&mut backing_a[..]);
for i in 0..20 { vec_a.push(i); }
let mut vec_b = Vec::<_, _, IndexB>::from(&mut backing_b[..]);
for i in 0..30 { vec_b.push(i * 2); }
let a = vec_a[IndexA(10)];
let b = vec_b[IndexB(15)];
let c = vec_a[IndexB(25)];
// ^^^^^^^^^^^^^^^^^ `coca::Vec<...>` cannot be indexed by `IndexB`