Skip to content

Commit a0ab1de

Browse files
committed
Rename drain_filter to extract_if
1 parent d57ccf3 commit a0ab1de

File tree

2 files changed

+41
-44
lines changed

2 files changed

+41
-44
lines changed

src/map.rs

+26-26
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
},
@@ -2728,10 +2728,10 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
27282728
/// A draining iterator over entries of a `HashMap` which don't satisfy the predicate
27292729
/// `f(&k, &mut v)` in arbitrary order. The iterator element type is `(K, V)`.
27302730
///
2731-
/// This `struct` is created by the [`drain_filter`] method on [`HashMap`]. See its
2731+
/// This `struct` is created by the [`extract_if`] method on [`HashMap`]. See its
27322732
/// documentation for more.
27332733
///
2734-
/// [`drain_filter`]: struct.HashMap.html#method.drain_filter
2734+
/// [`extract_if`]: struct.HashMap.html#method.extract_if
27352735
/// [`HashMap`]: struct.HashMap.html
27362736
///
27372737
/// # Examples
@@ -2741,31 +2741,31 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
27412741
///
27422742
/// let mut map: HashMap<i32, &str> = [(1, "a"), (2, "b"), (3, "c")].into();
27432743
///
2744-
/// let mut drain_filter = map.drain_filter(|k, _v| k % 2 != 0);
2745-
/// let mut vec = vec![drain_filter.next(), drain_filter.next()];
2744+
/// let mut extract_if = map.extract_if(|k, _v| k % 2 != 0);
2745+
/// let mut vec = vec![extract_if.next(), extract_if.next()];
27462746
///
2747-
/// // The `DrainFilter` iterator produces items in arbitrary order, so the
2747+
/// // The `ExtractIf` iterator produces items in arbitrary order, so the
27482748
/// // items must be sorted to test them against a sorted array.
27492749
/// vec.sort_unstable();
27502750
/// assert_eq!(vec, [Some((1, "a")),Some((3, "c"))]);
27512751
///
27522752
/// // It is fused iterator
2753-
/// assert_eq!(drain_filter.next(), None);
2754-
/// assert_eq!(drain_filter.next(), None);
2755-
/// drop(drain_filter);
2753+
/// assert_eq!(extract_if.next(), None);
2754+
/// assert_eq!(extract_if.next(), None);
2755+
/// drop(extract_if);
27562756
///
27572757
/// assert_eq!(map.len(), 1);
27582758
/// ```
27592759
#[must_use = "Iterators are lazy unless consumed"]
2760-
pub struct DrainFilter<'a, K, V, F, A: Allocator + Clone = Global>
2760+
pub struct ExtractIf<'a, K, V, F, A: Allocator + Clone = Global>
27612761
where
27622762
F: FnMut(&K, &mut V) -> bool,
27632763
{
27642764
f: F,
2765-
inner: DrainFilterInner<'a, K, V, A>,
2765+
inner: ExtractIfInner<'a, K, V, A>,
27662766
}
27672767

2768-
impl<K, V, F, A> Iterator for DrainFilter<'_, K, V, F, A>
2768+
impl<K, V, F, A> Iterator for ExtractIf<'_, K, V, F, A>
27692769
where
27702770
F: FnMut(&K, &mut V) -> bool,
27712771
A: Allocator + Clone,
@@ -2783,15 +2783,15 @@ where
27832783
}
27842784
}
27852785

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

2788-
/// Portions of `DrainFilter` shared with `set::DrainFilter`
2789-
pub(super) struct DrainFilterInner<'a, K, V, A: Allocator + Clone> {
2788+
/// Portions of `ExtractIf` shared with `set::ExtractIf`
2789+
pub(super) struct ExtractIfInner<'a, K, V, A: Allocator + Clone> {
27902790
pub iter: RawIter<(K, V)>,
27912791
pub table: &'a mut RawTable<(K, V), A>,
27922792
}
27932793

2794-
impl<K, V, A: Allocator + Clone> DrainFilterInner<'_, K, V, A> {
2794+
impl<K, V, A: Allocator + Clone> ExtractIfInner<'_, K, V, A> {
27952795
#[cfg_attr(feature = "inline-more", inline)]
27962796
pub(super) fn next<F>(&mut self, f: &mut F) -> Option<(K, V)>
27972797
where
@@ -8142,18 +8142,18 @@ mod test_map {
81428142
}
81438143

