pub struct TypeMap<S: ?Sized = DefaultStorage>(/* private fields */);
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>)> for TypeMap<S>
impl<S: ?Sized> Extend<(TypeId, Box<S>)> 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> Freeze for TypeMap<S>where
S: ?Sized,
impl<S> RefUnwindSafe for TypeMap<S>where
S: RefUnwindSafe + ?Sized,
impl<S> Send for TypeMap<S>
impl<S> Sync for TypeMap<S>
impl<S> Unpin for TypeMap<S>where
S: ?Sized,
impl<S> UnwindSafe for TypeMap<S>where
S: UnwindSafe + ?Sized,
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