Skip to content

Commit 760eb3e

Browse files
committed
Add insert_with_key, allowing the user to provide the key at insertion time.
1 parent e514afe commit 760eb3e

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/map.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,10 +2983,10 @@ where
29832983
/// }
29842984
/// assert!(map["b"] == 20 && map.len() == 2);
29852985
/// ```
2986-
pub struct VacantEntryRef<'a, 'b, K, Q: ?Sized, V, S, A: Allocator = Global> {
2986+
pub struct VacantEntryRef<'map, 'key, K, Q: ?Sized, V, S, A: Allocator = Global> {
29872987
hash: u64,
2988-
key: &'b Q,
2989-
table: &'a mut HashMap<K, V, S, A>,
2988+
key: &'key Q,
2989+
table: &'map mut HashMap<K, V, S, A>,
29902990
}
29912991

29922992
impl<K, Q, V, S, A> Debug for VacantEntryRef<'_, '_, K, Q, V, S, A>
@@ -4332,7 +4332,7 @@ impl<'a, 'b, K, Q: ?Sized, V: Default, S, A: Allocator> EntryRef<'a, 'b, K, Q, V
43324332
}
43334333
}
43344334

4335-
impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S, A> {
4335+
impl<'map, 'key, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'map, 'key, K, Q, V, S, A> {
43364336
/// Gets a reference to the key that would be used when inserting a value
43374337
/// through the `VacantEntryRef`.
43384338
///
@@ -4346,7 +4346,7 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
43464346
/// assert_eq!(map.entry_ref(key).key(), "poneyland");
43474347
/// ```
43484348
#[cfg_attr(feature = "inline-more", inline)]
4349-
pub fn key(&self) -> &'b Q {
4349+
pub fn key(&self) -> &'key Q {
43504350
self.key
43514351
}
43524352

@@ -4368,10 +4368,10 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
43684368
/// assert_eq!(map["poneyland"], 37);
43694369
/// ```
43704370
#[cfg_attr(feature = "inline-more", inline)]
4371-
pub fn insert(self, value: V) -> &'a mut V
4371+
pub fn insert(self, value: V) -> &'map mut V
43724372
where
43734373
K: Hash,
4374-
&'b Q: Into<K>,
4374+
&'key Q: Into<K>,
43754375
S: BuildHasher,
43764376
{
43774377
let table = &mut self.table.table;
@@ -4383,6 +4383,27 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
43834383
&mut entry.1
43844384
}
43854385

4386+
/// [insert](Self::insert) a value by providing the key at insertion time.
4387+
/// Returns a mutable reference to the inserted value.
4388+
///
4389+
/// Useful if Into is not implemented for converting from &Q to K.
4390+
#[cfg_attr(feature = "inline-more", inline)]
4391+
pub fn insert_with_key(self, key: K, value: V) -> &'map mut V
4392+
where
4393+
K: Hash,
4394+
Q: Equivalent<K>,
4395+
S: BuildHasher,
4396+
{
4397+
let table = &mut self.table.table;
4398+
assert!((&self.key).equivalent(&key));
4399+
let entry = table.insert_entry(
4400+
self.hash,
4401+
(key, value),
4402+
make_hasher::<_, V, S>(&self.table.hash_builder),
4403+
);
4404+
&mut entry.1
4405+
}
4406+
43864407
/// Sets the value of the entry with the [`VacantEntryRef`]'s key,
43874408
/// and returns an [`OccupiedEntry`].
43884409
///
@@ -4400,10 +4421,10 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
44004421
/// }
44014422
/// ```
44024423
#[cfg_attr(feature = "inline-more", inline)]
4403-
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V, S, A>
4424+
pub fn insert_entry(self, value: V) -> OccupiedEntry<'map, K, V, S, A>
44044425
where
44054426
K: Hash,
4406-
&'b Q: Into<K>,
4427+
&'key Q: Into<K>,
44074428
S: BuildHasher,
44084429
{
44094430
let elem = self.table.table.insert(

0 commit comments

Comments
 (0)