chashmap: add a version of upsert that returns the locked (mutable) reference
sometimes we want to change external values during an upsert (pseudo-code):
let mut is_one = false;
map.upsert(key, || { is_one = true; 1 }, |mut n| { n += 1; if n == 1 { is_one = true; } });
this code doesn't compile because is_one
is mutably borrowed more than once.
It's possible to workaround this awkwardly:
let mut is_one1 = false;
let mut is_one2 = false;
map.upsert(key, || { is_one1 = true; 1 }, |mut n| { n += 1; if n == 1 { is_one2 = true; }});
// look at both is_one1 and is_one2
if there's a way to return a WriteGuard<K, V>
with upsert that this can be solved more elegantly:
let is_one = {
let n = map.upsert(key, || 1, |mut n| n+= 1);
n == 1;
}