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

HashMap: #or_default_entry and #or_insert_entry #601

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

braddunbar
Copy link
Contributor

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());

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());
```
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

Successfully merging this pull request may close these issues.

1 participant