elsa icon indicating copy to clipboard operation
elsa copied to clipboard

Give back references to keys

Open g2p opened this issue 1 year ago • 2 comments

FrozenMap hands out references to its values, but since the map also owns the keys it is possible to provide references to the keys as well.

Assuming K: StableDeref, implement:

fn insert_key_value(self, k: K, v: V) -> (&K::Target, &V::Target);
fn get_key_value(…) -> (&K::Target, &V::Target);

g2p avatar Dec 14 '22 09:12 g2p

Ought to be fine as long as we appropriately guard things

Manishearth avatar Dec 14 '22 10:12 Manishearth

There's a PR for get_key_value at #35.

I couldn't do insert_key_value, getting a key with the map lifetime from hash_map::Entry can't be done in one go / without cloning, see https://github.com/rust-lang/rust/issues/65225#issuecomment-1066442440 “On the topic of key ownership…” for a discussion on what needs to be done before something like that could be stabilized.

OccupiedEntry needs to keep a non-Optional reference to the key within the map.

g2p avatar Dec 14 '22 14:12 g2p