Skip to content

Commit 5137f7c

Browse files
authored
Rollup merge of #91593 - upsuper-forks:hashmap-set-methods-bound, r=dtolnay
Remove unnecessary bounds for some Hash{Map,Set} methods This PR moves `HashMap::{into_keys,into_values,retain}` and `HashSet::retain` from `impl` blocks with `K: Eq + Hash, S: BuildHasher` into the blocks without them. It doesn't seem to me there is any reason these methods need to be bounded by that. This change brings `HashMap::{into_keys,into_values}` on par with `HashMap::{keys,values,values_mut}` which are not bounded either.
2 parents 30ec1f0 + fb1e031 commit 5137f7c

File tree

2 files changed

+99
-99
lines changed

2 files changed

+99
-99
lines changed

library/std/src/collections/hash/map.rs

+77-77
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,33 @@ impl<K, V, S> HashMap<K, V, S> {
349349
Keys { inner: self.iter() }
350350
}
351351

352+
/// Creates a consuming iterator visiting all the keys in arbitrary order.
353+
/// The map cannot be used after calling this.
354+
/// The iterator element type is `K`.
355+
///
356+
/// # Examples
357+
///
358+
/// ```
359+
/// use std::collections::HashMap;
360+
///
361+
/// let map = HashMap::from([
362+
/// ("a", 1),
363+
/// ("b", 2),
364+
/// ("c", 3),
365+
/// ]);
366+
///
367+
/// let mut vec: Vec<&str> = map.into_keys().collect();
368+
/// // The `IntoKeys` iterator produces keys in arbitrary order, so the
369+
/// // keys must be sorted to test them against a sorted array.
370+
/// vec.sort_unstable();
371+
/// assert_eq!(vec, ["a", "b", "c"]);
372+
/// ```
373+
#[inline]
374+
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
375+
pub fn into_keys(self) -> IntoKeys<K, V> {
376+
IntoKeys { inner: self.into_iter() }
377+
}
378+
352379
/// An iterator visiting all values in arbitrary order.
353380
/// The iterator element type is `&'a V`.
354381
///
@@ -399,6 +426,33 @@ impl<K, V, S> HashMap<K, V, S> {
399426
ValuesMut { inner: self.iter_mut() }
400427
}
401428

429+
/// Creates a consuming iterator visiting all the values in arbitrary order.
430+
/// The map cannot be used after calling this.
431+
/// The iterator element type is `V`.
432+
///
433+
/// # Examples
434+
///
435+
/// ```
436+
/// use std::collections::HashMap;
437+
///
438+
/// let map = HashMap::from([
439+
/// ("a", 1),
440+
/// ("b", 2),
441+
/// ("c", 3),
442+
/// ]);
443+
///
444+
/// let mut vec: Vec<i32> = map.into_values().collect();
445+
/// // The `IntoValues` iterator produces values in arbitrary order, so
446+
/// // the values must be sorted to test them against a sorted array.
447+
/// vec.sort_unstable();
448+
/// assert_eq!(vec, [1, 2, 3]);
449+
/// ```
450+
#[inline]
451+
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
452+
pub fn into_values(self) -> IntoValues<K, V> {
453+
IntoValues { inner: self.into_iter() }
454+
}
455+
402456
/// An iterator visiting all key-value pairs in arbitrary order.
403457
/// The iterator element type is `(&'a K, &'a V)`.
404458
///
@@ -555,6 +609,29 @@ impl<K, V, S> HashMap<K, V, S> {
555609
DrainFilter { base: self.base.drain_filter(pred) }
556610
}
557611

