Trait typemap_core::TypeMapSet [−][src]
The TypeMapSet trait allows for setting values of types in a typemap.
Required methods
fn try_set<T: 'static>(&mut self, value: T) -> bool
[src]
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
fn set<T: 'static>(&mut self, value: T) where
Self: ContainsMut<T>,
[src]
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>());