Skip to content

Commit edfce75

Browse files
authored
Unrolled build for rust-lang#139764
Rollup merge of rust-lang#139764 - dtolnay:extractif, r=Amanieu Consistent trait bounds for ExtractIf Debug impls Closes rust-lang#137654. Refer to that issue for a table of the **4** different impl signatures we previously had in the standard library for Debug impls of various ExtractIf iterator types. The one we are standardizing on is the one so far only used by `alloc::collections::linked_list::ExtractIf`, which is _no_ `F: Debug` bound, _no_ `F: FnMut` bound, only `T: Debug` bound. This PR applies the following signature changes: ```diff /* alloc::collections::btree_map */ pub struct ExtractIf<'a, K, V, F, A = Global> where - F: 'a + FnMut(&K, &mut V) -> bool, Allocator + Clone, impl Debug for ExtractIf<'a, K, V, F, + A, > where K: Debug, V: Debug, - F: FnMut(&K, &mut V) -> bool, + A: Allocator + Clone, ``` ```diff /* alloc::collections::btree_set */ pub struct ExtractIf<'a, T, F, A = Global> where - T: 'a, - F: 'a + FnMut(&T) -> bool, Allocator + Clone, impl Debug for ExtractIf<'a, T, F, A> where T: Debug, - F: FnMut(&T) -> bool, A: Allocator + Clone, ``` ```diff /* alloc::collections::linked_list */ impl Debug for ExtractIf<'a, T, F, + A, > where T: Debug, + A: Allocator, ``` ```diff /* alloc::vec */ impl Debug for ExtractIf<'a, T, F, A> where T: Debug, - F: Debug, A: Allocator, - A: Debug, ``` ```diff /* std::collections::hash_map */ pub struct ExtractIf<'a, K, V, F> where - F: FnMut(&K, &mut V) -> bool, impl Debug for ExtractIf<'a, K, V, F> where + K: Debug, + V: Debug, - F: FnMut(&K, &mut V) -> bool, ``` ```diff /* std::collections::hash_set */ pub struct ExtractIf<'a, T, F> where - F: FnMut(&T) -> bool, impl Debug for ExtractIf<'a, T, F> where + T: Debug, - F: FnMut(&T) -> bool, ``` I have made the following changes to bring these types into better alignment with one another. - Delete `F: Debug` bounds. These are especially problematic because Rust closures do not come with a Debug impl, rendering the impl useless. - Delete `A: Debug` bounds. Allocator parameters are unstable for now, but in the future this would become an API commitment that we do not debug-print a representation of the allocator when printing an iterator. - Delete `F: FnMut` bounds. Requires `hashbrown` PR: rust-lang/hashbrown#616. **API commitment:** we commit to not doing RefCell voodoo inside ExtractIf to have some way for its Debug impl (which takes &amp;self) to call a FnMut closure, if this is even possible. - Add `T: Debug` bounds (or `K`/`V`), even on Debug impls that do not currently make use of them, but might in the future. **Breaking change.** Must backport into Rust 1.87 (current beta) or do a de-stabilization PR in beta to delay those types by one release. - Render using `debug_struct` + `finish_non_exhaustive`, instead of `debug_tuple`. - Do not render the _entire_ underlying collection. - Show a "peek" field indicating the current position of the iterator.
2 parents 7295b08 + c359143 commit edfce75

File tree

7 files changed

+49
-40
lines changed

7 files changed

+49
-40
lines changed

library/alloc/src/collections/btree/map.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1917,14 +1917,13 @@ pub struct ExtractIf<
19171917
V,
19181918
F,
19191919
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + Clone = Global,
1920-
> where
1921-
F: 'a + FnMut(&K, &mut V) -> bool,
1922-
{
1920+
> {
19231921
pred: F,
19241922
inner: ExtractIfInner<'a, K, V>,
19251923
/// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
19261924
alloc: A,
19271925
}
1926+
19281927
/// Most of the implementation of ExtractIf are generic over the type
19291928
/// of the predicate, thus also serving for BTreeSet::ExtractIf.
19301929
pub(super) struct ExtractIfInner<'a, K, V> {
@@ -1940,14 +1939,14 @@ pub(super) struct ExtractIfInner<'a, K, V> {
19401939
}
19411940

