Skip to content

Commit 8bfe87c

Browse files
committed
proof of concept
1 parent e514afe commit 8bfe87c

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

src/map.rs

Lines changed: 45 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,25 @@ 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, V, S, A: Allocator> VacantEntryRef<'map, 'key, K, K, V, S, A> {
4336+
/// insert, cloing the key
4337+
#[cfg_attr(feature = "inline-more", inline)]
4338+
pub fn insert_clone(self, value: V) -> &'map mut V
4339+
where
4340+
K: Hash + Clone,
4341+
S: BuildHasher,
4342+
{
4343+
let table = &mut self.table.table;
4344+
let entry = table.insert_entry(
4345+
self.hash,
4346+
(self.key.clone(), value),
4347+
make_hasher::<_, V, S>(&self.table.hash_builder),
4348+
);
4349+
&mut entry.1
4350+
}
4351+
}
4352+
4353+
impl<'map, 'key, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'map, 'key, K, Q, V, S, A> {
43364354
/// Gets a reference to the key that would be used when inserting a value
43374355
/// through the `VacantEntryRef`.
43384356
///
@@ -4346,7 +4364,7 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
43464364
/// assert_eq!(map.entry_ref(key).key(), "poneyland");
43474365
/// ```
43484366
#[cfg_attr(feature = "inline-more", inline)]
4349-
pub fn key(&self) -> &'b Q {
4367+
pub fn key(&self) -> &'key Q {
43504368
self.key
43514369
}
43524370

@@ -4368,10 +4386,10 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
43684386
/// assert_eq!(map["poneyland"], 37);
43694387
/// ```
43704388
#[cfg_attr(feature = "inline-more", inline)]
4371-
pub fn insert(self, value: V) -> &'a mut V
4389+
pub fn insert(self, value: V) -> &'map mut V
43724390
where
43734391
K: Hash,
4374-
&'b Q: Into<K>,
4392+
&'key Q: Into<K>,
43754393
S: BuildHasher,
43764394
{
43774395
let table = &mut self.table.table;
@@ -4383,6 +4401,24 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
43834401
&mut entry.1
43844402
}
43854403

4404+
/// provide explicit key at insert-time instead of relying on there being effectively a from &K to K implementation and not working with cloneable values
4405+
#[cfg_attr(feature = "inline-more", inline)]
4406+
pub fn insert_kv(self, key: K, value: V) -> &'map mut V
4407+
where
4408+
K: Hash,
4409+
for<'k> &'k K: Equivalent<&'key Q>,
4410+
S: BuildHasher,
4411+
{
4412+
let table = &mut self.table.table;
4413+
assert!((&key).equivalent(&self.key));
4414+
let entry = table.insert_entry(
4415+
self.hash,
4416+
(key, value),
4417+
make_hasher::<_, V, S>(&self.table.hash_builder),
4418+
);
4419+
&mut entry.1
4420+
}
4421+
43864422
/// Sets the value of the entry with the [`VacantEntryRef`]'s key,
43874423
/// and returns an [`OccupiedEntry`].
43884424
///
@@ -4400,10 +4436,10 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S
44004436
/// }
44014437
/// ```
44024438
#[cfg_attr(feature = "inline-more", inline)]
4403-
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V, S, A>
4439+
pub fn insert_entry(self, value: V) -> OccupiedEntry<'map, K, V, S, A>
44044440
where
44054441
K: Hash,
4406-
&'b Q: Into<K>,
4442+
&'key Q: Into<K>,
44074443
S: BuildHasher,
44084444
{
44094445
let elem = self.table.table.insert(

0 commit comments

Comments
 (0)