#record #mapping #level #documentation #create #component #arguments

nightly type-record

类型级别记录(详细信息请参阅文档)

2 个稳定版本

1.0.1 2020 年 12 月 18 日

#2128Rust 模式

ISC 许可证

7KB
77

警告:此软件包需要 nightly 编译器和 generic_associated_types

此软件包提供了一个宏来创建类型级别的记录,其中可以通过类型检索值。记录内事物的类型由输入的 Mapping 指定。

示例用法

#![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);
}

无运行时依赖项