Skip to content

Commit 3f9a5a2

Browse files
committed
Rename drain_filter to extract_if
1 parent 6628f6d commit 3f9a5a2

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

src/map.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,10 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
972972
/// In other words, move all pairs `(k, v)` such that `f(&k, &mut v)` returns `true` out
973973
/// into another iterator.
974974
///
975-
/// Note that `drain_filter` lets you mutate every value in the filter closure, regardless of
975+
/// Note that `extract_if` lets you mutate every value in the filter closure, regardless of
976976
/// whether you choose to keep or remove it.
977977
///
978-
/// If the returned `DrainFilter` is not exhausted, e.g. because it is dropped without iterating
978+
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
979979
/// or the iteration short-circuits, then the remaining elements will be retained.
980980
/// Use [`retain()`] with a negated predicate if you do not need the returned iterator.
981981
///
@@ -988,7 +988,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
988988
///
989989
/// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
990990
///
991-
/// let drained: HashMap<i32, i32> = map.drain_filter(|k, _v| k % 2 == 0).collect();
991+
/// let drained: HashMap<i32, i32> = map.extract_if(|k, _v| k % 2 == 0).collect();
992992
///
993993
/// let mut evens = drained.keys().cloned().collect::<Vec<_>>();
994994
/// let mut odds = map.keys().cloned().collect::<Vec<_>>();
@@ -1001,20 +1001,20 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
10011001
/// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
10021002
///
10031003
/// { // Iterator is dropped without being consumed.
1004-
/// let d = map.drain_filter(|k, _v| k % 2 != 0);
1004+
/// let d = map.extract_if(|k, _v| k % 2 != 0);
10051005
/// }
10061006
///
1007-
/// // DrainFilter was not exhausted, therefore no elements were drained.
1007+
/// // ExtractIf was not exhausted, therefore no elements were drained.
10081008
/// assert_eq!(map.len(), 8);
10091009
/// ```
10101010
#[cfg_attr(feature = "inline-more", inline)]
1011-
pub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, K, V, F, A>
1011+
pub fn extract_if<F>(&mut self, f: F) -> ExtractIf<'_, K, V, F, A>
10121012
where
10131013
F: FnMut(&K, &mut V) -> bool,
10141014
{
1015-
DrainFilter {
1015+
ExtractIf {
10161016
f,
1017-
inner: DrainFilterInner {
1017+
inner: ExtractIfInner {
10181018
iter: unsafe { self.table.iter() },
10191019
table: &mut self.table,
10201020
},
@@ -2707,10 +2707,10 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
27072707
/// A draining iterator over entries of a `HashMap` which don't satisfy the predicate
27082708
/// `f(&k, &mut v)` in arbitrary order. The iterator element type is `(K, V)`.
27092709
///
2710-
/// This `struct` is created by the [`drain_filter`] method on [`HashMap`]. See its
2710+
/// This `struct` is created by the [`extract_if`] method on [`HashMap`]. See its
27112711
/// documentation for more.
27122712
///
2713-
/// [`drain_filter`]: struct.HashMap.html#method.drain_filter
2713+
/// [`extract_if`]: struct.HashMap.html#method.extract_if
27142714
/// [`HashMap`]: struct.HashMap.html
27152715
///
27162716
/// # Examples
@@ -2720,31 +2720,31 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
27202720
///
27212721
/// let mut map: HashMap<i32, &str> = [(1, "a"), (2, "b"), (3, "c")].into();
27222722
///
2723-
/// let mut drain_filter = map.drain_filter(|k, _v| k % 2 != 0);
2724-
/// let mut vec = vec![drain_filter.next(), drain_filter.next()];
2723+
/// let mut extract_if = map.extract_if(|k, _v| k % 2 != 0);
2724+
/// let mut vec = vec![extract_if.next(), extract_if.next()];
27252725
///
2726-
/// // The `DrainFilter` iterator produces items in arbitrary order, so the
2726+
/// // The `ExtractIf` iterator produces items in arbitrary order, so the
27272727
/// // items must be sorted to test them against a sorted array.
27282728
/// vec.sort_unstable();
27292729
/// assert_eq!(vec, [Some((1, "a")),Some((3, "c"))]);
27302730
///
27312731
/// // It is fused iterator
2732-
/// assert_eq!(drain_filter.next(), None);
2733-
/// assert_eq!(drain_filter.next(), None);
2734-
/// drop(drain_filter);
2732+
/// assert_eq!(extract_if.next(), None);
2733+
/// assert_eq!(extract_if.next(), None);
2734+
/// drop(extract_if);
27352735
///
27362736
/// assert_eq!(map.len(), 1);
27372737
/// ```
27382738
#[must_use = "Iterators are lazy unless consumed"]
2739-
pub struct DrainFilter<'a, K, V, F, A: Allocator + Clone = Global>
2739+
pub struct ExtractIf<'a, K, V, F, A: Allocator + Clone = Global>
27402740
where
27412741
F: FnMut(&K, &mut V) -> bool,
27422742
{
27432743
f: F,
2744-
inner: DrainFilterInner<'a, K, V, A>,
2744+
inner: ExtractIfInner<'a, K, V, A>,
27452745
}
27462746

2747-
impl<K, V, F, A> Iterator for DrainFilter<'_, K, V, F, A>
2747+
impl<K, V, F, A> Iterator for ExtractIf<'_, K, V, F, A>
27482748
where
27492749
F: FnMut(&K, &mut V) -> bool,
27502750
A: Allocator + Clone,
@@ -2762,15 +2762,15 @@ where
27622762
}
27632763
}
27642764

2765-
impl<K, V, F> FusedIterator for DrainFilter<'_, K, V, F> where F: FnMut(&K, &mut V) -> bool {}
2765+
impl<K, V, F> FusedIterator for ExtractIf<'_, K, V, F> where F: FnMut(&K, &mut V) -> bool {}
27662766

2767-
/// Portions of `DrainFilter` shared with `set::DrainFilter`
2768-
pub(super) struct DrainFilterInner<'a, K, V, A: Allocator + Clone> {
2767+
/// Portions of `ExtractIf` shared with `set::ExtractIf`
2768+
pub(super) struct ExtractIfInner<'a, K, V, A: Allocator + Clone> {
27692769
pub iter: RawIter<(K, V)>,
27702770
pub table: &'a mut RawTable<(K, V), A>,
27712771
}
27722772

2773-
impl<K, V, A: Allocator + Clone> DrainFilterInner<'_, K, V, A> {
2773+
impl<K, V, A: Allocator + Clone> ExtractIfInner<'_, K, V, A> {
27742774
#[cfg_attr(feature = "inline-more", inline)]
27752775
pub(super) fn next<F>(&mut self, f: &mut F) -> Option<(K, V)>
27762776
where
@@ -8121,18 +8121,18 @@ mod test_map {
81218121
}
81228122

81238123
#[test]
8124-
fn test_drain_filter() {
8124+
fn test_extract_if() {
81258125
{
81268126
let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x * 10)).collect();
8127-
let drained = map.drain_filter(|&k, _| k % 2 == 0);
8127+
let drained = map.extract_if(|&k, _| k % 2 == 0);
81288128
let mut out = drained.collect::<Vec<_>>();
81298129
out.sort_unstable();
81308130
assert_eq!(vec![(0, 0), (2, 20), (4, 40), (6, 60)], out);
81318131
assert_eq!(map.len(), 4);
81328132
}
81338133
{
81348134
let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x * 10)).collect();
8135-
map.drain_filter(|&k, _| k % 2 == 0).for_each(drop);
8135+
map.extract_if(|&k, _| k % 2 == 0).for_each(drop);
81368136
assert_eq!(map.len(), 4);
81378137
}
81388138
}

