Trait TypeMapSet

Source
pub trait TypeMapSet: Sealed {
    // Required method
    fn try_set<T: 'static>(&mut self, value: T) -> bool;

    // Provided method
    fn set<T: 'static>(&mut self, value: T)
       where Self: ContainsMut<T> { ... }
}
Expand description

The TypeMapSet trait allows for setting values of types in a typemap.

Required Methods§

Source

fn try_set<T: 'static>(&mut self, value: T) -> bool

Attempts to mutably set a value of a given type in 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 false if the type is not present in the map. On nightly, this should only occur if ContainsMut<T> is not implemented.

use typemap_core::{typemap, TypeMapGet, TypeMapSet};

let mut map = typemap!(u32 = 13u32);
assert!(map.try_set(42u32));
assert_eq!(map.get::<u32>(), &42);

// type is not present in map
assert!(!map.try_set(1u128));

Provided Methods§

Source

fn set<T: 'static>(&mut self, value: T)
where Self: ContainsMut<T>,

Mutable sets a value of a given type in the map.

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

§Panics

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

use typemap_core::{typemap, TypeMapGet, TypeMapSet};

let mut map = typemap!(&str = "");
map.set("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 TypeMapSet for TyEnd

Source§

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

Source§

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

Source§

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