@@ -1281,6 +1281,8 @@ where
1281
1281
/// Insert a key-value pair into the map without checking
1282
1282
/// if the key already exists in the map.
1283
1283
///
1284
+ /// Returns a reference to the key and value just inserted.
1285
+ ///
1284
1286
/// This operation is safe if a key does not exist in the map.
1285
1287
///
1286
1288
/// However, if a key exists in the map already, the behavior is unspecified:
@@ -1297,10 +1299,13 @@ where
1297
1299
/// For example, when constructing a map from another map, we know
1298
1300
/// that keys are unique.
1299
1301
#[ 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 ) {
1301
1303
let hash = make_insert_hash :: < K , S > ( & self . hash_builder , & k) ;
1302
- self . table
1304
+ let bucket = self
1305
+ . table
1303
1306
. insert ( hash, ( k, v) , make_hasher :: < K , _ , V , S > ( & self . hash_builder ) ) ;
1307
+ let ( k_ref, v_ref) = unsafe { bucket. as_mut ( ) } ;
1308
+ ( k_ref, v_ref)
1304
1309
}
1305
1310
1306
1311
/// Tries to insert a key-value pair into the map, and returns
@@ -3926,8 +3931,10 @@ mod test_map {
3926
3931
#[ test]
3927
3932
fn test_insert_unique_unchecked ( ) {
3928
3933
let mut map = HashMap :: new ( ) ;
3929
- map. insert_unique_unchecked ( 10 , 11 ) ;
3930
- map. insert_unique_unchecked ( 20 , 21 ) ;
3934
+ let ( k1, v1) = map. insert_unique_unchecked ( 10 , 11 ) ;
3935
+ assert_eq ! ( ( & 10 , & mut 11 ) , ( k1, v1) ) ;
3936
+ let ( k2, v2) = map. insert_unique_unchecked ( 20 , 21 ) ;
3937
+ assert_eq ! ( ( & 20 , & mut 21 ) , ( k2, v2) ) ;
3931
3938
assert_eq ! ( Some ( & 11 ) , map. get( & 10 ) ) ;
3932
3939
assert_eq ! ( Some ( & 21 ) , map. get( & 20 ) ) ;
3933
3940
assert_eq ! ( None , map. get( & 30 ) ) ;
0 commit comments