[][src]Macro coca::index_type

macro_rules! index_type {
    ($v:vis $name:ident: $repr:ty) => { ... };
}

Generates a newtype wrapping an implementor of Capacity.

This can help in avoiding use of the wrong index with a Vec.

Examples

use coca::{index_type, Vec};
use core::mem::MaybeUninit;

index_type! { pub IndexA: u8 };
index_type! { 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`