Struct TypeMap

Source
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 TypeMap

Source

pub fn new() -> Self

Creates a new instance of TypeMap.

Source§

impl<S: ?Sized + Any + Send + Sync> TypeMap<S>

Source

pub fn custom() -> Self

Creates a new instance of TypeMap with a custom storage type.

Source

pub fn len(&self) -> usize

Returns the amount of entries in the map.

Source

pub fn is_empty(&self) -> bool

Returns an indicator whether the map is empty (no entries).

Source

pub fn clear(&mut self)

Clears all entries in the map.

Source

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>());
Source

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);
Source

pub fn entry<T>(&mut self) -> Entry<'_, T, S>
where T: TypeMapKey, T::Value: IntoBox<S>,

Retrieve the entry based on its TypeMapKey

Source

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);
Source

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);
Source

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> Clone for TypeMap<S>
where Box<S>: Clone,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: ?Sized + DebuggableStorage> Debug for TypeMap<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: ?Sized> Default for TypeMap<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S: ?Sized> Extend<(TypeId, Box<S>)> for TypeMap<S>

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

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> FromIterator<(TypeId, Box<S>)> for TypeMap<S>

Source§

fn from_iter<T: IntoIterator<Item = (TypeId, Box<S>)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<S: ?Sized> IntoIterator for TypeMap<S>

Source§

type Item = (TypeId, Box<S>)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<TypeId, Box<S>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

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>
where S: Send + ?Sized,

§

impl<S> Sync for TypeMap<S>
where S: Sync + ?Sized,

§

impl<S> Unpin for TypeMap<S>
where S: ?Sized,

§

impl<S> UnwindSafe for TypeMap<S>
where S: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneDebuggableStorage for T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> CloneableStorage for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DebuggableStorage for T
where T: Any + Send + Sync + Debug,