一种由类型进行键值的安全存储,其中包含不同类型的值。
TypeMapA typesafe store keyed by types and containing different types of values.
It provides functionality similar to AnyMap, but is more flexible because it allows for key-value pairs, rather than enforcing that keys and values are the same type.
Key-value associations are defined through the Key trait, which uses an
associated type parameter and trait coherence rules to enforce the invariants
of TypeMap.
extern crate typemap;
use typemap::{TypeMap, Key};
struct KeyType;
#[derive(Debug, PartialEq)]
struct Value(i32);
impl Key for KeyType { type Value = Value; }
#[test] fn test_pairing() {
let mut map = TypeMap::new();
map.insert::(Value(42));
assert_eq!(*map.get::().unwrap(), Value(42));
}暂无开放 Issues,或尚未同步最近议题。