WARNING: This crate requires a nightly compiler and
generic_associated_types
.
This crate provides a macro to create a type level record, in which values
can be fetched by their types. The types of the things within the record are
specified by an inputted Mapping
.
Example usage:
#![allow(incomplete_features)]
#![feature(generic_associated_types)]
use type_record::{record, Mapping};
struct Thing(u8);
struct OtherThing(i64);
record! {
Record {
Thing,
OtherThing,
}
}
use std::collections::HashMap;
struct HashMapping;
impl Mapping for HashMapping {
type To<X> = HashMap<usize, X>;
type Arguments = ();
fn create<X>(_: &Self::Arguments) -> Self::To<X> {
HashMap::new()
}
}
#[test]
fn test() {
let mut record = Record::<HashMapping>::new(());
record.get_mut().insert(0, Thing(16));
assert_eq!(16, record.get::<Thing>()[&0].0);
record
.get_mut()
.insert(18, OtherThing(1024));
assert_eq!(1024, record.get::<OtherThing>()[&18].0);
}