Trait new_units::OfUnit

source ·
pub trait OfUnit: Sized {
    // Provided methods
    fn of<M>(self, _m: M) -> Unit<Self, M> { ... }
    fn over<M>(self, _m: M) -> Unit<Self, [M; 0]> { ... }
}
Expand description

Utility trait for constructing Units.

Provided Methods§

source

fn of<M>(self, _m: M) -> Unit<Self, M>

Ascribe a unit onto this value.

Example
struct Diamonds;
4.of(Diamonds);
source

fn over<M>(self, _m: M) -> Unit<Self, [M; 0]>

Let this value be an index into Ms.

Example

// The marker type.
struct Cheese;
let mut cheeses = vec![].of(Cheese);
// made cheeses: Unit<Vec<&str>, Cheese>

// Adds a new cheese to the list, and return its index.
let mut register = |name| {
    let index: IndexTo<Cheese, usize> = cheeses.len().over(Cheese);
    cheeses.push(name);
    // Save space by using u8 instead of usize.
    index.shrink::<u8>()
};

let gouda = register("gouda");
let goata = register("goata");
let bahda = register("bahda");

dbg!(gouda);
dbg!(cheeses[goata]);
cheeses[bahda] = "greata";
dbg!(&cheeses);


struct Goats;
let mut goats = vec![].of(Goats);
let mut register = |name| {
    let index = goats.len().over(Goats);
    goats.push(name);
    index
};
let billy = register("billy");
let goatb = register("goatb");

// So confusing! Thankfully Rust lets us do deep type checking.
goats[goatb];
cheeses[goata];

Implementors§

source§

impl<X> OfUnit for X