pub struct TypeMap(_);
client
only.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
sourceimpl TypeMap
impl TypeMap
sourcepub fn contains_key<T>(&self) -> bool where
T: TypeMapKey,
pub fn contains_key<T>(&self) -> bool where
T: TypeMapKey,
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>());
sourcepub fn insert<T>(&mut self, value: <T as TypeMapKey>::Value) where
T: TypeMapKey,
pub fn insert<T>(&mut self, value: <T as TypeMapKey>::Value) where
T: TypeMapKey,
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);
sourcepub fn entry<T>(&mut self) -> Entry<'_, T> where
T: TypeMapKey,
pub fn entry<T>(&mut self) -> Entry<'_, T> where
T: TypeMapKey,
Retrieve the entry based on its TypeMapKey
sourcepub fn get<T>(&self) -> Option<&<T as TypeMapKey>::Value> where
T: TypeMapKey,
pub fn get<T>(&self) -> Option<&<T as TypeMapKey>::Value> where
T: 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);
sourcepub fn get_mut<T>(&mut self) -> Option<&mut <T as TypeMapKey>::Value> where
T: TypeMapKey,
pub fn get_mut<T>(&mut self) -> Option<&mut <T as TypeMapKey>::Value> where
T: TypeMapKey,
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);
sourcepub fn remove<T>(&mut self) -> Option<<T as TypeMapKey>::Value> where
T: TypeMapKey,
pub fn remove<T>(&mut self) -> Option<<T as TypeMapKey>::Value> where
T: TypeMapKey,
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());
Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more