Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HashMap: #or_default_entry and #or_insert_entry
I often find myself in a situation where I would like to insert a key into a map via `#entry_ref` and then use the key from the resulting entry. ``` // Get a byte slice from a buffer let key = client.request.get(index); // Insert the value (default) let mut entry = match queues.entry_ref(&key) { EntryRef::Occupied(entry) => entry, EntryRef::Vacant(entry) => entry.insert_entry(Default::default()), }; // Use the value entry.get_mut().insert_back(client.id); // Reuse the key instead of copying the bytes again keys.insert(client.id, entry.key()); ``` This is common enough that I'd love to have functions for it, similar to `insert_entry`. ``` // Get a byte slice from a buffer let key = client.request.get(index); // Insert the value (default) let mut entry = queues.entry_ref(&key).or_default_entry(); // Use the value entry.get_mut().insert_back(client.id); // Reuse the key instead of copying the bytes again keys.insert(client.id, entry.key()); ```
- Loading branch information