@@ -1132,7 +1132,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
1132
1132
K : Ord ,
1133
1133
F : FnMut ( & K , & mut V ) -> bool ,
1134
1134
{
1135
- self . drain_filter ( |k, v| !f ( k, v) ) ;
1135
+ self . extract_if ( |k, v| !f ( k, v) ) . for_each ( drop ) ;
1136
1136
}
1137
1137
1138
1138
/// Moves all elements from `other` into `self`, leaving `other` empty.
@@ -1395,48 +1395,45 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
1395
1395
/// The iterator also lets you mutate the value of each element in the
1396
1396
/// closure, regardless of whether you choose to keep or remove it.
1397
1397
///
1398
- /// If the iterator is only partially consumed or not consumed at all, each
1399
- /// of the remaining elements is still subjected to the closure, which may
1400
- /// change its value and, by returning `true`, have the element removed and
1401
- /// dropped.
1398
+ /// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
1399
+ /// or the iteration short-circuits, then the remaining elements will be retained.
1400
+ /// Use [`retain`] with a negated predicate if you do not need the returned iterator.
1402
1401
///
1403
- /// It is unspecified how many more elements will be subjected to the
1404
- /// closure if a panic occurs in the closure, or a panic occurs while
1405
- /// dropping an element, or if the `DrainFilter` value is leaked.
1402
+ /// [`retain`]: BTreeMap::retain
1406
1403
///
1407
1404
/// # Examples
1408
1405
///
1409
1406
/// Splitting a map into even and odd keys, reusing the original map:
1410
1407
///
1411
1408
/// ```
1412
- /// #![feature(btree_drain_filter )]
1409
+ /// #![feature(btree_extract_if )]
1413
1410
/// use std::collections::BTreeMap;
1414
1411
///
1415
1412
/// let mut map: BTreeMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
1416
- /// 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();
1417
1414
/// let odds = map;
1418
1415
/// assert_eq!(evens.keys().copied().collect::<Vec<_>>(), [0, 2, 4, 6]);
1419
1416
/// assert_eq!(odds.keys().copied().collect::<Vec<_>>(), [1, 3, 5, 7]);
1420
1417
/// ```
1421
- #[ unstable( feature = "btree_drain_filter " , issue = "70530" ) ]
1422
- 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 >
1423
1420
where
1424
1421
K : Ord ,
1425
1422
F : FnMut ( & K , & mut V ) -> bool ,
1426
1423
{
1427
- let ( inner, alloc) = self . drain_filter_inner ( ) ;
1428
- DrainFilter { pred, inner, alloc }
1424
+ let ( inner, alloc) = self . extract_if_inner ( ) ;
1425
+ ExtractIf { pred, inner, alloc }
1429
1426
}
1430
1427
1431
- 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 )
1432
1429
where
1433
1430
K : Ord ,
1434
1431
{
1435
1432
if let Some ( root) = self . root . as_mut ( ) {
1436
1433
let ( root, dormant_root) = DormantMutRef :: new ( root) ;
1437
1434
let front = root. borrow_mut ( ) . first_leaf_edge ( ) ;
1438
1435
(
1439
- DrainFilterInner {
1436
+ ExtractIfInner {
1440
1437
length : & mut self . length ,
1441
1438
dormant_root : Some ( dormant_root) ,
1442
1439
cur_leaf_edge : Some ( front) ,
@@ -1445,7 +1442,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
1445
1442
)
1446
1443
} else {
1447
1444
(
1448
- DrainFilterInner {
1445
+ ExtractIfInner {
1449
1446
length : & mut self . length ,
1450
1447
dormant_root : None ,
1451
1448
cur_leaf_edge : None ,
@@ -1899,9 +1896,10 @@ impl<K, V> Default for Values<'_, K, V> {
1899
1896
}
1900
1897
}
1901
1898
1902
- /// An iterator produced by calling `drain_filter` on BTreeMap.
1903
- #[ unstable( feature = "btree_drain_filter" , issue = "70530" ) ]
1904
- pub struct DrainFilter <
1899
+ /// An iterator produced by calling `extract_if` on BTreeMap.
1900
+ #[ unstable( feature = "btree_extract_if" , issue = "70530" ) ]
1901
+ #[ must_use = "iterators are lazy and do nothing unless consumed" ]
1902
+ pub struct ExtractIf <
1905
1903
' a ,
1906
1904
K ,
1907
1905
V ,
@@ -1911,13 +1909,13 @@ pub struct DrainFilter<
1911
1909
F : ' a + FnMut ( & K , & mut V ) -> bool ,
1912
1910
{
1913
1911
pred : F ,
1914
- inner : DrainFilterInner < ' a , K , V > ,
1912
+ inner : ExtractIfInner < ' a , K , V > ,
1915
1913
/// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
1916
1914
alloc : A ,
1917
1915
}
1918
- /// Most of the implementation of DrainFilter are generic over the type
1919
- /// of the predicate, thus also serving for BTreeSet::DrainFilter .
1920
- 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 > {
1921
1919
/// Reference to the length field in the borrowed map, updated live.
1922
1920
length : & ' a mut usize ,
1923
1921
/// Buried reference to the root field in the borrowed map.
@@ -1929,30 +1927,20 @@ pub(super) struct DrainFilterInner<'a, K, V> {
1929
1927
cur_leaf_edge : Option < Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > > ,
1930
1928
}
1931
1929
1932
- #[ unstable( feature = "btree_drain_filter" , issue = "70530" ) ]
1933
- impl < K , V , F , A : Allocator + Clone > Drop for DrainFilter < ' _ , K , V , F , A >
1934
- where
1935
- F : FnMut ( & K , & mut V ) -> bool ,
1936
- {
1937
- fn drop ( & mut self ) {
1938
- self . for_each ( drop) ;
1939
- }
1940
- }
1941
-
1942
- #[ unstable( feature = "btree_drain_filter" , issue = "70530" ) ]
1943
- 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 >
1944
1932
where
1945
1933
K : fmt:: Debug ,
1946
1934
V : fmt:: Debug ,
1947
1935
F : FnMut ( & K , & mut V ) -> bool ,
1948
1936
{
1949
1937
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1950
- f. debug_tuple ( "DrainFilter " ) . field ( & self . inner . peek ( ) ) . finish ( )
1938
+ f. debug_tuple ( "ExtractIf " ) . field ( & self . inner . peek ( ) ) . finish ( )
1951
1939
}
1952
1940
}
1953
1941
1954
- #[ unstable( feature = "btree_drain_filter " , issue = "70530" ) ]
1955
- 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 >
1956
1944
where
1957
1945
F : FnMut ( & K , & mut V ) -> bool ,
1958
1946
{
@@ -1967,14 +1955,14 @@ where
1967
1955
}
1968
1956
}
1969
1957
1970
- impl < ' a , K , V > DrainFilterInner < ' a , K , V > {
1958
+ impl < ' a , K , V > ExtractIfInner < ' a , K , V > {
1971
1959
/// Allow Debug implementations to predict the next element.
1972
1960
pub ( super ) fn peek ( & self ) -> Option < ( & K , & V ) > {
1973
1961
let edge = self . cur_leaf_edge . as_ref ( ) ?;
1974
1962
edge. reborrow ( ) . next_kv ( ) . ok ( ) . map ( Handle :: into_kv)
1975
1963
}
1976
1964
1977
- /// Implementation of a typical `DrainFilter ::next` method, given the predicate.
1965
+ /// Implementation of a typical `ExtractIf ::next` method, given the predicate.
1978
1966
pub ( super ) fn next < F , A : Allocator + Clone > ( & mut self , pred : & mut F , alloc : A ) -> Option < ( K , V ) >
1979
1967
where
1980
1968
F : FnMut ( & K , & mut V ) -> bool ,
@@ -2001,7 +1989,7 @@ impl<'a, K, V> DrainFilterInner<'a, K, V> {
2001
1989
None
2002
1990
}
2003
1991
2004
- /// Implementation of a typical `DrainFilter ::size_hint` method.
1992
+ /// Implementation of a typical `ExtractIf ::size_hint` method.
2005
1993
pub ( super ) fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2006
1994
// In most of the btree iterators, `self.length` is the number of elements
2007
1995
// yet to be visited. Here, it includes elements that were visited and that
@@ -2011,8 +1999,8 @@ impl<'a, K, V> DrainFilterInner<'a, K, V> {
2011
1999
}
2012
2000
}
2013
2001
2014
- #[ unstable( feature = "btree_drain_filter " , issue = "70530" ) ]
2015
- 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 { }
2016
2004
2017
2005
#[ stable( feature = "btree_range" , since = "1.17.0" ) ]
2018
2006
impl < ' a , K , V > Iterator for Range < ' a , K , V > {
0 commit comments