@@ -1761,8 +1761,17 @@ where
17611761 /// Insert a key-value pair into the map without checking
17621762 /// if the key already exists in the map.
17631763 ///
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+ ///
17641771 /// Returns a reference to the key and value just inserted.
17651772 ///
1773+ /// # Safety
1774+ ///
17661775 /// This operation is safe if a key does not exist in the map.
17671776 ///
17681777 /// However, if a key exists in the map already, the behavior is unspecified:
@@ -1772,12 +1781,9 @@ where
17721781 /// That said, this operation (and following operations) are guaranteed to
17731782 /// not violate memory safety.
17741783 ///
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.
17811787 ///
17821788 /// # Examples
17831789 ///
@@ -1793,10 +1799,12 @@ where
17931799 /// let mut map2 = HashMap::new();
17941800 ///
17951801 /// for (key, value) in map1.into_iter() {
1796- /// map2.insert_unique_unchecked(key, value);
1802+ /// unsafe {
1803+ /// map2.insert_unique_unchecked(key, value);
1804+ /// }
17971805 /// }
17981806 ///
1799- /// let (key, value) = map2.insert_unique_unchecked(4, "d");
1807+ /// let (key, value) = unsafe { map2.insert_unique_unchecked(4, "d") } ;
18001808 /// assert_eq!(key, &4);
18011809 /// assert_eq!(value, &mut "d");
18021810 /// *value = "e";
@@ -1808,7 +1816,7 @@ where
18081816 /// assert_eq!(map2.len(), 4);
18091817 /// ```
18101818 #[ 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 ) {
18121820 let hash = make_hash :: < K , S > ( & self . hash_builder , & k) ;
18131821 let bucket = self
18141822 . table
@@ -3187,7 +3195,7 @@ impl<'a, K, V, S, A: Allocator> IntoIterator for &'a HashMap<K, V, S, A> {
31873195 ///
31883196 /// for (key, value) in &map_one {
31893197 /// println!("Key: {}, Value: {}", key, value);
3190- /// map_two.insert_unique_unchecked (*key, *value);
3198+ /// map_two.insert (*key, *value);
31913199 /// }
31923200 ///
31933201 /// assert_eq!(map_one, map_two);
@@ -5710,9 +5718,9 @@ mod test_map {
57105718 #[ test]
57115719 fn test_insert_unique_unchecked ( ) {
57125720 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 ) } ;
57145722 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 ) } ;
57165724 assert_eq ! ( ( & 20 , & mut 21 ) , ( k2, v2) ) ;
57175725 assert_eq ! ( Some ( & 11 ) , map. get( & 10 ) ) ;
57185726 assert_eq ! ( Some ( & 21 ) , map. get( & 20 ) ) ;
0 commit comments