mirror of https://github.com/rust-lang/rust.git
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.
This commit is contained in:
commit
5137f7c9db
|
@ -349,6 +349,33 @@ impl<K, V, S> HashMap<K, V, S> {
|
|||
Keys { inner: self.iter() }
|
||||
}
|
||||
|
||||
/// Creates a consuming iterator visiting all the keys in arbitrary order.
|
||||
/// The map cannot be used after calling this.
|
||||
/// The iterator element type is `K`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// let map = HashMap::from([
|
||||
/// ("a", 1),
|
||||
/// ("b", 2),
|
||||
/// ("c", 3),
|
||||
/// ]);
|
||||
///
|
||||
/// let mut vec: Vec<&str> = map.into_keys().collect();
|
||||
/// // The `IntoKeys` iterator produces keys in arbitrary order, so the
|
||||
/// // keys must be sorted to test them against a sorted array.
|
||||
/// vec.sort_unstable();
|
||||
/// assert_eq!(vec, ["a", "b", "c"]);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
|
||||
pub fn into_keys(self) -> IntoKeys<K, V> {
|
||||
IntoKeys { inner: self.into_iter() }
|
||||
}
|
||||
|
||||
/// An iterator visiting all values in arbitrary order.
|
||||
/// The iterator element type is `&'a V`.
|
||||
///
|
||||
|
@ -399,6 +426,33 @@ impl<K, V, S> HashMap<K, V, S> {
|
|||
ValuesMut { inner: self.iter_mut() }
|
||||
}
|
||||
|
||||
/// Creates a consuming iterator visiting all the values in arbitrary order.
|
||||
/// The map cannot be used after calling this.
|
||||
/// The iterator element type is `V`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// let map = HashMap::from([
|
||||
/// ("a", 1),
|
||||
/// ("b", 2),
|
||||
/// ("c", 3),
|
||||
/// ]);
|
||||
///
|
||||
/// let mut vec: Vec<i32> = map.into_values().collect();
|
||||
/// // The `IntoValues` iterator produces values in arbitrary order, so
|
||||
/// // the values must be sorted to test them against a sorted array.
|
||||
/// vec.sort_unstable();
|
||||
/// assert_eq!(vec, [1, 2, 3]);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
|
||||
pub fn into_values(self) -> IntoValues<K, V> {
|
||||
IntoValues { inner: self.into_iter() }
|
||||
}
|
||||
|
||||
/// An iterator visiting all key-value pairs in arbitrary order.
|
||||
/// The iterator element type is `(&'a K, &'a V)`.
|
||||
///
|
||||
|
@ -555,6 +609,29 @@ impl<K, V, S> HashMap<K, V, S> {
|
|||
DrainFilter { base: self.base.drain_filter(pred) }
|
||||
}
|
||||
|
||||
/// Retains only the elements specified by the predicate.
|
||||
///
|
||||
/// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`.
|
||||
/// The elements are visited in unsorted (and unspecified) order.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
|
||||
/// map.retain(|&k, _| k % 2 == 0);
|
||||
/// assert_eq!(map.len(), 4);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "retain_hash_collection", since = "1.18.0")]
|
||||
pub fn retain<F>(&mut self, f: F)
|
||||
where
|
||||
F: FnMut(&K, &mut V) -> bool,
|
||||
{
|
||||
self.base.retain(f)
|
||||
}
|
||||
|
||||
/// Clears the map, removing all key-value pairs. Keeps the allocated memory
|
||||
/// for reuse.
|
||||
///
|
||||
|
@ -937,83 +1014,6 @@ where
|
|||
{
|
||||
self.base.remove_entry(k)
|
||||
}
|
||||
|
||||
/// Retains only the elements specified by the predicate.
|
||||
///
|
||||
/// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`.
|
||||
/// The elements are visited in unsorted (and unspecified) order.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
|
||||
/// map.retain(|&k, _| k % 2 == 0);
|
||||
/// assert_eq!(map.len(), 4);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "retain_hash_collection", since = "1.18.0")]
|
||||
pub fn retain<F>(&mut self, f: F)
|
||||
where
|
||||
F: FnMut(&K, &mut V) -> bool,
|
||||
{
|
||||
self.base.retain(f)
|
||||
}
|
||||
|
||||
/// Creates a consuming iterator visiting all the keys in arbitrary order.
|
||||
/// The map cannot be used after calling this.
|
||||
/// The iterator element type is `K`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// let map = HashMap::from([
|
||||
/// ("a", 1),
|
||||
/// ("b", 2),
|
||||
/// ("c", 3),
|
||||
/// ]);
|
||||
///
|
||||
/// let mut vec: Vec<&str> = map.into_keys().collect();
|
||||
/// // The `IntoKeys` iterator produces keys in arbitrary order, so the
|
||||
/// // keys must be sorted to test them against a sorted array.
|
||||
/// vec.sort_unstable();
|
||||
/// assert_eq!(vec, ["a", "b", "c"]);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
|
||||
pub fn into_keys(self) -> IntoKeys<K, V> {
|
||||
IntoKeys { inner: self.into_iter() }
|
||||
}
|
||||
|
||||
/// Creates a consuming iterator visiting all the values in arbitrary order.
|
||||
/// The map cannot be used after calling this.
|
||||
/// The iterator element type is `V`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// let map = HashMap::from([
|
||||
/// ("a", 1),
|
||||
/// ("b", 2),
|
||||
/// ("c", 3),
|
||||
/// ]);
|
||||
///
|
||||
/// let mut vec: Vec<i32> = map.into_values().collect();
|
||||
/// // The `IntoValues` iterator produces values in arbitrary order, so
|
||||
/// // the values must be sorted to test them against a sorted array.
|
||||
/// vec.sort_unstable();
|
||||
/// assert_eq!(vec, [1, 2, 3]);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
|
||||
pub fn into_values(self) -> IntoValues<K, V> {
|
||||
IntoValues { inner: self.into_iter() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> HashMap<K, V, S>
|
||||
|
|
|
@ -290,6 +290,28 @@ impl<T, S> HashSet<T, S> {
|
|||
DrainFilter { base: self.base.drain_filter(pred) }
|
||||
}
|
||||
|
||||
/// Retains only the elements specified by the predicate.
|
||||
///
|
||||
/// In other words, remove all elements `e` such that `f(&e)` returns `false`.
|
||||
/// The elements are visited in unsorted (and unspecified) order.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let mut set = HashSet::from([1, 2, 3, 4, 5, 6]);
|
||||
/// set.retain(|&k| k % 2 == 0);
|
||||
/// assert_eq!(set.len(), 3);
|
||||
/// ```
|
||||
#[stable(feature = "retain_hash_collection", since = "1.18.0")]
|
||||
pub fn retain<F>(&mut self, f: F)
|
||||
where
|
||||
F: FnMut(&T) -> bool,
|
||||
{
|
||||
self.base.retain(f)
|
||||
}
|
||||
|
||||
/// Clears the set, removing all values.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -906,28 +928,6 @@ where
|
|||
{
|
||||
self.base.take(value)
|
||||
}
|
||||
|
||||
/// Retains only the elements specified by the predicate.
|
||||
///
|
||||
/// In other words, remove all elements `e` such that `f(&e)` returns `false`.
|
||||
/// The elements are visited in unsorted (and unspecified) order.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// let mut set = HashSet::from([1, 2, 3, 4, 5, 6]);
|
||||
/// set.retain(|&k| k % 2 == 0);
|
||||
/// assert_eq!(set.len(), 3);
|
||||
/// ```
|
||||
#[stable(feature = "retain_hash_collection", since = "1.18.0")]
|
||||
pub fn retain<F>(&mut self, f: F)
|
||||
where
|
||||
F: FnMut(&T) -> bool,
|
||||
{
|
||||
self.base.retain(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
|
|
Loading…
Reference in New Issue