19421941
#[unstable(feature = "btree_extract_if", issue = "70530")]
1943-
impl<K, V, F> fmt::Debug for ExtractIf<'_, K, V, F>
1942+
impl<K, V, F, A> fmt::Debug for ExtractIf<'_, K, V, F, A>
19441943
where
19451944
K: fmt::Debug,
19461945
V: fmt::Debug,
1947-
F: FnMut(&K, &mut V) -> bool,
1946+
A: Allocator + Clone,
19481947
{
19491948
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1950-
f.debug_tuple("ExtractIf").field(&self.inner.peek()).finish()
1949+
f.debug_struct("ExtractIf").field("peek", &self.inner.peek()).finish_non_exhaustive()
19511950
}
19521951
}
19531952

library/alloc/src/collections/btree/set.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1556,24 +1556,23 @@ pub struct ExtractIf<
15561556
T,
15571557
F,
15581558
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + Clone = Global,
1559-
> where
1560-
T: 'a,
1561-
F: 'a + FnMut(&T) -> bool,
1562-
{
1559+
> {
15631560
pred: F,
15641561
inner: super::map::ExtractIfInner<'a, T, SetValZST>,
15651562
/// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
15661563
alloc: A,
15671564
}
15681565

15691566
#[unstable(feature = "btree_extract_if", issue = "70530")]
1570-
impl<T, F, A: Allocator + Clone> fmt::Debug for ExtractIf<'_, T, F, A>
1567+
impl<T, F, A> fmt::Debug for ExtractIf<'_, T, F, A>
15711568
where
15721569
T: fmt::Debug,
1573-
F: FnMut(&T) -> bool,
1570+
A: Allocator + Clone,
15741571
{
15751572
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1576-
f.debug_tuple("ExtractIf").field(&self.inner.peek().map(|(k, _)| k)).finish()
1573+
f.debug_struct("ExtractIf")
1574+
.field("peek", &self.inner.peek().map(|(k, _)| k))
1575+
.finish_non_exhaustive()
15771576
}
15781577
}
15791578

library/alloc/src/collections/linked_list.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1976,9 +1976,14 @@ where
19761976
}
19771977

19781978
#[stable(feature = "extract_if", since = "1.87.0")]
1979-
impl<T: fmt::Debug, F> fmt::Debug for ExtractIf<'_, T, F> {
1979+
impl<T, F, A> fmt::Debug for ExtractIf<'_, T, F, A>
1980+
where
1981+
T: fmt::Debug,
1982+
A: Allocator,
1983+
{
19801984
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1981-
f.debug_tuple("ExtractIf").field(&self.list).finish()
1985+
let peek = self.it.map(|node| unsafe { &node.as_ref().element });
1986+
f.debug_struct("ExtractIf").field("peek", &peek).finish_non_exhaustive()
19821987
}
19831988
}
19841989

library/alloc/src/vec/extract_if.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::ops::{Range, RangeBounds};
2-
use core::{ptr, slice};
2+
use core::{fmt, ptr, slice};
33

44
use super::Vec;
55
use crate::alloc::{Allocator, Global};
@@ -16,7 +16,6 @@ use crate::alloc::{Allocator, Global};
1616
/// let iter: std::vec::ExtractIf<'_, _, _> = v.extract_if(.., |x| *x % 2 == 0);
1717
/// ```
1818
#[stable(feature = "extract_if", since = "1.87.0")]
19-
#[derive(Debug)]
2019
#[must_use = "iterators are lazy and do nothing unless consumed"]
2120
pub struct ExtractIf<
2221
'a,
@@ -108,3 +107,15 @@ impl<T, F, A: Allocator> Drop for ExtractIf<'_, T, F, A> {
108107
}
109108
}
110109
}
110+
111+
#[stable(feature = "extract_if", since = "1.87.0")]
112+
impl<T, F, A> fmt::Debug for ExtractIf<'_, T, F, A>
113+
where
114+
T: fmt::Debug,
115+
A: Allocator,
116+
{
117+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118+
let peek = if self.idx < self.end { self.vec.get(self.idx) } else { None };
119+
f.debug_struct("ExtractIf").field("peek", &peek).finish_non_exhaustive()
120+
}
121+
}

