pub trait MutableHashTable<TKey, TValue>: ImmutableHashTable<TKey, TValue>{
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§
Sourcefn get_mut<Q>(&mut self, key: &Q) -> Option<&mut TValue>
fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut TValue>
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.
Sourcefn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<KVP<&TKey, &mut TValue>>
fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<KVP<&TKey, &mut TValue>>
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.
Sourcefn remove_entry<Q>(&mut self, key: &Q) -> Option<KVP<TKey, TValue>>
fn remove_entry<Q>(&mut self, key: &Q) -> Option<KVP<TKey, TValue>>
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.
Sourcefn try_insert_or_update_with<FAdd, FUpdate>(
&mut self,
key: TKey,
adder: FAdd,
updater: FUpdate,
) -> Result<&mut TValue, HashTableError>
fn try_insert_or_update_with<FAdd, FUpdate>( &mut self, key: TKey, adder: FAdd, updater: FUpdate, ) -> Result<&mut TValue, HashTableError>
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.
Sourcefn iter_mut<'a>(
&'a mut self,
) -> impl Iterator<Item = KVP<&'a TKey, &'a mut TValue>> + 'awhere
TKey: 'a,
TValue: 'a,
Self: 'a,
fn iter_mut<'a>(
&'a mut self,
) -> impl Iterator<Item = KVP<&'a TKey, &'a mut TValue>> + 'awhere
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§
Sourcefn insert(&mut self, key: TKey, value: TValue) -> Option<TValue>
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.
Sourcefn try_insert(
&mut self,
key: TKey,
value: TValue,
) -> Result<Option<TValue>, HashTableError>
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.
Sourcefn insert_or_update_with<FAdd, FUpdate>(
&mut self,
key: TKey,
adder: FAdd,
updater: FUpdate,
) -> &mut TValue
fn insert_or_update_with<FAdd, FUpdate>( &mut self, key: TKey, adder: FAdd, updater: FUpdate, ) -> &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.
Sourcefn remove<Q>(&mut self, key: &Q) -> Option<TValue>
fn remove<Q>(&mut self, key: &Q) -> Option<TValue>
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.
Sourcefn get_or_insert_default(&mut self, key: TKey) -> &mut TValuewhere
TValue: Default,
fn get_or_insert_default(&mut self, key: TKey) -> &mut TValuewhere
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.
Sourcefn try_get_or_insert_default(
&mut self,
key: TKey,
) -> Result<&mut TValue, HashTableError>where
TValue: Default,
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.
Sourcefn get_or_insert(&mut self, key: TKey, value: TValue) -> &mut TValue
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.
Sourcefn try_get_or_insert(
&mut self,
key: TKey,
value: TValue,
) -> Result<&mut TValue, HashTableError>
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>
Available on crate feature std only.
impl<TKey, TValue> MutableHashTable<TKey, TValue> for HashMap<TKey, TValue>
std only.