[][src]Crate type_record

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);
// This creates a type level record, by the name of "Record",
// with component types `Thing` and `OtherThing`.
record! {
    Record {
        Thing,
        OtherThing,
    }
}

use std::collections::HashMap;
struct HashMapping;
impl Mapping for HashMapping {
    type To<X> = HashMap<usize, X>;
    // This type is the arguments inputted into the create function.
    type Arguments = ();
    // This create function is called for each type within the record.
    fn create<X>(_: &Self::Arguments) -> Self::To<X> {
        HashMap::new()
    }
}

#[test]
fn test() {
    // Creates a record, using the `HashMapping` mapping, and arguments `()`.
    let mut record = Record::<HashMapping>::new(());
    // Gets a mutable reference to the `HashMap<usize, Thing>` within the record.
    record.get_mut().insert(0, Thing(16));
    // Gets a `HashMap<usize, Thing>` and finds the inserted `Thing`.
    assert_eq!(16, record.get::<Thing>()[&0].0);
    record
        .get_mut()
        .insert(18, OtherThing(1024));
    assert_eq!(1024, record.get::<OtherThing>()[&18].0);
}

Macros

record

The macro to create a type level record. Always generates a struct with name $record_name, with a single generic argument, which is the Mapping that controls what the record contains.

Structs

_EmptyMapping

This type exists for macro expansion. Do not use.

Traits

Mapping

A mapping from a type to another type, with a function for creation.

_Record

This type exists for macro expansion. Do not use.

_RecordType

This type exists for macro expansion. Do not use.