Open eira-fransham opened 6 years ago
can this not be done with something like the following?
let was_updated = match map.entry(key) {
Entry::Occupied(ref entry) if !new_val.better_than(entry.get()) => false,
(entry) _ => { entry.insert(new_val); true }
};
If it cannot be done, then this could be a good solution
The insert
calls have different type signatures, so although I don't know what the (entry) _
syntax is it wouldn't work either way.
Sounds reasonable. This is the small kind of improvement that you can normally contribute as a PR and the libs team will discuss it from there. User can fix it locally in the mean time:
use std::collections::hash_map::Entry;
pub trait InsertExt {
type Item;
fn insert(self, item: Self::Item);
}
impl<'a, K, V> InsertExt for Entry<'a, K, V> {
type Item = V;
fn insert(self, item: Self::Item) {
match self {
Entry::Occupied(mut e) => { e.insert(item); }
Entry::Vacant(e) => { e.insert(item); }
}
}
}
I'm currently in a position of wanting an
Entry::insert
(or other bikesheddable name) for when you want to conditionally update a value - inserting a value if there is none and updating it if it exists but is "worse" by some metric. This would be nicely captured with code similar to the following:Instead, you have to do:
Which is redundant. The two
insert
methods have different signatures but I would imagine thatEntry::insert
would return anResult<&mut Value, Value>
to pass ownership in the case that the entry is occupied (or maybe aCowMut
if that was to exist).