612+
/// Retains only the elements specified by the predicate.
613+
///
614+
/// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`.
615+
/// The elements are visited in unsorted (and unspecified) order.
616+
///
617+
/// # Examples
618+
///
619+
/// ```
620+
/// use std::collections::HashMap;
621+
///
622+
/// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
623+
/// map.retain(|&k, _| k % 2 == 0);
624+
/// assert_eq!(map.len(), 4);
625+
/// ```
626+
#[inline]
627+
#[stable(feature = "retain_hash_collection", since = "1.18.0")]
628+
pub fn retain<F>(&mut self, f: F)
629+
where
630+
F: FnMut(&K, &mut V) -> bool,
631+
{
632+
self.base.retain(f)
633+
}
634+
558635
/// Clears the map, removing all key-value pairs. Keeps the allocated memory
559636
/// for reuse.
560637
///
@@ -937,83 +1014,6 @@ where
9371014
{
9381015
self.base.remove_entry(k)
9391016
}
940-
941-
/// Retains only the elements specified by the predicate.
942-
///
943-
/// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`.
944-
/// The elements are visited in unsorted (and unspecified) order.
945-
///
946-
/// # Examples
947-
///
948-
/// ```
949-
/// use std::collections::HashMap;
950-
///
951-
/// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
952-
/// map.retain(|&k, _| k % 2 == 0);
953-
/// assert_eq!(map.len(), 4);
954-
/// ```
955-
#[inline]
956-
#[stable(feature = "retain_hash_collection", since = "1.18.0")]
957-
pub fn retain<F>(&mut self, f: F)
958-
where
959-
F: FnMut(&K, &mut V) -> bool,
960-
{
961-
self.base.retain(f)
962-
}
963-
964-
/// Creates a consuming iterator visiting all the keys in arbitrary order.
965-
/// The map cannot be used after calling this.
966-
/// The iterator element type is `K`.
967-
///
968-
/// # Examples
969-
///
970-
/// ```
971-
/// use std::collections::HashMap;
972-
///
973-
/// let map = HashMap::from([
974-
/// ("a", 1),
975-
/// ("b", 2),
976-
/// ("c", 3),
977-
/// ]);
978-
///
979-
/// let mut vec: Vec<&str> = map.into_keys().collect();
980-
/// // The `IntoKeys` iterator produces keys in arbitrary order, so the
981-
/// // keys must be sorted to test them against a sorted array.
982-
/// vec.sort_unstable();
983-
/// assert_eq!(vec, ["a", "b", "c"]);
984-
/// ```
985-
#[inline]
986-
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
987-
pub fn into_keys(self) -> IntoKeys<K, V> {
988-
IntoKeys { inner: self.into_iter() }
989-
}
990-
991-
/// Creates a consuming iterator visiting all the values in arbitrary order.
992-
/// The map cannot be used after calling this.
993-
/// The iterator element type is `V`.
994-
///
995-
/// # Examples
996-
///
997-
/// ```
998-
/// use std::collections::HashMap;
999-
///
1000-
/// let map = HashMap::from([
1001-
/// ("a", 1),
1002-
/// ("b", 2),
1003-
/// ("c", 3),
1004-
/// ]);
1005-
///
1006-
/// let mut vec: Vec<i32> = map.into_values().collect();
1007-
/// // The `IntoValues` iterator produces values in arbitrary order, so
1008-
/// // the values must be sorted to test them against a sorted array.
1009-
/// vec.sort_unstable();
1010-
/// assert_eq!(vec, [1, 2, 3]);
1011-
/// ```
1012-
#[inline]
1013-
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
1014-
pub fn into_values(self) -> IntoValues<K, V> {
1015-
IntoValues { inner: self.into_iter() }
1016-
}
10171017
}
10181018

10191019
impl<K, V, S> HashMap<K, V, S>

library/std/src/collections/hash/set.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,28 @@ impl<T, S> HashSet<T, S> {
290290
DrainFilter { base: self.base.drain_filter(pred) }
291291
}
292292

293+
/// Retains only the elements specified by the predicate.
294+
///
295+
/// In other words, remove all elements `e` such that `f(&e)` returns `false`.
296+
/// The elements are visited in unsorted (and unspecified) order.
297+
///
298+
/// # Examples
299+
///
300+
/// ```
301+
/// use std::collections::HashSet;
302+
///
303+
/// let mut set = HashSet::from([1, 2, 3, 4, 5, 6]);
304+
/// set.retain(|&k| k % 2 == 0);
305+
/// assert_eq!(set.len(), 3);
306+
/// ```
307+
#[stable(feature = "retain_hash_collection", since = "1.18.0")]
308+
pub fn retain<F>(&mut self, f: F)
309+
where
310+
F: FnMut(&T) -> bool,
311+
{
312+
self.base.retain(f)
313+
}
314+
293315
/// Clears the set, removing all values.
294316
///
295317
/// # Examples
@@ -906,28 +928,6 @@ where
906928
{
907929
self.base.take(value)
908930
}
909-
910-
/// Retains only the elements specified by the predicate.
911-
///
912-
/// In other words, remove all elements `e` such that `f(&e)` returns `false`.
913-
/// The elements are visited in unsorted (and unspecified) order.
914-
///
915-
/// # Examples
916-
///
917-
/// ```
918-
/// use std::collections::HashSet;
919-
///
920-
/// let mut set = HashSet::from([1, 2, 3, 4, 5, 6]);
921-
/// set.retain(|&k| k % 2 == 0);
922-
/// assert_eq!(set.len(), 3);
923-
/// ```
924-
#[stable(feature = "retain_hash_collection", since = "1.18.0")]
925-
pub fn retain<F>(&mut self, f: F)
926-
where
927-
F: FnMut(&T) -> bool,
928-
{
929-
self.base.retain(f)
930-
}
931931
}
932932

933933
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)