Skip to main content

MutableHashTable

Trait MutableHashTable 

Source
pub trait MutableHashTable<TKey, TValue>: ImmutableHashTable<TKey, TValue>
where TKey: Hash + Eq,
{ // Required methods fn insert(&mut self, key: TKey, value: TValue) -> Option<TValue>; fn remove_entry<Q>(&mut self, key: &Q) -> Option<(TKey, TValue)> where TKey: Borrow<Q>, Q: Eq + Hash + ?Sized; fn insert_or_update_with<FAdd, FUpdate>( &mut self, key: TKey, adder: FAdd, updater: FUpdate, ) -> &mut TValue where FAdd: FnOnce() -> TValue, FUpdate: FnOnce(&mut TValue); fn iter_mut<'a>( &'a mut self, ) -> impl Iterator<Item = (&'a TKey, &'a mut TValue)> where TKey: 'a, TValue: 'a, Self: 'a; // Provided methods fn remove<Q>(&mut self, key: &Q) -> Option<TValue> where TKey: Borrow<Q>, Q: Eq + Hash + ?Sized { ... } fn get_or_insert_default(&mut self, key: TKey) -> &mut TValue where TValue: Default { ... } fn get_or_insert(&mut self, key: TKey, value: TValue) -> &mut TValue { ... } }
Expand description

An extension of ImmutableHashTable that allows the actual modifications to the table.

Required Methods§

Source

fn insert(&mut self, key: TKey, value: TValue) -> Option<TValue>

Inserts given (TKey, TValue) pair into the table.

Return None if the key didn’t already exist in the table.

Otherwise returns the old TValue.

Source

fn remove_entry<Q>(&mut self, key: &Q) -> Option<(TKey, TValue)>
where TKey: Borrow<Q>, Q: Eq + Hash + ?Sized,

Removes entire entry from the table. Returns (TKey, TValue) pair for the matching key or None if there is no match.

§Notes

The borrowed Q type’s Hash and Eq must match those for TKey.

Source

fn insert_or_update_with<FAdd, FUpdate>( &mut self, key: TKey, adder: FAdd, updater: FUpdate, ) -> &mut TValue
where FAdd: FnOnce() -> TValue, FUpdate: FnOnce(&mut TValue),

Searches the table for a given key. If the table contains it, then it runs updater on the corresponding TValue. Otherwise runs adder to add a new TValue to the table. Returns the mutable reference to the final TValue.

§Notes

The implementation has to guarantee that one of: adder or updater will be called during its execution, but not both.

Source

fn iter_mut<'a>( &'a mut self, ) -> impl Iterator<Item = (&'a TKey, &'a mut TValue)>
where TKey: 'a, TValue: 'a, Self: 'a,

Returns a mutable iterator over the key-value pairs in the table.

§Notes

The iterator yields (&TKey, &mut TValue) tuples representing each key-value pair in the hash table.

Provided Methods§

Source

fn remove<Q>(&mut self, key: &Q) -> Option<TValue>
where TKey: Borrow<Q>, Q: Eq + Hash + ?Sized,

Removes entire entry from the table. Similar to MutableHashTable::remove_entry, but returns TValue only for the matching key or None if there is no match.

§Notes

The borrowed Q type’s Hash and Eq must match those for TKey.

Source

fn get_or_insert_default(&mut self, key: TKey) -> &mut TValue
where TValue: Default,

Retrieves an existing TValue, or inserts a default one.

Internally equivalent to self.insert_or_update_with(key, TValue::default, |_| {}).

Source

fn get_or_insert(&mut self, key: TKey, value: TValue) -> &mut TValue

Retrieves an existing TValue, or inserts the passed one.

Internally equivalent to self.insert_or_update_with(key, || value, || {}).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<TKey, TValue> MutableHashTable<TKey, TValue> for HashMap<TKey, TValue>
where TKey: Hash + Eq,

Available on crate feature std only.
Source§

fn insert(&mut self, key: TKey, value: TValue) -> Option<TValue>

Source§

fn remove_entry<Q>(&mut self, key: &Q) -> Option<(TKey, TValue)>
where TKey: Borrow<Q>, Q: Eq + Hash + ?Sized,

Source§

fn insert_or_update_with<FAdd, FUpdate>( &mut self, key: TKey, adder: FAdd, updater: FUpdate, ) -> &mut TValue
where FAdd: FnOnce() -> TValue, FUpdate: FnOnce(&mut TValue),

Source§

fn iter_mut<'a>( &'a mut self, ) -> impl Iterator<Item = (&'a TKey, &'a mut TValue)>
where TKey: 'a, TValue: 'a, Self: 'a,

Implementors§

Source§

impl<TKey, TValue, TAllocator> MutableHashTable<TKey, TValue> for DefaultHashTable<TKey, TValue, TAllocator>
where TKey: Eq + Hash, TAllocator: Allocator,

Source§

impl<TKey, TValue, TConfig> MutableHashTable<TKey, TValue> for BytellHashTable<TKey, TValue, TConfig>
where TKey: Eq + Hash, TConfig: BytellConfig,