-
Notifications
You must be signed in to change notification settings - Fork 291
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
wip: allow providing the key at insertion time for EntryRef #579
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2984,10 +2984,10 @@ where | |
/// } | ||
/// assert!(map["b"] == 20 && map.len() == 2); | ||
/// ``` | ||
pub struct VacantEntryRef<'a, 'b, K, Q: ?Sized, V, S, A: Allocator = Global> { | ||
pub struct VacantEntryRef<'map, 'key, K, Q: ?Sized, V, S, A: Allocator = Global> { | ||
hash: u64, | ||
key: &'b Q, | ||
table: &'a mut HashMap<K, V, S, A>, | ||
key: &'key Q, | ||
table: &'map mut HashMap<K, V, S, A>, | ||
} | ||
|
||
impl<K, Q, V, S, A> Debug for VacantEntryRef<'_, '_, K, Q, V, S, A> | ||
|
@@ -4328,7 +4328,25 @@ impl<'a, 'b, K, Q: ?Sized, V: Default, S, A: Allocator> EntryRef<'a, 'b, K, Q, V | |
} | ||
} | ||
|
||
impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S, A> { | ||
impl<'map, 'key, K, V, S, A: Allocator> VacantEntryRef<'map, 'key, K, K, V, S, A> { | ||
/// insert, cloing the key | ||
#[cfg_attr(feature = "inline-more", inline)] | ||
pub fn insert_clone(self, value: V) -> &'map mut V | ||
where | ||
K: Hash + Clone, | ||
S: BuildHasher, | ||
{ | ||
let table = &mut self.table.table; | ||
let entry = table.insert_entry( | ||
self.hash, | ||
(self.key.clone(), value), | ||
make_hasher::<_, V, S>(&self.table.hash_builder), | ||
); | ||
&mut entry.1 | ||
} | ||
} | ||
|
||
impl<'map, 'key, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'map, 'key, K, Q, V, S, A> { | ||
/// Gets a reference to the key that would be used when inserting a value | ||
/// through the `VacantEntryRef`. | ||
/// | ||
|
@@ -4342,7 +4360,7 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S | |
/// assert_eq!(map.entry_ref(key).key(), "poneyland"); | ||
/// ``` | ||
#[cfg_attr(feature = "inline-more", inline)] | ||
pub fn key(&self) -> &'b Q { | ||
pub fn key(&self) -> &'key Q { | ||
self.key | ||
} | ||
|
||
|
@@ -4364,9 +4382,9 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S | |
/// assert_eq!(map["poneyland"], 37); | ||
/// ``` | ||
#[cfg_attr(feature = "inline-more", inline)] | ||
pub fn insert(self, value: V) -> &'a mut V | ||
pub fn insert(self, value: V) -> &'map mut V | ||
where | ||
K: Hash + From<&'b Q>, | ||
K: Hash + From<&'key Q>, | ||
S: BuildHasher, | ||
{ | ||
let table = &mut self.table.table; | ||
|
@@ -4378,6 +4396,24 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S | |
&mut entry.1 | ||
} | ||
|
||
/// 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 | ||
#[cfg_attr(feature = "inline-more", inline)] | ||
pub fn insert_kv(self, key: K, value: V) -> &'map mut V | ||
where | ||
K: Hash, | ||
for<'k> &'k K: PartialEq<&'key Q>, | ||
S: BuildHasher, | ||
{ | ||
let table = &mut self.table.table; | ||
assert!(&key == self.key); | ||
let entry = table.insert_entry( | ||
self.hash, | ||
(key, value), | ||
make_hasher::<_, V, S>(&self.table.hash_builder), | ||
); | ||
&mut entry.1 | ||
} | ||
|
||
/// Sets the value of the entry with the [`VacantEntryRef`]'s key, | ||
/// and returns an [`OccupiedEntry`]. | ||
/// | ||
|
@@ -4395,9 +4431,9 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S | |
/// } | ||
/// ``` | ||
#[cfg_attr(feature = "inline-more", inline)] | ||
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V, S, A> | ||
pub fn insert_entry(self, value: V) -> OccupiedEntry<'map, K, V, S, A> | ||
where | ||
K: Hash + From<&'b Q>, | ||
K: Hash + From<&'key Q>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not your problem, but this is an issue too, since downstream There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah i noticed that too, that was half the reason i asked about the assert |
||
S: BuildHasher, | ||
{ | ||
let elem = self.table.table.insert( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should use
Q: Equivalent<K>
instead, just likeentry_ref
did when it was created.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes that covers exactly what is needed there. had not seen that trait so far and tbh i have not read a lot of hashbrowns code, i just needed one of those methods to avoid either copying my key more than needed or doing a double lookup.