library/std/src/collections/hash/map.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ impl<K, V, S> HashMap<K, V, S> {
683683
/// ```
684684
#[inline]
685685
#[rustc_lint_query_instability]
686-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
686+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
687687
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, K, V, F>
688688
where
689689
F: FnMut(&K, &mut V) -> bool,
@@ -1680,12 +1680,9 @@ impl<'a, K, V> Drain<'a, K, V> {
16801680
/// ]);
16811681
/// let iter = map.extract_if(|_k, v| *v % 2 == 0);
16821682
/// ```
1683-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
1683+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
16841684
#[must_use = "iterators are lazy and do nothing unless consumed"]
1685-
pub struct ExtractIf<'a, K, V, F>
1686-
where
1687-
F: FnMut(&K, &mut V) -> bool,
1688-
{
1685+
pub struct ExtractIf<'a, K, V, F> {
16891686
base: base::ExtractIf<'a, K, V, F>,
16901687
}
16911688

@@ -2297,7 +2294,7 @@ where
22972294
}
22982295
}
22992296

2300-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
2297+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
23012298
impl<K, V, F> Iterator for ExtractIf<'_, K, V, F>
23022299
where
23032300
F: FnMut(&K, &mut V) -> bool,
@@ -2314,13 +2311,14 @@ where
23142311
}
23152312
}
23162313

2317-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
2314+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
23182315
impl<K, V, F> FusedIterator for ExtractIf<'_, K, V, F> where F: FnMut(&K, &mut V) -> bool {}
23192316

2320-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
2321-
impl<'a, K, V, F> fmt::Debug for ExtractIf<'a, K, V, F>
2317+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
2318+
impl<K, V, F> fmt::Debug for ExtractIf<'_, K, V, F>
23222319
where
2323-
F: FnMut(&K, &mut V) -> bool,
2320+
K: fmt::Debug,
2321+
V: fmt::Debug,
23242322
{
23252323
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23262324
f.debug_struct("ExtractIf").finish_non_exhaustive()

library/std/src/collections/hash/set.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl<T, S> HashSet<T, S> {
308308
/// ```
309309
#[inline]
310310
#[rustc_lint_query_instability]
311-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
311+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
312312
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, T, F>
313313
where
314314
F: FnMut(&T) -> bool,
@@ -1390,11 +1390,8 @@ pub struct Drain<'a, K: 'a> {
13901390
///
13911391
/// let mut extract_ifed = a.extract_if(|v| v % 2 == 0);
13921392
/// ```
1393-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
1394-
pub struct ExtractIf<'a, K, F>
1395-
where
1396-
F: FnMut(&K) -> bool,
1397-
{
1393+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
1394+
pub struct ExtractIf<'a, K, F> {
13981395
base: base::ExtractIf<'a, K, F>,
13991396
}
14001397

@@ -1673,7 +1670,7 @@ impl<K: fmt::Debug> fmt::Debug for Drain<'_, K> {
16731670
}
16741671
}
16751672

1676-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
1673+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
16771674
impl<K, F> Iterator for ExtractIf<'_, K, F>
16781675
where
16791676
F: FnMut(&K) -> bool,
@@ -1690,13 +1687,13 @@ where
16901687
}
16911688
}
16921689

1693-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
1690+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
16941691
impl<K, F> FusedIterator for ExtractIf<'_, K, F> where F: FnMut(&K) -> bool {}
16951692

1696-
#[stable(feature = "hash_extract_if", since = "1.87.0")]
1697-
impl<'a, K, F> fmt::Debug for ExtractIf<'a, K, F>
1693+
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
1694+
impl<K, F> fmt::Debug for ExtractIf<'_, K, F>
16981695
where
1699-
F: FnMut(&K) -> bool,
1696+
K: fmt::Debug,
17001697
{
17011698
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17021699
f.debug_struct("ExtractIf").finish_non_exhaustive()

tests/rustdoc-js-std/path-maxeditdistance.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const EXPECTED = [
1313
{ 'path': 'std::vec', 'name': 'IntoIter' },
1414
{ 'path': 'std::vec::Vec', 'name': 'from_iter' },
1515
{ 'path': 'std::vec::Vec', 'name': 'into_iter' },
16+
{ 'path': 'std::vec::ExtractIf', 'name': 'into_iter' },
1617
{ 'path': 'std::vec::Drain', 'name': 'into_iter' },
1718
{ 'path': 'std::vec::IntoIter', 'name': 'into_iter' },
18-
{ 'path': 'std::vec::ExtractIf', 'name': 'into_iter' },
1919
{ 'path': 'std::vec::Splice', 'name': 'into_iter' },
2020
{ 'path': 'std::collections::VecDeque', 'name': 'iter' },
2121
{ 'path': 'std::collections::VecDeque', 'name': 'iter_mut' },

0 commit comments

Comments
 (0)