Struct typemap_rev::TypeMap
source · pub struct TypeMap<S: ?Sized = DefaultStorage>(_);
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§
source§impl<S: ?Sized + Any + Send + Sync> TypeMap<S>
impl<S: ?Sized + Any + Send + Sync> TypeMap<S>
sourcepub fn contains_key<T>(&self) -> boolwhere
T: TypeMapKey,
pub fn contains_key<T>(&self) -> boolwhere 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::Value)where
T: TypeMapKey,
T::Value: IntoBox<S>,
pub fn insert<T>(&mut self, value: T::Value)where T: TypeMapKey, T::Value: IntoBox<S>,
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, S>where
T: TypeMapKey,
T::Value: IntoBox<S>,
pub fn entry<T>(&mut self) -> Entry<'_, T, S>where T: TypeMapKey, T::Value: IntoBox<S>,
Retrieve the entry based on its TypeMapKey
sourcepub fn get<T>(&self) -> Option<&T::Value>where
T: TypeMapKey,
T::Value: IntoBox<S>,
pub fn get<T>(&self) -> Option<&T::Value>where T: TypeMapKey, T::Value: IntoBox<S>,
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::Value>where
T: TypeMapKey,
T::Value: IntoBox<S>,
pub fn get_mut<T>(&mut self) -> Option<&mut T::Value>where T: TypeMapKey, T::Value: IntoBox<S>,
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::Value>where
T: TypeMapKey,
T::Value: IntoBox<S>,
pub fn remove<T>(&mut self) -> Option<T::Value>where T: TypeMapKey, T::Value: IntoBox<S>,
Removes a value from the map based on its TypeMapKey
.
Returns a boolean indicating whether the value existed prior to its removal.
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§
source§impl<S: ?Sized> Extend<(TypeId, Box<S, Global>)> for TypeMap<S>
impl<S: ?Sized> Extend<(TypeId, Box<S, Global>)> for TypeMap<S>
source§fn extend<T: IntoIterator<Item = (TypeId, Box<S>)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (TypeId, Box<S>)>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
source§impl<S: ?Sized> IntoIterator for TypeMap<S>
impl<S: ?Sized> IntoIterator for TypeMap<S>
Auto Trait Implementations§
impl<S: ?Sized> RefUnwindSafe for TypeMap<S>where S: RefUnwindSafe,
impl<S: ?Sized> Send for TypeMap<S>where S: Send,
impl<S: ?Sized> Sync for TypeMap<S>where S: Sync,
impl<S: ?Sized> Unpin for TypeMap<S>
impl<S: ?Sized> UnwindSafe for TypeMap<S>where S: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more