Struct typemap_rev::TypeMap

source ·
pub struct TypeMap<S: ?Sized = DefaultStorage>(_);
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.

Creates a new instance of TypeMap with a custom storage type.

Returns the amount of entries in the map.

Returns an indicator whether the map is empty (no entries).

Clears all entries in the map.

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.

Returns a boolean indicating whether the value existed prior to its removal.

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 a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. 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.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
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.