@@ -1281,6 +1281,8 @@ where
12811281 /// Insert a key-value pair into the map without checking
12821282 /// if the key already exists in the map.
12831283 ///
1284+ /// Returns a reference to the key and value just inserted.
1285+ ///
12841286 /// This operation is safe if a key does not exist in the map.
12851287 ///
12861288 /// However, if a key exists in the map already, the behavior is unspecified:
@@ -1297,10 +1299,13 @@ where
12971299 /// For example, when constructing a map from another map, we know
12981300 /// that keys are unique.
12991301 #[ cfg_attr( feature = "inline-more" , inline) ]
1300- pub fn insert_unique_unchecked ( & mut self , k : K , v : V ) {
1302+ pub fn insert_unique_unchecked ( & mut self , k : K , v : V ) -> ( & K , & mut V ) {
13011303 let hash = make_insert_hash :: < K , S > ( & self . hash_builder , & k) ;
1302- self . table
1304+ let bucket = self
1305+ . table
13031306 . insert ( hash, ( k, v) , make_hasher :: < K , _ , V , S > ( & self . hash_builder ) ) ;
1307+ let ( k, v) = unsafe { bucket. as_mut ( ) } ;
1308+ ( k, v)
13041309 }
13051310
13061311 /// Tries to insert a key-value pair into the map, and returns
@@ -3926,8 +3931,10 @@ mod test_map {
39263931 #[ test]
39273932 fn test_insert_unique_unchecked ( ) {
39283933 let mut map = HashMap :: new ( ) ;
3929- map. insert_unique_unchecked ( 10 , 11 ) ;
3930- map. insert_unique_unchecked ( 20 , 21 ) ;
3934+ let ( k, v) = map. insert_unique_unchecked ( 10 , 11 ) ;
3935+ assert_eq ! ( ( & 10 , & mut 11 ) , ( k, v) ) ;
3936+ let ( k, v) = map. insert_unique_unchecked ( 20 , 21 ) ;
3937+ assert_eq ! ( ( & 20 , & mut 21 ) , ( k, v) ) ;
39313938 assert_eq ! ( Some ( & 11 ) , map. get( & 10 ) ) ;
39323939 assert_eq ! ( Some ( & 21 ) , map. get( & 20 ) ) ;
39333940 assert_eq ! ( None , map. get( & 30 ) ) ;
0 commit comments