pub struct TypeMap(_);
Available on crate feature client only.
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

Creates a new instance of TypeMap.

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());

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more