For a custom data structure I'm implementing, I'd like to be able to use something like the following interface for a hashbrown::HashMap
(equivalently std::collections::HashMap
):
// returns the value of `map.insert(key, val)` (wrapped in `Ok`)
// if this call would not cause `map` to reallocate
// if reallocation would occur, returns `Err(())`
fn try_insert(map: &mut HashMap<K, V, S>, key: K, val: V) -> Result<Option<V>, ()> {
unimplemented!()
}
I thought of comparing map.len()
to map.capacity()
, but the documentation says that map.capacity()
is "only a lower bound", so this would be inexact; is there a way to achieve it in a precise way? (Also if someone feels like explaining why the capacity is not exact I'd be interested in reading that -- otherwise I can dig into the Swiss Tables documentation I guess.) I'm willing to use the RawTable
interface if necessary.
(Why do I want this? Because I want to do some other stuff to my HashMap
-containing data structure every time reallocation needs to occur, including removing some elements for storage elsewhere.)