Skip to main content

MutableHashTable

Trait MutableHashTable 

Source
pub trait MutableHashTable<TKey, TValue>: ImmutableHashTable<TKey, TValue>
where TKey: Hash + Eq,
{
Show 13 methods // Required methods fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut TValue> where TKey: Borrow<Q>, Q: Eq + Hash + ?Sized; fn get_key_value_mut<Q>( &mut self, key: &Q, ) -> Option<KVP<&TKey, &mut TValue>> where TKey: Borrow<Q>, Q: Eq + Hash + ?Sized; fn remove_entry<Q>(&mut self, key: &Q) -> Option<KVP<TKey, TValue>> where TKey: Borrow<Q>, Q: Eq + Hash + ?Sized; fn try_insert_or_update_with<FAdd, FUpdate>( &mut self, key: TKey, adder: FAdd, updater: FUpdate, ) -> Result<&mut TValue, HashTableError> where FAdd: FnOnce() -> TValue, FUpdate: FnOnce(&mut TValue); fn iter_mut<'a>( &'a mut self, ) -> impl Iterator<Item = KVP<&'a TKey, &'a mut TValue>> + 'a where TKey: 'a, TValue: 'a, Self: 'a; // Provided methods fn insert(&mut self, key: TKey, value: TValue) -> Option<TValue> { ... } fn try_insert( &mut self, key: TKey, value: TValue, ) -> Result<Option<TValue>, HashTableError> { ... } 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 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 try_get_or_insert_default( &mut self, key: TKey, ) -> Result<&mut TValue, HashTableError> where TValue: Default { ... } fn get_or_insert(&mut self, key: TKey, value: TValue) -> &mut TValue { ... } fn try_get_or_insert( &mut self, key: TKey, value: TValue, ) -> Result<&mut TValue, HashTableError> { ... }
}
Expand description

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

Required Methods§

Source

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

Checks if the table contains the corresponding key, and if so then returns a mutable reference to the TValue, or None otherwise.

§Notes

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

Source

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

Checks if the table contains the corresponding key, and if so then returns a mutable reference to the TValue, or None otherwise.

§Notes

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

Source

fn remove_entry<Q>(&mut self, key: &Q) -> Option<KVP<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 try_insert_or_update_with<FAdd, FUpdate>( &mut self, key: TKey, adder: FAdd, updater: FUpdate, ) -> Result<&mut TValue, HashTableError>
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.

§Errors

For details see HashTableError.

Source

fn iter_mut<'a>( &'a mut self, ) -> impl Iterator<Item = KVP<&'a TKey, &'a mut TValue>> + 'a
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 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.

§Panics

Whenever the corresponding MutableHashTable::try_insert would fail.

Source

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

Tries to insert given (TKey, TValue) pair into the table.

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

Otherwise returns the old TValue.

§Errors

For details see HashTableError.

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.

§Panics

Whenever the corresponding MutableHashTable::try_insert_or_update_with would fail.

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.try_get_or_insert_default(key).

§Panics

Whenever the corresponding MutableHashTable::try_get_or_insert_default would fail.

Source

fn try_get_or_insert_default( &mut self, key: TKey, ) -> Result<&mut TValue, HashTableError>
where TValue: Default,

Retrieves an existing TValue, or inserts a default one.

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

§Errors

For details see HashTableError.

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.try_get_or_insert(key, value).

§Panics

Whenever the corresponding MutableHashTable::try_get_or_insert would fail.

Source

fn try_get_or_insert( &mut self, key: TKey, value: TValue, ) -> Result<&mut TValue, HashTableError>

Retrieves an existing TValue, or inserts the passed one.

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

§Errors

For details see HashTableError.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

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 get_mut<Q>(&mut self, key: &Q) -> Option<&mut TValue>
where TKey: Borrow<Q>, Q: Eq + Hash + ?Sized,

Source§

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

Source§

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

Source§

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

Source§

fn iter_mut<'a>( &'a mut self, ) -> impl Iterator<Item = KVP<&'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, AbseilHashTable<TKey, TValue, DefaultAbseilConfig<TAllocator>>: MutableHashTable<TKey, TValue>,

Source§

impl<TKey, TValue, TConfig> MutableHashTable<TKey, TValue> for AbseilHashTable<TKey, TValue, TConfig>
where TKey: Eq + Hash, TConfig: AbseilConfig,

Source§

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