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 Unit
s.
Provided Methods§
sourcefn over<M>(self, _m: M) -> Unit<Self, [M; 0]>
fn over<M>(self, _m: M) -> Unit<Self, [M; 0]>
Let this value be an index into M
s.
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];