Struct TypeMap

Source
pub struct TypeMap<S = RandomState> { /* private fields */ }
Expand description

A container for values of varying types.

Values contained in a TypeMap are stored uniquely according to their type. This means that, for each possible type, only one value may be stored in a single TypeMap instance.

If you would like to store multiple values of a given type, mapped to individual keys, use PolyMap.

§Example

use polymap::TypeMap;

let mut map = TypeMap::new();

// Stores a `&str` value
map.insert("Hello, world!");

// Stores an `i32` value
map.insert(123);

// Gets a reference to the stored value
let &foo: &&str = map.get().unwrap();
assert_eq!(foo, "Hello, world!");

let &bar: &i32 = map.get().unwrap();
assert_eq!(bar, 123);

Implementations§

Source§

impl TypeMap<RandomState>

Source

pub fn new() -> TypeMap

Constructs a new TypeMap.

Source

pub fn with_capacity(n: usize) -> TypeMap

Constructs a new TypeMap with space reserved for n elements.

Source§

impl<S: BuildHasher> TypeMap<S>

Source

pub fn with_hasher(hash_builder: S) -> TypeMap<S>

Creates an empty TypeMap which will use the given hash builder to hash keys.

Source

pub fn clear(&mut self)

Removes all values from the map.

Source

pub fn contains<T: Any>(&self) -> bool

Returns whether the map contains a value corresponding to the given type.

Source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating.

Source

pub fn entry<T: Any>(&mut self) -> Entry<'_, T>

Returns the type’s corresponding entry in the map for in-place manipulation.

Source

pub fn get<T: Any>(&self) -> Option<&T>

Returns a reference to the value corresponding to the given type.

If the type is not contained within the map, None will be returned.

Source

pub fn get_mut<T: Any>(&mut self) -> Option<&mut T>

Returns a mutable reference to the value corresponding to the given type.

If the type is not contained within the map, None will be returned.

Source

pub fn insert<T: Any>(&mut self, t: T) -> Option<T>

Inserts a value into the map. If a value of that type is already present, that value is returned. Otherwise, None is returned.

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

Source

pub fn is_empty(&self) -> bool

Returns whether the map is empty.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional additional elements.

Source

pub fn remove<T: Any>(&mut self) -> Option<T>

Removes a value from the map, returning the value if one existed.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the map as much as possible.

Trait Implementations§

Source§

impl<S> Debug for TypeMap<S>

Source§

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

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

impl<S: BuildHasher + Default> Default for TypeMap<S>

Source§

fn default() -> TypeMap<S>

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

Auto Trait Implementations§

§

impl<S> Freeze for TypeMap<S>
where S: Freeze,

§

impl<S = RandomState> !RefUnwindSafe for TypeMap<S>

§

impl<S = RandomState> !Send for TypeMap<S>

§

impl<S = RandomState> !Sync for TypeMap<S>

§

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

§

impl<S = RandomState> !UnwindSafe for TypeMap<S>

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> 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, 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.