@@ -1771,8 +1771,17 @@ where
17711771 /// Insert a key-value pair into the map without checking
17721772 /// if the key already exists in the map.
17731773 ///
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+ ///
17741781 /// Returns a reference to the key and value just inserted.
17751782 ///
1783+ /// # Safety
1784+ ///
17761785 /// This operation is safe if a key does not exist in the map.
17771786 ///
17781787 /// However, if a key exists in the map already, the behavior is unspecified:
@@ -1782,12 +1791,9 @@ where
17821791 /// That said, this operation (and following operations) are guaranteed to
17831792 /// not violate memory safety.
17841793 ///
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.
17911797 ///
17921798 /// # Examples
17931799 ///
@@ -1803,10 +1809,12 @@ where
18031809 /// let mut map2 = HashMap::new();
18041810 ///
18051811 /// for (key, value) in map1.into_iter() {
1806- /// map2.insert_unique_unchecked(key, value);
1812+ /// unsafe {
1813+ /// map2.insert_unique_unchecked(key, value);
1814+ /// }
18071815 /// }
18081816 ///
1809- /// let (key, value) = map2.insert_unique_unchecked(4, "d");
1817+ /// let (key, value) = unsafe { map2.insert_unique_unchecked(4, "d") } ;
18101818 /// assert_eq!(key, &4);
18111819 /// assert_eq!(value, &mut "d");
18121820 /// *value = "e";
@@ -1818,7 +1826,7 @@ where
18181826 /// assert_eq!(map2.len(), 4);
18191827 /// ```
18201828 #[ 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 ) {
18221830 let hash = make_hash :: < K , S > ( & self . hash_builder , & k) ;
18231831 let bucket = self
18241832 . table
@@ -3021,7 +3029,7 @@ impl<'a, K, V, S, A: Allocator> IntoIterator for &'a HashMap<K, V, S, A> {
30213029 ///
30223030 /// for (key, value) in &map_one {
30233031 /// println!("Key: {}, Value: {}", key, value);
3024- /// map_two.insert_unique_unchecked (*key, *value);
3032+ /// map_two.insert (*key, *value);
30253033 /// }
30263034 ///
30273035 /// assert_eq!(map_one, map_two);
@@ -5040,9 +5048,9 @@ mod test_map {
50405048 #[ test]
50415049 fn test_insert_unique_unchecked ( ) {
50425050 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 ) } ;
50445052 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 ) } ;
50465054 assert_eq ! ( ( & 20 , & mut 21 ) , ( k2, v2) ) ;
50475055 assert_eq ! ( Some ( & 11 ) , map. get( & 10 ) ) ;
50485056 assert_eq ! ( Some ( & 21 ) , map. get( & 20 ) ) ;
0 commit comments