I have a use case where I need to do the following things on a BTreeMap
- Start with a reference to a key
- Get or insert default and modify the value
- Get a reference to the key that is in the HashMap (it's a
unicase::Ascii
, so potentially slightly different, I want to preserve the first case specified)
Maybe I'm missing something in the entry API but I can't seem to do this without constructing two owned keys.
type Map = BTreeMap<Ascii<String>, Vec<&'static str>>;
fn do_thing(map: &mut Map, key: &str, value: &'static str) -> String {
let values = map.entry(Ascii::new(key.to_owned())).or_default();
// check if we need to insert particular value
let i = match values.iter().position(|v| *v == value) {
Some(i) => i,
None => {
let i = values.len();
values.push(value);
i
}
};
// now I need the key, in it's originally specified case
let (orig_key, _) = map.get_key_value(&Ascii::new(key.to_owned())).unwrap();
// Do something with original key and i
// ...
format!("{}-{}", orig_key, i)
}