Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add immutable map accessors to OccupiedEntry and VacantEntry
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.
- Loading branch information