[][src]Struct typemap_rev::TypeMap

pub struct TypeMap(_);

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

impl TypeMap[src]

pub fn new() -> Self[src]

Creates a new instance of TypeMap.

pub fn contains_key<T>(&self) -> bool where
    T: TypeMapKey
[src]

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

pub fn insert<T>(&mut self, value: T::Value) where
    T: TypeMapKey
[src]

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

pub fn entry<T>(&mut self) -> Entry<'_, T> where
    T: TypeMapKey
[src]

Retrieve the entry based on its TypeMapKey

pub fn get<T>(&self) -> Option<&T::Value> where
    T: TypeMapKey
[src]

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

pub fn get_mut<T>(&mut self) -> Option<&mut T::Value> where
    T: TypeMapKey
[src]

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

pub fn remove<T>(&mut self) -> Option<T::Value> where
    T: TypeMapKey
[src]

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

Auto Trait Implementations

impl !RefUnwindSafe for TypeMap

impl Send for TypeMap

impl Sync for TypeMap

impl Unpin for TypeMap

impl !UnwindSafe for TypeMap

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.