pub trait MutableHashTable<TKey, TValue>: ImmutableHashTable<TKey, TValue>{
// 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§
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.
Sourcefn remove_entry<Q>(&mut self, key: &Q) -> Option<(TKey, TValue)>
fn remove_entry<Q>(&mut self, key: &Q) -> Option<(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 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.
Provided Methods§
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.insert_or_update_with(key, TValue::default, |_| {}).
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.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>
Available on crate feature std only.
impl<TKey, TValue> MutableHashTable<TKey, TValue> for HashMap<TKey, TValue>
std only.