diff --git a/src/map.rs b/src/map.rs index 0cffb9dd8..7e8bbd5d7 100644 --- a/src/map.rs +++ b/src/map.rs @@ -2983,10 +2983,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, + key: &'key Q, + table: &'map mut HashMap, } impl 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 } } -impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S, A> { +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`. /// @@ -4346,7 +4346,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 } @@ -4368,10 +4368,10 @@ 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, - &'b Q: Into, + &'key Q: Into, S: BuildHasher, { 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 &mut entry.1 } + /// [insert](Self::insert) a value by providing the key at insertion time. + /// Returns a mutable reference to the inserted value. + /// + /// Useful if Into is not implemented for converting from &Q to K. + #[cfg_attr(feature = "inline-more", inline)] + pub fn insert_with_key(self, key: K, value: V) -> &'map mut V + where + K: Hash, + Q: Equivalent, + S: BuildHasher, + { + let table = &mut self.table.table; + assert!((self.key).equivalent(&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`]. /// @@ -4400,10 +4421,10 @@ 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, - &'b Q: Into, + &'key Q: Into, S: BuildHasher, { let elem = self.table.table.insert(