Struct HashMap

Source
pub struct HashMap<K, S>(/* private fields */);
Expand description

Polymorphic hash map

Internally this uses a HashMap with boxed values, thus performance is much like that of HashMap.

Note: HashMap’s capacity reservation API is excluded. There is no fundamental reason for this, other than each stored value requiring an allocation (boxing) anyway.

§Warning

Warning: values stored in the map will not have their destructor (Drop::drop) run when the map is destroyed. This is not currently fixable (min_specialization + https://github.com/rust-lang/rust/issues/46893 may be sufficient).

Implementations§

Source§

impl<K> HashMap<K, RandomState>

Source

pub fn new() -> Self

Construct a new store

Source§

impl<K, S> HashMap<K, S>

Source

pub fn with_hasher(hash_builder: S) -> Self

Construct a new store with the given hash bulider

Source

pub fn hasher(&self) -> &S

Return’s a reference to the map’s BuildHasher

Source

pub fn keys(&self) -> Keys<'_, K, Box<dyn Any>>

Returns an iterator over all keys in arbitrary order

These are not tagged keys; only the bare key is stored.

Source

pub fn len(&self) -> usize

Returns the number of elements in the store

Source

pub fn is_empty(&self) -> bool

Returns true if the store contains no elements

Source

pub fn clear(&mut self)

Clears the store, removing all elements

Source§

impl<K: Eq + Hash, S: BuildHasher> HashMap<K, S>

Source

pub fn contains_key(&self, key: &K) -> bool

Checks whether a value matching the specified key exists

Source

pub fn get_type_id(&self, key: &K) -> Option<TypeId>

Get the TypeId of a stored value

Source

pub fn get_tagged<V: 'static>(&self, key: &TaggedKey<K, V>) -> Option<&V>

Returns a reference to the value corresponding to the key

The value’s type is inferred from the TaggedKey.

Returns None if no element matches tag or if the type V does not match the element.

Source

pub fn get<V: 'static>(&self, key: &K) -> Option<&V>

Returns a reference to the value corresponding to the key

The value’s type must be specified explicitly.

Returns None if no element matches key. Panics on type mismatch.

Source

pub fn get_tagged_mut<V: 'static>( &mut self, key: &TaggedKey<K, V>, ) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the key

Source

pub fn get_mut<V: 'static>(&mut self, key: &K) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the key

Returns None if no element matches key. Panics on type mismatch.

Source

pub fn insert<V: 'static>(&mut self, key: K, value: V) -> TaggedKey<K, V>
where K: Clone,

Inserts a key-value pair into the store

Returns the corresponding TaggedKey for convenience.

Unlike HashMap this does not return any displaced old value since the type of any such value is unknown.

Source

pub fn insert_boxed<V: 'static>( &mut self, key: K, value: Box<V>, ) -> TaggedKey<K, V>
where K: Clone,

Inserts a key and boxed value pair

Returns the corresponding TaggedKey for convenience.

Unlike HashMap this does not return any displaced old value since the type of any such value is unknown.

Source

pub fn remove<V: 'static>(&mut self, key: &TaggedKey<K, V>) -> Option<V>

Removes and returns a value, if present

Panics on type mismatch.

Source

pub fn remove_boxed<V: 'static>( &mut self, key: &TaggedKey<K, V>, ) -> Option<Box<V>>

Removes and returns a value, if present

Panics on type mismatch.

Source

pub fn entry<V: 'static>(&mut self, key: K) -> Entry<'_, K, V>

Get key’s entry for in-place manipulation

Trait Implementations§

Source§

impl<K: Debug, S: Debug> Debug for HashMap<K, S>

Source§

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

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

impl<K, S: Default> Default for HashMap<K, S>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, S> Freeze for HashMap<K, S>
where S: Freeze,

§

impl<K, S> !RefUnwindSafe for HashMap<K, S>

§

impl<K, S> !Send for HashMap<K, S>

§

impl<K, S> !Sync for HashMap<K, S>

§

impl<K, S> Unpin for HashMap<K, S>
where S: Unpin, K: Unpin,

§

impl<K, S> !UnwindSafe for HashMap<K, 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.