Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give back references to keys #34

Open
g2p opened this issue Dec 14, 2022 · 2 comments
Open

Give back references to keys #34

g2p opened this issue Dec 14, 2022 · 2 comments

Comments

@g2p
Copy link
Contributor

g2p commented Dec 14, 2022

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
Copy link
Owner

Ought to be fine as long as we appropriately guard things

g2p added a commit to g2p/elsa that referenced this issue Dec 14, 2022
This returns references to both key and value with the map lifetime.

Addresses <Manishearth#34>.
@g2p
Copy link
Contributor Author

g2p commented Dec 14, 2022

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 rust-lang/rust#65225 (comment) “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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants