Crates.io | typemap-ors |
lib.rs | typemap-ors |
version | 1.0.0 |
source | src |
created_at | 2022-09-03 14:45:20.042154 |
updated_at | 2022-09-03 14:45:20.042154 |
description | A typesafe store for many value types. |
homepage | |
repository | https://github.com/orphanage-rs/rust-typemap |
max_upload_size | |
id | 657914 |
size | 19,775 |
A 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::<KeyType>(Value(42));
assert_eq!(*map.get::<KeyType>().unwrap(), Value(42));
}