Skip to content

Commit 5a0e770

Browse files
committed
s/drain_filter/extract_if/ for Vec, Btree{Map,Set} and LinkedList
1 parent af4efeb commit 5a0e770

File tree

14 files changed

+184
-189
lines changed

14 files changed

+184
-189
lines changed

alloc/benches/btree/map.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,15 @@ pub fn clone_slim_100_and_clear(b: &mut Bencher) {
385385
#[bench]
386386
pub fn clone_slim_100_and_drain_all(b: &mut Bencher) {
387387
let src = slim_map(100);
388-
b.iter(|| src.clone().drain_filter(|_, _| true).count())
388+
b.iter(|| src.clone().extract_if(|_, _| true).count())
389389
}
390390

391391
#[bench]
392392
pub fn clone_slim_100_and_drain_half(b: &mut Bencher) {
393393
let src = slim_map(100);
394394
b.iter(|| {
395395
let mut map = src.clone();
396-
assert_eq!(map.drain_filter(|i, _| i % 2 == 0).count(), 100 / 2);
396+
assert_eq!(map.extract_if(|i, _| i % 2 == 0).count(), 100 / 2);
397397
assert_eq!(map.len(), 100 / 2);
398398
})
399399
}
@@ -456,15 +456,15 @@ pub fn clone_slim_10k_and_clear(b: &mut Bencher) {
456456
#[bench]
457457
pub fn clone_slim_10k_and_drain_all(b: &mut Bencher) {
458458
let src = slim_map(10_000);
459-
b.iter(|| src.clone().drain_filter(|_, _| true).count())
459+
b.iter(|| src.clone().extract_if(|_, _| true).count())
460460
}
461461

462462
#[bench]
463463
pub fn clone_slim_10k_and_drain_half(b: &mut Bencher) {
464464
let src = slim_map(10_000);
465465
b.iter(|| {
466466
let mut map = src.clone();
467-
assert_eq!(map.drain_filter(|i, _| i % 2 == 0).count(), 10_000 / 2);
467+
assert_eq!(map.extract_if(|i, _| i % 2 == 0).count(), 10_000 / 2);
468468
assert_eq!(map.len(), 10_000 / 2);
469469
})
470470
}
@@ -527,15 +527,15 @@ pub fn clone_fat_val_100_and_clear(b: &mut Bencher) {
527527
#[bench]
528528
pub fn clone_fat_val_100_and_drain_all(b: &mut Bencher) {
529529
let src = fat_val_map(100);
530-
b.iter(|| src.clone().drain_filter(|_, _| true).count())
530+
b.iter(|| src.clone().extract_if(|_, _| true).count())
531531
}
532532

533533
#[bench]
534534
pub fn clone_fat_val_100_and_drain_half(b: &mut Bencher) {
535535
let src = fat_val_map(100);
536536
b.iter(|| {
537537
let mut map = src.clone();
538-
assert_eq!(map.drain_filter(|i, _| i % 2 == 0).count(), 100 / 2);
538+
assert_eq!(map.extract_if(|i, _| i % 2 == 0).count(), 100 / 2);
539539
assert_eq!(map.len(), 100 / 2);
540540
})
541541
}

alloc/benches/btree/set.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ pub fn clone_100_and_clear(b: &mut Bencher) {
6969
#[bench]
7070
pub fn clone_100_and_drain_all(b: &mut Bencher) {
7171
let src = slim_set(100);
72-
b.iter(|| src.clone().drain_filter(|_| true).count())
72+
b.iter(|| src.clone().extract_if(|_| true).count())
7373
}
7474

7575
#[bench]
7676
pub fn clone_100_and_drain_half(b: &mut Bencher) {
7777
let src = slim_set(100);
7878
b.iter(|| {
7979
let mut set = src.clone();
80-
assert_eq!(set.drain_filter(|i| i % 2 == 0).count(), 100 / 2);
80+
assert_eq!(set.extract_if(|i| i % 2 == 0).count(), 100 / 2);
8181
assert_eq!(set.len(), 100 / 2);
8282
})
8383
}
@@ -140,15 +140,15 @@ pub fn clone_10k_and_clear(b: &mut Bencher) {
140140
#[bench]
141141
pub fn clone_10k_and_drain_all(b: &mut Bencher) {
142142
let src = slim_set(10_000);
143-
b.iter(|| src.clone().drain_filter(|_| true).count())
143+
b.iter(|| src.clone().extract_if(|_| true).count())
144144
}
145145

146146
#[bench]
147147
pub fn clone_10k_and_drain_half(b: &mut Bencher) {
148148
let src = slim_set(10_000);
149149
b.iter(|| {
150150
let mut set = src.clone();
151-
assert_eq!(set.drain_filter(|i| i % 2 == 0).count(), 10_000 / 2);
151+
assert_eq!(set.extract_if(|i| i % 2 == 0).count(), 10_000 / 2);
152152
assert_eq!(set.len(), 10_000 / 2);
153153
})
154154
}

alloc/benches/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Disabling on android for the time being
22
// See https://github.com/rust-lang/rust/issues/73535#event-3477699747
33
#![cfg(not(target_os = "android"))]
4-
#![feature(btree_drain_filter)]
4+
#![feature(btree_extract_if)]
55
#![feature(iter_next_chunk)]
66
#![feature(repr_simd)]
77
#![feature(slice_partition_dedup)]

alloc/src/collections/btree/map.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
11321132
K: Ord,
11331133
F: FnMut(&K, &mut V) -> bool,
11341134
{
1135-
self.drain_filter(|k, v| !f(k, v)).for_each(drop);
1135+
self.extract_if(|k, v| !f(k, v)).for_each(drop);
11361136
}
11371137

11381138
/// Moves all elements from `other` into `self`, leaving `other` empty.
@@ -1395,7 +1395,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
13951395
/// The iterator also lets you mutate the value of each element in the
13961396
/// closure, regardless of whether you choose to keep or remove it.
13971397
///
1398-
/// If the returned `DrainFilter` is not exhausted, e.g. because it is dropped without iterating
1398+
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
13991399
/// or the iteration short-circuits, then the remaining elements will be retained.
14001400
/// Use [`retain`] with a negated predicate if you do not need the returned iterator.
14011401
///
@@ -1406,34 +1406,34 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14061406
/// Splitting a map into even and odd keys, reusing the original map:
14071407
///
14081408
/// ```
1409-
/// #![feature(btree_drain_filter)]
1409+
/// #![feature(btree_extract_if)]
14101410
/// use std::collections::BTreeMap;
14111411
///
14121412
/// let mut map: BTreeMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
1413-
/// let evens: BTreeMap<_, _> = map.drain_filter(|k, _v| k % 2 == 0).collect();
1413+
/// let evens: BTreeMap<_, _> = map.extract_if(|k, _v| k % 2 == 0).collect();
14141414
/// let odds = map;
14151415
/// assert_eq!(evens.keys().copied().collect::<Vec<_>>(), [0, 2, 4, 6]);
14161416
/// assert_eq!(odds.keys().copied().collect::<Vec<_>>(), [1, 3, 5, 7]);
14171417
/// ```
1418-
#[unstable(feature = "btree_drain_filter", issue = "70530")]
1419-
pub fn drain_filter<F>(&mut self, pred: F) -> DrainFilter<'_, K, V, F, A>
1418+
#[unstable(feature = "btree_extract_if", issue = "70530")]
1419+
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, K, V, F, A>
14201420
where
14211421
K: Ord,
14221422
F: FnMut(&K, &mut V) -> bool,
14231423
{
1424-
let (inner, alloc) = self.drain_filter_inner();
1425-
DrainFilter { pred, inner, alloc }
1424+
let (inner, alloc) = self.extract_if_inner();
1425+
ExtractIf { pred, inner, alloc }
14261426
}
14271427

1428-
pub(super) fn drain_filter_inner(&mut self) -> (DrainFilterInner<'_, K, V>, A)
1428+
pub(super) fn extract_if_inner(&mut self) -> (ExtractIfInner<'_, K, V>, A)
14291429
where
14301430
K: Ord,
14311431
{
14321432
if let Some(root) = self.root.as_mut() {
14331433
let (root, dormant_root) = DormantMutRef::new(root);
14341434
let front = root.borrow_mut().first_leaf_edge();
14351435
(
1436-
DrainFilterInner {
1436+
ExtractIfInner {
14371437
length: &mut self.length,
14381438
dormant_root: Some(dormant_root),
14391439
cur_leaf_edge: Some(front),
@@ -1442,7 +1442,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14421442
)
14431443
} else {
14441444
(
1445-
DrainFilterInner {
1445+
ExtractIfInner {
14461446
length: &mut self.length,
14471447
dormant_root: None,
14481448
cur_leaf_edge: None,
@@ -1896,10 +1896,10 @@ impl<K, V> Default for Values<'_, K, V> {
18961896
}
18971897
}
18981898

1899-
/// An iterator produced by calling `drain_filter` on BTreeMap.
1900-
#[unstable(feature = "btree_drain_filter", issue = "70530")]
1899+
/// An iterator produced by calling `extract_if` on BTreeMap.
1900+
#[unstable(feature = "btree_extract_if", issue = "70530")]
19011901
#[must_use = "iterators are lazy and do nothing unless consumed"]
1902-
pub struct DrainFilter<
1902+
pub struct ExtractIf<
19031903
'a,
19041904
K,
19051905
V,
@@ -1909,13 +1909,13 @@ pub struct DrainFilter<
19091909
F: 'a + FnMut(&K, &mut V) -> bool,
19101910
{
19111911
pred: F,
1912-
inner: DrainFilterInner<'a, K, V>,
1912+
inner: ExtractIfInner<'a, K, V>,
19131913
/// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
19141914
alloc: A,
19151915
}
1916-
/// Most of the implementation of DrainFilter are generic over the type
1917-
/// of the predicate, thus also serving for BTreeSet::DrainFilter.
1918-
pub(super) struct DrainFilterInner<'a, K, V> {
1916+
/// Most of the implementation of ExtractIf are generic over the type
1917+
/// of the predicate, thus also serving for BTreeSet::ExtractIf.
1918+
pub(super) struct ExtractIfInner<'a, K, V> {
19191919
/// Reference to the length field in the borrowed map, updated live.
19201920
length: &'a mut usize,
19211921
/// Buried reference to the root field in the borrowed map.
@@ -1927,20 +1927,20 @@ pub(super) struct DrainFilterInner<'a, K, V> {
19271927
cur_leaf_edge: Option<Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>>,
19281928
}
19291929

1930-
#[unstable(feature = "btree_drain_filter", issue = "70530")]
1931-
impl<K, V, F> fmt::Debug for DrainFilter<'_, K, V, F>
1930+
#[unstable(feature = "btree_extract_if", issue = "70530")]
1931+
impl<K, V, F> fmt::Debug for ExtractIf<'_, K, V, F>
19321932
where
19331933
K: fmt::Debug,
19341934
V: fmt::Debug,
19351935
F: FnMut(&K, &mut V) -> bool,
19361936
{
19371937
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1938-
f.debug_tuple("DrainFilter").field(&self.inner.peek()).finish()
1938+
f.debug_tuple("ExtractIf").field(&self.inner.peek()).finish()
19391939
}
19401940
}
19411941

1942-
#[unstable(feature = "btree_drain_filter", issue = "70530")]
1943-
impl<K, V, F, A: Allocator + Clone> Iterator for DrainFilter<'_, K, V, F, A>
1942+
#[unstable(feature = "btree_extract_if", issue = "70530")]
1943+
impl<K, V, F, A: Allocator + Clone> Iterator for ExtractIf<'_, K, V, F, A>
19441944
where
19451945
F: FnMut(&K, &mut V) -> bool,
19461946
{
@@ -1955,14 +1955,14 @@ where
19551955
}
19561956
}
19571957

1958-
impl<'a, K, V> DrainFilterInner<'a, K, V> {
1958+
impl<'a, K, V> ExtractIfInner<'a, K, V> {
19591959
/// Allow Debug implementations to predict the next element.
19601960
pub(super) fn peek(&self) -> Option<(&K, &V)> {
19611961
let edge = self.cur_leaf_edge.as_ref()?;
19621962
edge.reborrow().next_kv().ok().map(Handle::into_kv)
19631963
}
19641964

1965-
/// Implementation of a typical `DrainFilter::next` method, given the predicate.
1965+
/// Implementation of a typical `ExtractIf::next` method, given the predicate.
19661966
pub(super) fn next<F, A: Allocator + Clone>(&mut self, pred: &mut F, alloc: A) -> Option<(K, V)>
19671967
where
19681968
F: FnMut(&K, &mut V) -> bool,
@@ -1989,7 +1989,7 @@ impl<'a, K, V> DrainFilterInner<'a, K, V> {
19891989
None
19901990
}
19911991

1992-
/// Implementation of a typical `DrainFilter::size_hint` method.
1992+
/// Implementation of a typical `ExtractIf::size_hint` method.
19931993
pub(super) fn size_hint(&self) -> (usize, Option<usize>) {
19941994
// In most of the btree iterators, `self.length` is the number of elements
19951995
// yet to be visited. Here, it includes elements that were visited and that
@@ -1999,8 +1999,8 @@ impl<'a, K, V> DrainFilterInner<'a, K, V> {
19991999
}
20002000
}
20012001

2002-
#[unstable(feature = "btree_drain_filter", issue = "70530")]
2003-
impl<K, V, F> FusedIterator for DrainFilter<'_, K, V, F> where F: FnMut(&K, &mut V) -> bool {}
2002+
#[unstable(feature = "btree_extract_if", issue = "70530")]
2003+
impl<K, V, F> FusedIterator for ExtractIf<'_, K, V, F> where F: FnMut(&K, &mut V) -> bool {}
20042004

20052005
#[stable(feature = "btree_range", since = "1.17.0")]
20062006
impl<'a, K, V> Iterator for Range<'a, K, V> {

0 commit comments

Comments
 (0)