Commit 5b9c757
committed
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());
```1 parent 25365fc commit 5b9c757
1 file changed
+65
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3527 | 3527 | | |
3528 | 3528 | | |
3529 | 3529 | | |
| 3530 | + | |
| 3531 | + | |
| 3532 | + | |
| 3533 | + | |
| 3534 | + | |
| 3535 | + | |
| 3536 | + | |
| 3537 | + | |
| 3538 | + | |
| 3539 | + | |
| 3540 | + | |
| 3541 | + | |
| 3542 | + | |
| 3543 | + | |
| 3544 | + | |
| 3545 | + | |
| 3546 | + | |
| 3547 | + | |
| 3548 | + | |
| 3549 | + | |
| 3550 | + | |
| 3551 | + | |
| 3552 | + | |
| 3553 | + | |
| 3554 | + | |
| 3555 | + | |
| 3556 | + | |
| 3557 | + | |
| 3558 | + | |
| 3559 | + | |
| 3560 | + | |
| 3561 | + | |
3530 | 3562 | | |
3531 | 3563 | | |
3532 | 3564 | | |
| |||
4328 | 4360 | | |
4329 | 4361 | | |
4330 | 4362 | | |
| 4363 | + | |
| 4364 | + | |
| 4365 | + | |
| 4366 | + | |
| 4367 | + | |
| 4368 | + | |
| 4369 | + | |
| 4370 | + | |
| 4371 | + | |
| 4372 | + | |
| 4373 | + | |
| 4374 | + | |
| 4375 | + | |
| 4376 | + | |
| 4377 | + | |
| 4378 | + | |
| 4379 | + | |
| 4380 | + | |
| 4381 | + | |
| 4382 | + | |
| 4383 | + | |
| 4384 | + | |
| 4385 | + | |
| 4386 | + | |
| 4387 | + | |
| 4388 | + | |
| 4389 | + | |
| 4390 | + | |
| 4391 | + | |
| 4392 | + | |
| 4393 | + | |
| 4394 | + | |
| 4395 | + | |
4331 | 4396 | | |
4332 | 4397 | | |
4333 | 4398 | | |
| |||
0 commit comments