Manishearth / elsa

Append-only collections for Rust where borrows to entries can outlive insertions
Apache License 2.0
228 stars 33 forks source link

Give back references to keys #34

Open g2p opened 1 year ago

g2p commented 1 year ago

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);
Manishearth commented 1 year ago

Ought to be fine as long as we appropriately guard things

g2p commented 1 year ago

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.