@@ -1771,8 +1771,17 @@ where
1771
1771
/// Insert a key-value pair into the map without checking
1772
1772
/// if the key already exists in the map.
1773
1773
///
1774
+ /// This operation is faster than regular insert, because it does not perform
1775
+ /// lookup before insertion.
1776
+ ///
1777
+ /// This operation is useful during initial population of the map.
1778
+ /// For example, when constructing a map from another map, we know
1779
+ /// that keys are unique.
1780
+ ///
1774
1781
/// Returns a reference to the key and value just inserted.
1775
1782
///
1783
+ /// # Safety
1784
+ ///
1776
1785
/// This operation is safe if a key does not exist in the map.
1777
1786
///
1778
1787
/// However, if a key exists in the map already, the behavior is unspecified:
@@ -1782,12 +1791,9 @@ where
1782
1791
/// That said, this operation (and following operations) are guaranteed to
1783
1792
/// not violate memory safety.
1784
1793
///
1785
- /// This operation is faster than regular insert, because it does not perform
1786
- /// lookup before insertion.
1787
- ///
1788
- /// This operation is useful during initial population of the map.
1789
- /// For example, when constructing a map from another map, we know
1790
- /// that keys are unique.
1794
+ /// However this operation is still unsafe because the resulting `HashMap`
1795
+ /// may be passed to unsafe code which does expect the map to behave
1796
+ /// correctly, and would cause unsoundness as a result.
1791
1797
///
1792
1798
/// # Examples
1793
1799
///
@@ -1803,10 +1809,12 @@ where
1803
1809
/// let mut map2 = HashMap::new();
1804
1810
///
1805
1811
/// for (key, value) in map1.into_iter() {
1806
- /// map2.insert_unique_unchecked(key, value);
1812
+ /// unsafe {
1813
+ /// map2.insert_unique_unchecked(key, value);
1814
+ /// }
1807
1815
/// }
1808
1816
///
1809
- /// let (key, value) = map2.insert_unique_unchecked(4, "d");
1817
+ /// let (key, value) = unsafe { map2.insert_unique_unchecked(4, "d") } ;
1810
1818
/// assert_eq!(key, &4);
1811
1819
/// assert_eq!(value, &mut "d");
1812
1820
/// *value = "e";
@@ -1818,7 +1826,7 @@ where
1818
1826
/// assert_eq!(map2.len(), 4);
1819
1827
/// ```
1820
1828
#[ cfg_attr( feature = "inline-more" , inline) ]
1821
- pub fn insert_unique_unchecked ( & mut self , k : K , v : V ) -> ( & K , & mut V ) {
1829
+ pub unsafe fn insert_unique_unchecked ( & mut self , k : K , v : V ) -> ( & K , & mut V ) {
1822
1830
let hash = make_hash :: < K , S > ( & self . hash_builder , & k) ;
1823
1831
let bucket = self
1824
1832
. table
@@ -3021,7 +3029,7 @@ impl<'a, K, V, S, A: Allocator> IntoIterator for &'a HashMap<K, V, S, A> {
3021
3029
///
3022
3030
/// for (key, value) in &map_one {
3023
3031
/// println!("Key: {}, Value: {}", key, value);
3024
- /// map_two.insert_unique_unchecked (*key, *value);
3032
+ /// map_two.insert (*key, *value);
3025
3033
/// }
3026
3034
///
3027
3035
/// assert_eq!(map_one, map_two);
@@ -5040,9 +5048,9 @@ mod test_map {
5040
5048
#[ test]
5041
5049
fn test_insert_unique_unchecked ( ) {
5042
5050
let mut map = HashMap :: new ( ) ;
5043
- let ( k1, v1) = map. insert_unique_unchecked ( 10 , 11 ) ;
5051
+ let ( k1, v1) = unsafe { map. insert_unique_unchecked ( 10 , 11 ) } ;
5044
5052
assert_eq ! ( ( & 10 , & mut 11 ) , ( k1, v1) ) ;
5045
- let ( k2, v2) = map. insert_unique_unchecked ( 20 , 21 ) ;
5053
+ let ( k2, v2) = unsafe { map. insert_unique_unchecked ( 20 , 21 ) } ;
5046
5054
assert_eq ! ( ( & 20 , & mut 21 ) , ( k2, v2) ) ;
5047
5055
assert_eq ! ( Some ( & 11 ) , map. get( & 10 ) ) ;
5048
5056
assert_eq ! ( Some ( & 21 ) , map. get( & 20 ) ) ;
0 commit comments