@@ -909,8 +909,11 @@ where
909
909
/// Attempts to get mutable references to `N` values in the map at once.
910
910
///
911
911
/// Returns an array of length `N` with the results of each query. For soundness, at most one
912
- /// mutable reference will be returned to any value. `None` will be returned if any of the
913
- /// keys are duplicates or missing.
912
+ /// mutable reference will be returned to any value. `None` will be used if the key is missing.
913
+ ///
914
+ /// # Panics
915
+ ///
916
+ /// Panics if any keys are overlapping.
914
917
///
915
918
/// # Examples
916
919
///
@@ -924,35 +927,55 @@ where
924
927
/// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
925
928
/// libraries.insert("Library of Congress".to_string(), 1800);
926
929
///
930
+ /// // Get Athenæum and Bodleian Library
931
+ /// let [Some(a), Some(b)] = libraries.get_many_mut([
932
+ /// "Athenæum",
933
+ /// "Bodleian Library",
934
+ /// ]) else { panic!() };
935
+ ///
936
+ /// // Assert values of Athenæum and Library of Congress
927
937
/// let got = libraries.get_many_mut([
928
938
/// "Athenæum",
929
939
/// "Library of Congress",
930
940
/// ]);
931
941
/// assert_eq!(
932
942
/// got,
933
- /// Some( [
934
- /// &mut 1807,
935
- /// &mut 1800,
936
- /// ]) ,
943
+ /// [
944
+ /// Some( &mut 1807) ,
945
+ /// Some( &mut 1800) ,
946
+ /// ],
937
947
/// );
938
948
///
939
949
/// // Missing keys result in None
940
950
/// let got = libraries.get_many_mut([
941
951
/// "Athenæum",
942
952
/// "New York Public Library",
943
953
/// ]);
944
- /// assert_eq!(got, None);
954
+ /// assert_eq!(
955
+ /// got,
956
+ /// [
957
+ /// Some(&mut 1807),
958
+ /// None
959
+ /// ]
960
+ /// );
961
+ /// ```
962
+ ///
963
+ /// ```should_panic
964
+ /// #![feature(map_many_mut)]
965
+ /// use std::collections::HashMap;
945
966
///
946
- /// // Duplicate keys result in None
967
+ /// let mut libraries = HashMap::new();
968
+ /// libraries.insert("Athenæum".to_string(), 1807);
969
+ ///
970
+ /// // Duplicate keys panic!
947
971
/// let got = libraries.get_many_mut([
948
972
/// "Athenæum",
949
973
/// "Athenæum",
950
974
/// ]);
951
- /// assert_eq!(got, None);
952
975
/// ```
953
976
#[ inline]
954
977
#[ unstable( feature = "map_many_mut" , issue = "97601" ) ]
955
- pub fn get_many_mut < Q : ?Sized , const N : usize > ( & mut self , ks : [ & Q ; N ] ) -> Option < [ & ' _ mut V ; N ] >
978
+ pub fn get_many_mut < Q : ?Sized , const N : usize > ( & mut self , ks : [ & Q ; N ] ) -> [ Option < & ' _ mut V > ; N ]
956
979
where
957
980
K : Borrow < Q > ,
958
981
Q : Hash + Eq ,
@@ -963,10 +986,10 @@ where
963
986
/// Attempts to get mutable references to `N` values in the map at once, without validating that
964
987
/// the values are unique.
965
988
///
966
- /// Returns an array of length `N` with the results of each query. `None` will be returned if
967
- /// any of the keys are missing.
989
+ /// Returns an array of length `N` with the results of each query. `None` will be used if
990
+ /// the key is missing.
968
991
///
969
- /// For a safe alternative see [`get_many_mut`](Self ::get_many_mut).
992
+ /// For a safe alternative see [`get_many_mut`](`HashMap ::get_many_mut` ).
970
993
///
971
994
/// # Safety
972
995
///
@@ -987,31 +1010,39 @@ where
987
1010
/// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
988
1011
/// libraries.insert("Library of Congress".to_string(), 1800);
989
1012
///
990
- /// let got = libraries.get_many_mut([
1013
+ /// // SAFETY: The keys do not overlap.
1014
+ /// let [Some(a), Some(b)] = (unsafe { libraries.get_many_unchecked_mut([
1015
+ /// "Athenæum",
1016
+ /// "Bodleian Library",
1017
+ /// ]) }) else { panic!() };
1018
+ ///
1019
+ /// // SAFETY: The keys do not overlap.
1020
+ /// let got = unsafe { libraries.get_many_unchecked_mut([
991
1021
/// "Athenæum",
992
1022
/// "Library of Congress",
993
- /// ]);
1023
+ /// ]) } ;
994
1024
/// assert_eq!(
995
1025
/// got,
996
- /// Some( [
997
- /// &mut 1807,
998
- /// &mut 1800,
999
- /// ]) ,
1026
+ /// [
1027
+ /// Some( &mut 1807) ,
1028
+ /// Some( &mut 1800) ,
1029
+ /// ],
1000
1030
/// );
1001
1031
///
1002
- /// // Missing keys result in None
1003
- /// let got = libraries.get_many_mut ([
1032
+ /// // SAFETY: The keys do not overlap.
1033
+ /// let got = unsafe { libraries.get_many_unchecked_mut ([
1004
1034
/// "Athenæum",
1005
1035
/// "New York Public Library",
1006
- /// ]);
1007
- /// assert_eq!(got, None);
1036
+ /// ]) };
1037
+ /// // Missing keys result in None
1038
+ /// assert_eq!(got, [Some(&mut 1807), None]);
1008
1039
/// ```
1009
1040
#[ inline]
1010
1041
#[ unstable( feature = "map_many_mut" , issue = "97601" ) ]
1011
1042
pub unsafe fn get_many_unchecked_mut < Q : ?Sized , const N : usize > (
1012
1043
& mut self ,
1013
1044
ks : [ & Q ; N ] ,
1014
- ) -> Option < [ & ' _ mut V ; N ] >
1045
+ ) -> [ Option < & ' _ mut V > ; N ]
1015
1046
where
1016
1047
K : Borrow < Q > ,
1017
1048
Q : Hash + Eq ,
@@ -2978,64 +3009,6 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
2978
3009
pub fn remove ( self ) -> V {
2979
3010
self . base . remove ( )
2980
3011
}
2981
-
2982
- /// Replaces the entry, returning the old key and value. The new key in the hash map will be
2983
- /// the key used to create this entry.
2984
- ///
2985
- /// # Examples
2986
- ///
2987
- /// ```
2988
- /// #![feature(map_entry_replace)]
2989
- /// use std::collections::hash_map::{Entry, HashMap};
2990
- /// use std::rc::Rc;
2991
- ///
2992
- /// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
2993
- /// map.insert(Rc::new("Stringthing".to_string()), 15);
2994
- ///
2995
- /// let my_key = Rc::new("Stringthing".to_string());
2996
- ///
2997
- /// if let Entry::Occupied(entry) = map.entry(my_key) {
2998
- /// // Also replace the key with a handle to our other key.
2999
- /// let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
3000
- /// }
3001
- ///
3002
- /// ```
3003
- #[ inline]
3004
- #[ unstable( feature = "map_entry_replace" , issue = "44286" ) ]
3005
- pub fn replace_entry ( self , value : V ) -> ( K , V ) {
3006
- self . base . replace_entry ( value)
3007
- }
3008
-
3009
- /// Replaces the key in the hash map with the key used to create this entry.
3010
- ///
3011
- /// # Examples
3012
- ///
3013
- /// ```
3014
- /// #![feature(map_entry_replace)]
3015
- /// use std::collections::hash_map::{Entry, HashMap};
3016
- /// use std::rc::Rc;
3017
- ///
3018
- /// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
3019
- /// let known_strings: Vec<Rc<String>> = Vec::new();
3020
- ///
3021
- /// // Initialise known strings, run program, etc.
3022
- ///
3023
- /// reclaim_memory(&mut map, &known_strings);
3024
- ///
3025
- /// fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
3026
- /// for s in known_strings {
3027
- /// if let Entry::Occupied(entry) = map.entry(Rc::clone(s)) {
3028
- /// // Replaces the entry's key with our version of it in `known_strings`.
3029
- /// entry.replace_key();
3030
- /// }
3031
- /// }
3032
- /// }
3033
- /// ```
3034
- #[ inline]
3035
- #[ unstable( feature = "map_entry_replace" , issue = "44286" ) ]
3036
- pub fn replace_key ( self ) -> K {
3037
- self . base . replace_key ( )
3038
- }
3039
3012
}
3040
3013
3041
3014
impl < ' a , K : ' a , V : ' a > VacantEntry < ' a , K , V > {
0 commit comments