Skip to content

Commit dcad630

Browse files
authored
Rollup merge of rust-lang#94647 - Urgau:hash-map-many-mut, r=Amanieu
Expose `get_many_mut` and `get_many_unchecked_mut` to HashMap This pull-request expose the function [`get_many_mut`](https://docs.rs/hashbrown/0.12.0/hashbrown/struct.HashMap.html#method.get_many_mut) and [`get_many_unchecked_mut`](https://docs.rs/hashbrown/0.12.0/hashbrown/struct.HashMap.html#method.get_many_unchecked_mut) from `hashbrown` to the standard library `HashMap` type. They obviously keep the same API and are added under the (new) `map_many_mut` feature. - `get_many_mut`: Attempts to get mutable references to `N` values in the map at once. - `get_many_unchecked_mut`: Attempts to get mutable references to `N` values in the map at once, without validating that the values are unique.
2 parents e355415 + c4c2328 commit dcad630

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
@@ -896,6 +896,119 @@ where
896896
self.base.get_key_value(k)
897897
}
898898

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

0 commit comments

Comments
 (0)