diff --git a/src/map.rs b/src/map.rs index a5b802179c..07d7ba72da 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1281,6 +1281,8 @@ where /// Insert a key-value pair into the map without checking /// if the key already exists in the map. /// + /// Returns a reference to the key and value just inserted. + /// /// This operation is safe if a key does not exist in the map. /// /// However, if a key exists in the map already, the behavior is unspecified: @@ -1297,10 +1299,13 @@ where /// For example, when constructing a map from another map, we know /// that keys are unique. #[cfg_attr(feature = "inline-more", inline)] - pub fn insert_unique_unchecked(&mut self, k: K, v: V) { + pub fn insert_unique_unchecked(&mut self, k: K, v: V) -> (&K, &mut V) { let hash = make_insert_hash::(&self.hash_builder, &k); - self.table + let bucket = self + .table .insert(hash, (k, v), make_hasher::(&self.hash_builder)); + let (k, v) = unsafe { bucket.as_mut() }; + (k, v) } /// Tries to insert a key-value pair into the map, and returns @@ -3926,8 +3931,10 @@ mod test_map { #[test] fn test_insert_unique_unchecked() { let mut map = HashMap::new(); - map.insert_unique_unchecked(10, 11); - map.insert_unique_unchecked(20, 21); + let (k, v) = map.insert_unique_unchecked(10, 11); + assert_eq!((&10, &mut 11), (k, v)); + let (k, v) = map.insert_unique_unchecked(20, 21); + assert_eq!((&20, &mut 21), (k, v)); assert_eq!(Some(&11), map.get(&10)); assert_eq!(Some(&21), map.get(&20)); assert_eq!(None, map.get(&30)); diff --git a/src/set.rs b/src/set.rs index a6bc4c271c..282d17bdb6 100644 --- a/src/set.rs +++ b/src/set.rs @@ -993,6 +993,8 @@ where /// Insert a value the set without checking if the value already exists in the set. /// + /// Returns a reference to the value just inserted. + /// /// This operation is safe if a value does not exist in the set. /// /// However, if a value exists in the set already, the behavior is unspecified: @@ -1009,8 +1011,8 @@ where /// For example, when constructing a set from another set, we know /// that values are unique. #[cfg_attr(feature = "inline-more", inline)] - pub fn insert_unique_unchecked(&mut self, value: T) { - self.map.insert_unique_unchecked(value, ()); + pub fn insert_unique_unchecked(&mut self, value: T) -> &T { + self.map.insert_unique_unchecked(value, ()).0 } /// Adds a value to the set, replacing the existing value, if any, that is equal to the given