@@ -1433,6 +1433,33 @@ where
1433
1433
}
1434
1434
1435
1435
impl < ' a , K , V , S > RawEntryMut < ' a , K , V , S > {
1436
+ /// Sets the value of the entry, and returns a RawOccupiedEntryMut.
1437
+ ///
1438
+ /// # Examples
1439
+ ///
1440
+ /// ```
1441
+ /// use hashbrown::HashMap;
1442
+ ///
1443
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
1444
+ /// let entry = map.raw_entry_mut().from_key("horseyland").insert("horseyland", 37);
1445
+ ///
1446
+ /// assert_eq!(entry.remove_entry(), ("horseyland", 37));
1447
+ /// ```
1448
+ #[ inline]
1449
+ pub fn insert ( self , key : K , value : V ) -> RawOccupiedEntryMut < ' a , K , V >
1450
+ where
1451
+ K : Hash ,
1452
+ S : BuildHasher ,
1453
+ {
1454
+ match self {
1455
+ RawEntryMut :: Occupied ( mut entry) => {
1456
+ entry. insert ( value) ;
1457
+ entry
1458
+ }
1459
+ RawEntryMut :: Vacant ( entry) => entry. insert_entry ( key, value) ,
1460
+ }
1461
+ }
1462
+
1436
1463
/// Ensures a value is in the entry by inserting the default if empty, and returns
1437
1464
/// mutable references to the key and value in the entry.
1438
1465
///
@@ -1674,6 +1701,25 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
1674
1701
( k, v)
1675
1702
}
1676
1703
}
1704
+
1705
+ #[ inline]
1706
+ fn insert_entry ( self , key : K , value : V ) -> RawOccupiedEntryMut < ' a , K , V >
1707
+ where
1708
+ K : Hash ,
1709
+ S : BuildHasher ,
1710
+ {
1711
+ let hash_builder = self . hash_builder ;
1712
+ let mut hasher = self . hash_builder . build_hasher ( ) ;
1713
+ key. hash ( & mut hasher) ;
1714
+
1715
+ let elem = self . table . insert ( hasher. finish ( ) , ( key, value) , |k| {
1716
+ make_hash ( hash_builder, & k. 0 )
1717
+ } ) ;
1718
+ RawOccupiedEntryMut {
1719
+ elem,
1720
+ table : self . table ,
1721
+ }
1722
+ }
1677
1723
}
1678
1724
1679
1725
impl < K , V , S > Debug for RawEntryBuilderMut < ' _ , K , V , S > {
@@ -2018,6 +2064,33 @@ where
2018
2064
}
2019
2065
2020
2066
impl < ' a , K , V , S > Entry < ' a , K , V , S > {
2067
+ /// Sets the value of the entry, and returns an OccupiedEntry.
2068
+ ///
2069
+ /// # Examples
2070
+ ///
2071
+ /// ```
2072
+ /// use hashbrown::HashMap;
2073
+ ///
2074
+ /// let mut map: HashMap<&str, u32> = HashMap::new();
2075
+ /// let entry = map.entry("horseyland").insert(37);
2076
+ ///
2077
+ /// assert_eq!(entry.key(), &"horseyland");
2078
+ /// ```
2079
+ #[ inline]
2080
+ pub fn insert ( self , value : V ) -> OccupiedEntry < ' a , K , V , S >
2081
+ where
2082
+ K : Hash ,
2083
+ S : BuildHasher ,
2084
+ {
2085
+ match self {
2086
+ Entry :: Occupied ( mut entry) => {
2087
+ entry. insert ( value) ;
2088
+ entry
2089
+ }
2090
+ Entry :: Vacant ( entry) => entry. insert_entry ( value) ,
2091
+ }
2092
+ }
2093
+
2021
2094
/// Ensures a value is in the entry by inserting the default if empty, and returns
2022
2095
/// a mutable reference to the value in the entry.
2023
2096
///
@@ -2449,6 +2522,23 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
2449
2522
} ) ;
2450
2523
unsafe { & mut bucket. as_mut ( ) . 1 }
2451
2524
}
2525
+
2526
+ #[ inline]
2527
+ fn insert_entry ( self , value : V ) -> OccupiedEntry < ' a , K , V , S >
2528
+ where
2529
+ K : Hash ,
2530
+ S : BuildHasher ,
2531
+ {
2532
+ let hash_builder = & self . table . hash_builder ;
2533
+ let elem = self . table . table . insert ( self . hash , ( self . key , value) , |x| {
2534
+ make_hash ( hash_builder, & x. 0 )
2535
+ } ) ;
2536
+ OccupiedEntry {
2537
+ key : None ,
2538
+ elem,
2539
+ table : self . table ,
2540
+ }
2541
+ }
2452
2542
}
2453
2543
2454
2544
impl < K , V , S > FromIterator < ( K , V ) > for HashMap < K , V , S >
0 commit comments