Skip to content

Commit c4c2328

Browse files
committed
Expose get_many_mut and get_many_unchecked_mut to HashMap
1 parent 5026e8c commit c4c2328

File tree

1 file changed

+113
-0
lines changed
  • std/src/collections/hash

1 file changed

+113
-0
lines changed

std/src/collections/hash/map.rs

+113
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,119 @@ where
895895
self.base.get_key_value(k)
896896
}
897897

898+
/// Attempts to get mutable references to `N` values in the map at once.
899+
///
900+
/// Returns an array of length `N` with the results of each query. For soundness, at most one
901+
/// mutable reference will be returned to any value. `None` will be returned if any of the
902+
/// keys are duplicates or missing.
903+
///
904+
/// # Examples
905+
///
906+
/// ```
907+
/// #![feature(map_many_mut)]
908+
/// use std::collections::HashMap;
909+
///
910+
/// let mut libraries = HashMap::new();
911+
/// libraries.insert("Bodleian Library".to_string(), 1602);
912+
/// libraries.insert("Athenæum".to_string(), 1807);
913+
/// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
914+
/// libraries.insert("Library of Congress".to_string(), 1800);
915+
///
916+
/// let got = libraries.get_many_mut([
917+
/// "Athenæum",
918+
/// "Library of Congress",
919+
/// ]);
920+
/// assert_eq!(
921+
/// got,
922+
/// Some([
923+
/// &mut 1807,
924+
/// &mut 1800,
925+
/// ]),
926+
/// );
927+
///
928+
/// // Missing keys result in None
929+
/// let got = libraries.get_many_mut([
930+
/// "Athenæum",
931+
/// "New York Public Library",
932+
/// ]);
933+
/// assert_eq!(got, None);
934+
///
935+
/// // Duplicate keys result in None
936+
/// let got = libraries.get_many_mut([
937+
/// "Athenæum",
938+
/// "Athenæum",
939+
/// ]);
940+
/// assert_eq!(got, None);
941+
/// ```
942+
#[inline]
943+
#[unstable(feature = "map_many_mut", issue = "97601")]
944+
pub fn get_many_mut<Q: ?Sized, const N: usize>(&mut self, ks: [&Q; N]) -> Option<[&'_ mut V; N]>
945+
where
946+
K: Borrow<Q>,
947+
Q: Hash + Eq,
948+
{
949+
self.base.get_many_mut(ks)
950+
}
951+
952+
/// Attempts to get mutable references to `N` values in the map at once, without validating that
953+
/// the values are unique.
954+
///
955+
/// Returns an array of length `N` with the results of each query. `None` will be returned if
956+
/// any of the keys are missing.
957+
///
958+
/// For a safe alternative see [`get_many_mut`](Self::get_many_mut).
959+
///
960+
/// # Safety
961+
///
962+
/// Calling this method with overlapping keys is *[undefined behavior]* even if the resulting
963+
/// references are not used.
964+
///
965+
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
966+
///
967+
/// # Examples
968+
///
969+
/// ```
970+
/// #![feature(map_many_mut)]
971+
/// use std::collections::HashMap;
972+
///
973+
/// let mut libraries = HashMap::new();
974+
/// libraries.insert("Bodleian Library".to_string(), 1602);
975+
/// libraries.insert("Athenæum".to_string(), 1807);
976+
/// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
977+
/// libraries.insert("Library of Congress".to_string(), 1800);
978+
///
979+
/// let got = libraries.get_many_mut([
980+
/// "Athenæum",
981+
/// "Library of Congress",
982+
/// ]);
983+
/// assert_eq!(
984+
/// got,
985+
/// Some([
986+
/// &mut 1807,
987+
/// &mut 1800,
988+
/// ]),
989+
/// );
990+
///
991+
/// // Missing keys result in None
992+
/// let got = libraries.get_many_mut([
993+
/// "Athenæum",
994+
/// "New York Public Library",
995+
/// ]);
996+
/// assert_eq!(got, None);
997+
/// ```
998+
#[inline]
999+
#[unstable(feature = "map_many_mut", issue = "97601")]
1000+
pub unsafe fn get_many_unchecked_mut<Q: ?Sized, const N: usize>(
1001+
&mut self,
1002+
ks: [&Q; N],
1003+
) -> Option<[&'_ mut V; N]>
1004+
where
1005+
K: Borrow<Q>,
1006+
Q: Hash + Eq,
1007+
{
1008+
self.base.get_many_unchecked_mut(ks)
1009+
}
1010+
8981011
/// Returns `true` if the map contains a value for the specified key.
8991012
///
9001013
/// The key may be any borrowed form of the map's key type, but

0 commit comments

Comments
 (0)