11#[ cfg( test) ]
22mod tests;
33
4- use hashbrown:: hash_map as base;
4+ use hashbrown:: hash_map:: { self as base, RustcOccupiedError } ;
55
66use self :: Entry :: * ;
77use crate :: alloc:: { Allocator , Global } ;
88use crate :: borrow:: Borrow ;
99use crate :: collections:: { TryReserveError , TryReserveErrorKind } ;
10- use crate :: error:: Error ;
1110use crate :: fmt:: { self , Debug } ;
1211use crate :: hash:: { BuildHasher , Hash , RandomState } ;
1312use crate :: iter:: FusedIterator ;
@@ -1333,7 +1332,7 @@ where
13331332 /// a mutable reference to the value in the entry.
13341333 ///
13351334 /// If the map already had this key present, nothing is updated, and
1336- /// an error containing the occupied entry and the value is returned.
1335+ /// an error containing the occupied entry, key, and the value is returned.
13371336 ///
13381337 /// # Examples
13391338 ///
@@ -1350,13 +1349,16 @@ where
13501349 /// let err = map.try_insert(37, "b").unwrap_err();
13511350 /// assert_eq!(err.entry.key(), &37);
13521351 /// assert_eq!(err.entry.get(), &"a");
1352+ /// assert_eq!(err.key, 37);
13531353 /// assert_eq!(err.value, "b");
13541354 /// ```
13551355 #[ unstable( feature = "map_try_insert" , issue = "82766" ) ]
13561356 pub fn try_insert ( & mut self , key : K , value : V ) -> Result < & mut V , OccupiedError < ' _ , K , V , A > > {
1357- match self . entry ( key) {
1358- Occupied ( entry) => Err ( OccupiedError { entry, value } ) ,
1359- Vacant ( entry) => Ok ( entry. insert ( value) ) ,
1357+ match self . base . rustc_try_insert ( key, value) {
1358+ Result :: Ok ( value) => Ok ( value) ,
1359+ Result :: Err ( RustcOccupiedError { entry, key, value, .. } ) => {
1360+ Err ( OccupiedError { entry : OccupiedEntry { base : entry } , key, value } )
1361+ }
13601362 }
13611363 }
13621364
@@ -2007,8 +2009,9 @@ impl<K: Debug, V, A: Allocator> Debug for VacantEntry<'_, K, V, A> {
20072009
20082010/// The error returned by [`try_insert`](HashMap::try_insert) when the key already exists.
20092011///
2010- /// Contains the occupied entry, and the value that was not inserted.
2012+ /// Contains the occupied entry, key, and the value that was not inserted.
20112013#[ unstable( feature = "map_try_insert" , issue = "82766" ) ]
2014+ #[ non_exhaustive]
20122015pub struct OccupiedError <
20132016 ' a ,
20142017 K : ' a ,
@@ -2017,6 +2020,8 @@ pub struct OccupiedError<
20172020> {
20182021 /// The entry in the map that was already occupied.
20192022 pub entry : OccupiedEntry < ' a , K , V , A > ,
2023+ /// The key which was not inserted, because the entry was already occupied.
2024+ pub key : K ,
20202025 /// The value which was not inserted, because the entry was already occupied.
20212026 pub value : V ,
20222027}
@@ -2026,28 +2031,13 @@ impl<K: Debug, V: Debug, A: Allocator> Debug for OccupiedError<'_, K, V, A> {
20262031 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
20272032 f. debug_struct ( "OccupiedError" )
20282033 . field ( "key" , self . entry . key ( ) )
2034+ . field ( "uninserted_key" , & self . key )
20292035 . field ( "old_value" , self . entry . get ( ) )
20302036 . field ( "new_value" , & self . value )
20312037 . finish_non_exhaustive ( )
20322038 }
20332039}
20342040
2035- #[ unstable( feature = "map_try_insert" , issue = "82766" ) ]
2036- impl < ' a , K : Debug , V : Debug , A : Allocator > fmt:: Display for OccupiedError < ' a , K , V , A > {
2037- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2038- write ! (
2039- f,
2040- "failed to insert {:?}, key {:?} already exists with value {:?}" ,
2041- self . value,
2042- self . entry. key( ) ,
2043- self . entry. get( ) ,
2044- )
2045- }
2046- }
2047-
2048- #[ unstable( feature = "map_try_insert" , issue = "82766" ) ]
2049- impl < ' a , K : Debug , V : Debug , A : Allocator > Error for OccupiedError < ' a , K , V , A > { }
2050-
20512041#[ stable( feature = "rust1" , since = "1.0.0" ) ]
20522042impl < ' a , K , V , S , A : Allocator > IntoIterator for & ' a HashMap < K , V , S , A > {
20532043 type Item = ( & ' a K , & ' a V ) ;
0 commit comments