-
Notifications
You must be signed in to change notification settings - Fork 291
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
Add immutable map accessors to OccupiedEntry and VacantEntry #600
base: master
Are you sure you want to change the base?
Conversation
Without these, it is not possible to inspect other keys in the map while deciding how to process the entry, forcing the user to either perform unnecessary checks before calling entry or performing multiple map lookups for the entry key. For example: ```rust fn some_expensive_operation(...) -> bool { ... } // old way, with two lookups for `key` if !map.contains_key(key) && some_expensive_operation(map.get(other_key)) { map.insert(key, val); } // old way, with expensive operation done unnecessarily when key is // already present if some_expensive_operation(map.get(other_key)) { if let VacantEntry(e) = map.entry(key) { e.insert(val); } } // new way, with one lookup for `key` if let VacantEntry(e) = map.entry(key) { if some_expensive_operation(e.map().get(other_key)) { e.insert(val); } } ``` We do not provide accessors returning a mutable reference to the map because doing so would make it possible to invalidate the entry itself.
This feels sound and reasonable to add, but I'll defer to others on whether that's actually the case. I would be interested in some more specifics on why you're looking to do this, since this doesn't quite feel like the best way of going about this, but is probably fine. Maybe an entry form of |
That would be fine too.
While deciding whether to insert a new entry into a map, I need to do as many as four other lookups, each accompanied by some additional, potentially expensive processing on the corresponding values, if they are present at all. Right now I just do two lookups for the to-be-inserted key ( |
I guess that my confusion lies in why you specifically need the different things being checked to all exist within the map as separate entries, rather than all lumped together in a single entry, if you want to access them all at once. |
Could these be renamed to Also I can see a use case for
That won't work since an invariant of the entry API is that only one entry may exist at any time. Otherwise methods on one entry would invalidate the others. |
Also could you add these to:
|
Is it possible to do this for |
Actually the API as it is will certainly cause problems in the future when we want to rewrite Perhaps the API could be re-considered: instead of a returning a reference the the map, there could be a method which explicitly performs a lookup. |
This makes sense, though it's unfortunate that each method of interest would have to be explicitly exposed here. I will update this PR with that approach shortly, exposing only |
Without these, it is not possible to inspect other keys in the map while deciding how to process the entry, forcing the user to either perform unnecessary work before calling entry or performing multiple map lookups for the entry key.
For example:
We do not provide accessors returning a mutable reference to the map because doing so would make it possible to invalidate the entry itself.