src/set.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::hash::{BuildHasher, Hash};
77
use core::iter::{Chain, FromIterator, FusedIterator};
88
use core::ops::{BitAnd, BitOr, BitXor, Sub};
99

10-
use super::map::{self, DefaultHashBuilder, DrainFilterInner, HashMap, Keys};
10+
use super::map::{self, DefaultHashBuilder, ExtractIfInner, HashMap, Keys};
1111
use crate::raw::{Allocator, Global};
1212

1313
// Future Optimization (FIXME!)
@@ -379,7 +379,7 @@ impl<T, S, A: Allocator + Clone> HashSet<T, S, A> {
379379
/// In other words, move all elements `e` such that `f(&e)` returns `true` out
380380
/// into another iterator.
381381
///
382-
/// If the returned `DrainFilter` is not exhausted, e.g. because it is dropped without iterating
382+
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
383383
/// or the iteration short-circuits, then the remaining elements will be retained.
384384
/// Use [`retain()`] with a negated predicate if you do not need the returned iterator.
385385
///
@@ -389,7 +389,7 @@ impl<T, S, A: Allocator + Clone> HashSet<T, S, A> {
389389
/// use hashbrown::HashSet;
390390
///
391391
/// let mut set: HashSet<i32> = (0..8).collect();
392-
/// let drained: HashSet<i32> = set.drain_filter(|v| v % 2 == 0).collect();
392+
/// let drained: HashSet<i32> = set.extract_if(|v| v % 2 == 0).collect();
393393
///
394394
/// let mut evens = drained.into_iter().collect::<Vec<_>>();
395395
/// let mut odds = set.into_iter().collect::<Vec<_>>();
@@ -400,13 +400,13 @@ impl<T, S, A: Allocator + Clone> HashSet<T, S, A> {
400400
/// assert_eq!(odds, vec![1, 3, 5, 7]);
401401
/// ```
402402
#[cfg_attr(feature = "inline-more", inline)]
403-
pub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, T, F, A>
403+
pub fn extract_if<F>(&mut self, f: F) -> ExtractIf<'_, T, F, A>
404404
where
405405
F: FnMut(&T) -> bool,
406406
{
407-
DrainFilter {
407+
ExtractIf {
408408
f,
409-
inner: DrainFilterInner {
409+
inner: ExtractIfInner {
410410
iter: unsafe { self.map.table.iter() },
411411
table: &mut self.map.table,
412412
},
@@ -1547,18 +1547,18 @@ pub struct Drain<'a, K, A: Allocator + Clone = Global> {
15471547

15481548
/// A draining iterator over entries of a `HashSet` which don't satisfy the predicate `f`.
15491549
///
1550-
/// This `struct` is created by the [`drain_filter`] method on [`HashSet`]. See its
1550+
/// This `struct` is created by the [`extract_if`] method on [`HashSet`]. See its
15511551
/// documentation for more.
15521552
///
1553-
/// [`drain_filter`]: struct.HashSet.html#method.drain_filter
1553+
/// [`extract_if`]: struct.HashSet.html#method.extract_if
15541554
/// [`HashSet`]: struct.HashSet.html
15551555
#[must_use = "Iterators are lazy unless consumed"]
1556-
pub struct DrainFilter<'a, K, F, A: Allocator + Clone = Global>
1556+
pub struct ExtractIf<'a, K, F, A: Allocator + Clone = Global>
15571557
where
15581558
F: FnMut(&K) -> bool,
15591559
{
15601560
f: F,
1561-
inner: DrainFilterInner<'a, K, (), A>,
1561+
inner: ExtractIfInner<'a, K, (), A>,
15621562
}
15631563

15641564
/// A lazy iterator producing elements in the intersection of `HashSet`s.
@@ -1749,7 +1749,7 @@ impl<K: fmt::Debug, A: Allocator + Clone> fmt::Debug for Drain<'_, K, A> {
17491749
}
17501750
}
17511751

1752-
impl<K, F, A: Allocator + Clone> Iterator for DrainFilter<'_, K, F, A>
1752+
impl<K, F, A: Allocator + Clone> Iterator for ExtractIf<'_, K, F, A>
17531753
where
17541754
F: FnMut(&K) -> bool,
17551755
{
@@ -1768,7 +1768,7 @@ where
17681768
}
17691769
}
17701770

1771-
impl<K, F, A: Allocator + Clone> FusedIterator for DrainFilter<'_, K, F, A> where
1771+
impl<K, F, A: Allocator + Clone> FusedIterator for ExtractIf<'_, K, F, A> where
17721772
F: FnMut(&K) -> bool
17731773
{
17741774
}
@@ -2816,18 +2816,18 @@ mod test_set {
28162816
}
28172817

28182818
#[test]
2819-
fn test_drain_filter() {
2819+
fn test_extract_if() {
28202820
{
28212821
let mut set: HashSet<i32> = (0..8).collect();
2822-
let drained = set.drain_filter(|&k| k % 2 == 0);
2822+
let drained = set.extract_if(|&k| k % 2 == 0);
28232823
let mut out = drained.collect::<Vec<_>>();
28242824
out.sort_unstable();
28252825
assert_eq!(vec![0, 2, 4, 6], out);
28262826
assert_eq!(set.len(), 4);
28272827
}
28282828
{
28292829
let mut set: HashSet<i32> = (0..8).collect();
2830-
set.drain_filter(|&k| k % 2 == 0).for_each(drop);
2830+
set.extract_if(|&k| k % 2 == 0).for_each(drop);
28312831
assert_eq!(set.len(), 4, "Removes non-matching items on drop");
28322832
}
28332833
}

0 commit comments

Comments
 (0)