@@ -1761,8 +1761,17 @@ where
1761
1761
/// Insert a key-value pair into the map without checking
1762
1762
/// if the key already exists in the map.
1763
1763
///
1764
+ /// This operation is faster than regular insert, because it does not perform
1765
+ /// lookup before insertion.
1766
+ ///
1767
+ /// This operation is useful during initial population of the map.
1768
+ /// For example, when constructing a map from another map, we know
1769
+ /// that keys are unique.
1770
+ ///
1764
1771
/// Returns a reference to the key and value just inserted.
1765
1772
///
1773
+ /// # Safety
1774
+ ///
1766
1775
/// This operation is safe if a key does not exist in the map.
1767
1776
///
1768
1777
/// However, if a key exists in the map already, the behavior is unspecified:
@@ -1772,12 +1781,9 @@ where
1772
1781
/// That said, this operation (and following operations) are guaranteed to
1773
1782
/// not violate memory safety.
1774
1783
///
1775
- /// This operation is faster than regular insert, because it does not perform
1776
- /// lookup before insertion.
1777
- ///
1778
- /// This operation is useful during initial population of the map.
1779
- /// For example, when constructing a map from another map, we know
1780
- /// that keys are unique.
1784
+ /// However this operation is still unsafe because the resulting `HashMap`
1785
+ /// may be passed to unsafe code which does expect the map to behave
1786
+ /// correctly, and would could unsoundness as a result.
1781
1787
///
1782
1788
/// # Examples
1783
1789
///
@@ -1793,10 +1799,12 @@ where
1793
1799
/// let mut map2 = HashMap::new();
1794
1800
///
1795
1801
/// for (key, value) in map1.into_iter() {
1796
- /// map2.insert_unique_unchecked(key, value);
1802
+ /// unsafe {
1803
+ /// map2.insert_unique_unchecked(key, value);
1804
+ /// }
1797
1805
/// }
1798
1806
///
1799
- /// let (key, value) = map2.insert_unique_unchecked(4, "d");
1807
+ /// let (key, value) = unsafe { map2.insert_unique_unchecked(4, "d") } ;
1800
1808
/// assert_eq!(key, &4);
1801
1809
/// assert_eq!(value, &mut "d");
1802
1810
/// *value = "e";
@@ -1808,7 +1816,7 @@ where
1808
1816
/// assert_eq!(map2.len(), 4);
1809
1817
/// ```
1810
1818
#[ cfg_attr( feature = "inline-more" , inline) ]
1811
- pub fn insert_unique_unchecked ( & mut self , k : K , v : V ) -> ( & K , & mut V ) {
1819
+ pub unsafe fn insert_unique_unchecked ( & mut self , k : K , v : V ) -> ( & K , & mut V ) {
1812
1820
let hash = make_hash :: < K , S > ( & self . hash_builder , & k) ;
1813
1821
let bucket = self
1814
1822
. table
@@ -3187,7 +3195,7 @@ impl<'a, K, V, S, A: Allocator> IntoIterator for &'a HashMap<K, V, S, A> {
3187
3195
///
3188
3196
/// for (key, value) in &map_one {
3189
3197
/// println!("Key: {}, Value: {}", key, value);
3190
- /// map_two.insert_unique_unchecked (*key, *value);
3198
+ /// map_two.insert (*key, *value);
3191
3199
/// }
3192
3200
///
3193
3201
/// assert_eq!(map_one, map_two);
@@ -5710,9 +5718,9 @@ mod test_map {
5710
5718
#[ test]
5711
5719
fn test_insert_unique_unchecked ( ) {
5712
5720
let mut map = HashMap :: new ( ) ;
5713
- let ( k1, v1) = map. insert_unique_unchecked ( 10 , 11 ) ;
5721
+ let ( k1, v1) = unsafe { map. insert_unique_unchecked ( 10 , 11 ) } ;
5714
5722
assert_eq ! ( ( & 10 , & mut 11 ) , ( k1, v1) ) ;
5715
- let ( k2, v2) = map. insert_unique_unchecked ( 20 , 21 ) ;
5723
+ let ( k2, v2) = unsafe { map. insert_unique_unchecked ( 20 , 21 ) } ;
5716
5724
assert_eq ! ( ( & 20 , & mut 21 ) , ( k2, v2) ) ;
5717
5725
assert_eq ! ( Some ( & 11 ) , map. get( & 10 ) ) ;
5718
5726
assert_eq ! ( Some ( & 21 ) , map. get( & 20 ) ) ;
0 commit comments