Struct typemap_rev::TypeMap [−][src]
pub struct TypeMap(_);
Expand description
TypeMap is a simple abstraction around the standard library’s HashMap
type, where types are its keys. This allows for statically-checked value
retrieval.
Implementations
Returns true
if the map contains a value for the specified TypeMapKey
.
use typemap_rev::{TypeMap, TypeMapKey};
struct Number;
impl TypeMapKey for Number {
type Value = i32;
}
let mut map = TypeMap::new();
assert!(!map.contains_key::<Number>());
map.insert::<Number>(42);
assert!(map.contains_key::<Number>());
Inserts a new value based on its TypeMapKey
.
If the value has been already inserted, it will be overwritten
with the new value.
use typemap_rev::{TypeMap, TypeMapKey};
struct Number;
impl TypeMapKey for Number {
type Value = i32;
}
let mut map = TypeMap::new();
map.insert::<Number>(42);
// Overwrite the value of `Number` with -42.
map.insert::<Number>(-42);
Retrieve the entry based on its TypeMapKey
Retrieve a reference to a value based on its TypeMapKey
.
Returns None
if it couldn’t be found.
use typemap_rev::{TypeMap, TypeMapKey};
struct Number;
impl TypeMapKey for Number {
type Value = i32;
}
let mut map = TypeMap::new();
map.insert::<Number>(42);
assert_eq!(*map.get::<Number>().unwrap(), 42);
Retrieve a mutable reference to a value based on its TypeMapKey
.
Returns None
if it couldn’t be found.
use typemap_rev::{TypeMap, TypeMapKey};
struct Number;
impl TypeMapKey for Number {
type Value = i32;
}
let mut map = TypeMap::new();
map.insert::<Number>(42);
assert_eq!(*map.get::<Number>().unwrap(), 42);
*map.get_mut::<Number>().unwrap() -= 42;
assert_eq!(*map.get::<Number>().unwrap(), 0);
Removes a value from the map based on its TypeMapKey
, returning the value or None
if
the key has not been in the map.
use typemap_rev::{TypeMap, TypeMapKey};
struct Text;
impl TypeMapKey for Text {
type Value = String;
}
let mut map = TypeMap::new();
map.insert::<Text>(String::from("Hello TypeMap!"));
assert!(map.remove::<Text>().is_some());
assert!(map.get::<Text>().is_none());