Crate andex[][src]

Expand description

andex is a single-file, zero-dependency rust crate that helps us create a strongly-typed, zero-cost, safe array index and use it to index a custom array wrapper.

This is specially useful in scenarios where we have different arrays inside a struct and we want reference members without holding “hard” references to those members.

Basic usage

use andex::*;
use andex::impl_andex_for;

// Create the array wrapper; we can use the andex size already:
#[derive(Default)]
pub struct MyU32([u32; MyIdx::SIZE]);

// Create the andex type alias:
type MyIdx = Andex<12>;

// Use `impl_andex_for` to make it indexable:
impl_andex_for!(MyU32, u32, MyIdx);

// We can use `impl_andex_for` with other wrappers too:
pub struct MyF64([f64; MyIdx::SIZE]);
impl_andex_for!(MyF64, f64, MyIdx);

fn example() {
    // Iterate:
    for i in MyIdx::iter() {
        println!("{:?}", i);
    }
    // Generate first index at compile time:
    const first : MyIdx = MyIdx::new::<0>();
    // Index the collection:
    let myu32 = MyU32::default();
    println!("{:?}", myu32[first]);
}

Macros

Implement Index and IndexMut for the provided array wrapper, base type and array size

Implement Deref for the wrapped array, making the wrapper behave like it except for only being indexable with the andex.

Structs

Array index generic type

Enums

Andex errors enum