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 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) -> 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>());
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, Global>)> 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, Global>)> 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>

§

type Item = (TypeId, Box<S, Global>)

The type of the elements being iterated over.
§

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

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: ?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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneDebuggableStorage for Twhere T: DebuggableStorage + Clone,

source§

impl<T> CloneableStorage for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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 Twhere T: Any + Send + Sync + Debug,