Trait TypeMapGet

Source
pub trait TypeMapGet: Sealed {
    // Required method
    fn try_get<T: 'static>(&self) -> Option<&T>;

    // Provided method
    fn get<T: 'static>(&self) -> &T
       where Self: Contains<T> { ... }
}
Expand description

The TypeMapGet trait allows for obtaining values of types from a typemap.

Required Methods§

Source

fn try_get<T: 'static>(&self) -> Option<&T>

Attempts to obtain a value of a given type from the map.

This is mainly intended for the case where you don’t require a type to be present, but would like to act on it if it is. Returns None if the type is not present in the map. On nightly, this should only occur if Contains<T> is not implemented.

use typemap_core::{typemap, TypeMapGet};

let map = typemap!(u32 = 23u32);

assert_eq!(map.try_get::<u32>(), Some(&23u32));
assert_eq!(map.try_get::<u128>(), None);

Provided Methods§

Source

fn get<T: 'static>(&self) -> &T
where Self: Contains<T>,

Obtains a value of a given type from the map.

On nightly, you can only call this method if the type is actually present. (i.e. the map implements implements Contains<T>)

§Panics

This function panics if try_get would return None. This should only be possible on stable, so you can weed out potential panics by occasionally checking against nightly.

use typemap_core::{typemap, TypeMapGet};

let map = typemap!(u32 = 23u32, &str = "Hello, world!");

println!("{}", map.get::<&str>());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl TypeMapGet for TyEnd

Source§

impl<V: 'static, R: TypeMapGet> TypeMapGet for &Ty<V, R>

Source§

impl<V: 'static, R: TypeMapGet> TypeMapGet for &mut Ty<V, R>

Source§

impl<V: 'static, R: TypeMapGet> TypeMapGet for Ty<V, R>