81448144
#[test]
8145-
fn test_drain_filter() {
8145+
fn test_extract_if() {
81468146
{
81478147
let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x * 10)).collect();
8148-
let drained = map.drain_filter(|&k, _| k % 2 == 0);
8148+
let drained = map.extract_if(|&k, _| k % 2 == 0);
81498149
let mut out = drained.collect::<Vec<_>>();
81508150
out.sort_unstable();
81518151
assert_eq!(vec![(0, 0), (2, 20), (4, 40), (6, 60)], out);
81528152
assert_eq!(map.len(), 4);
81538153
}
81548154
{
81558155
let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x * 10)).collect();
8156-
map.drain_filter(|&k, _| k % 2 == 0).for_each(drop);
8156+
map.extract_if(|&k, _| k % 2 == 0).for_each(drop);
81578157
assert_eq!(map.len(), 4);
81588158
}
81598159
}

src/set.rs

+15-18
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
},
@@ -1567,18 +1567,18 @@ pub struct Drain<'a, K, A: Allocator + Clone = Global> {
15671567

15681568
/// A draining iterator over entries of a `HashSet` which don't satisfy the predicate `f`.
15691569
///
1570-
/// This `struct` is created by the [`drain_filter`] method on [`HashSet`]. See its
1570+
/// This `struct` is created by the [`extract_if`] method on [`HashSet`]. See its
15711571
/// documentation for more.
15721572
///
1573-
/// [`drain_filter`]: struct.HashSet.html#method.drain_filter
1573+
/// [`extract_if`]: struct.HashSet.html#method.extract_if
15741574
/// [`HashSet`]: struct.HashSet.html
15751575
#[must_use = "Iterators are lazy unless consumed"]
1576-
pub struct DrainFilter<'a, K, F, A: Allocator + Clone = Global>
1576+
pub struct ExtractIf<'a, K, F, A: Allocator + Clone = Global>
15771577
where
15781578
F: FnMut(&K) -> bool,
15791579
{
15801580
f: F,
1581-
inner: DrainFilterInner<'a, K, (), A>,
1581+
inner: ExtractIfInner<'a, K, (), A>,
15821582
}
15831583

15841584
/// A lazy iterator producing elements in the intersection of `HashSet`s.
@@ -1769,7 +1769,7 @@ impl<K: fmt::Debug, A: Allocator + Clone> fmt::Debug for Drain<'_, K, A> {
17691769
}
17701770
}
17711771

1772-
impl<K, F, A: Allocator + Clone> Iterator for DrainFilter<'_, K, F, A>
1772+
impl<K, F, A: Allocator + Clone> Iterator for ExtractIf<'_, K, F, A>
17731773
where
17741774
F: FnMut(&K) -> bool,
17751775
{
@@ -1788,10 +1788,7 @@ where
17881788
}
17891789
}
17901790

1791-
impl<K, F, A: Allocator + Clone> FusedIterator for DrainFilter<'_, K, F, A> where
1792-
F: FnMut(&K) -> bool
1793-
{
1794-
}
1791+
impl<K, F, A: Allocator + Clone> FusedIterator for ExtractIf<'_, K, F, A> where F: FnMut(&K) -> bool {}
17951792

17961793
impl<T, S, A: Allocator + Clone> Clone for Intersection<'_, T, S, A> {
17971794
#[cfg_attr(feature = "inline-more", inline)]
@@ -2837,18 +2834,18 @@ mod test_set {
28372834
}
28382835

28392836
#[test]
2840-
fn test_drain_filter() {
2837+
fn test_extract_if() {
28412838
{
28422839
let mut set: HashSet<i32> = (0..8).collect();
2843-
let drained = set.drain_filter(|&k| k % 2 == 0);
2840+
let drained = set.extract_if(|&k| k % 2 == 0);
28442841
let mut out = drained.collect::<Vec<_>>();
28452842
out.sort_unstable();
28462843
assert_eq!(vec![0, 2, 4, 6], out);
28472844
assert_eq!(set.len(), 4);
28482845
}
28492846
{
28502847
let mut set: HashSet<i32> = (0..8).collect();
2851-
set.drain_filter(|&k| k % 2 == 0).for_each(drop);
2848+
set.extract_if(|&k| k % 2 == 0).for_each(drop);
28522849
assert_eq!(set.len(), 4, "Removes non-matching items on drop");
28532850
}
28542851
}

0 commit comments

Comments
 (0)