Skip to content

Consistent trait bounds for ExtractIf Debug impls #139764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1917,14 +1917,13 @@ pub struct ExtractIf<
V,
F,
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + Clone = Global,
> where
F: 'a + FnMut(&K, &mut V) -> bool,
{
> {
pred: F,
inner: ExtractIfInner<'a, K, V>,
/// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
alloc: A,
}

/// Most of the implementation of ExtractIf are generic over the type
/// of the predicate, thus also serving for BTreeSet::ExtractIf.
pub(super) struct ExtractIfInner<'a, K, V> {
Expand All @@ -1940,14 +1939,14 @@ pub(super) struct ExtractIfInner<'a, K, V> {
}

#[unstable(feature = "btree_extract_if", issue = "70530")]
impl<K, V, F> fmt::Debug for ExtractIf<'_, K, V, F>
impl<K, V, F, A> fmt::Debug for ExtractIf<'_, K, V, F, A>
where
K: fmt::Debug,
V: fmt::Debug,
F: FnMut(&K, &mut V) -> bool,
A: Allocator + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("ExtractIf").field(&self.inner.peek()).finish()
f.debug_struct("ExtractIf").field("peek", &self.inner.peek()).finish_non_exhaustive()
}
}

Expand Down
13 changes: 6 additions & 7 deletions library/alloc/src/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,24 +1556,23 @@ pub struct ExtractIf<
T,
F,
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + Clone = Global,
> where
T: 'a,
F: 'a + FnMut(&T) -> bool,
{
> {
pred: F,
inner: super::map::ExtractIfInner<'a, T, SetValZST>,
/// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
alloc: A,
}

#[unstable(feature = "btree_extract_if", issue = "70530")]
impl<T, F, A: Allocator + Clone> fmt::Debug for ExtractIf<'_, T, F, A>
impl<T, F, A> fmt::Debug for ExtractIf<'_, T, F, A>
where
T: fmt::Debug,
F: FnMut(&T) -> bool,
A: Allocator + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("ExtractIf").field(&self.inner.peek().map(|(k, _)| k)).finish()
f.debug_struct("ExtractIf")
.field("peek", &self.inner.peek().map(|(k, _)| k))
.finish_non_exhaustive()
}
}

Expand Down
9 changes: 7 additions & 2 deletions library/alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,9 +1976,14 @@ where
}

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

Expand Down
15 changes: 13 additions & 2 deletions library/alloc/src/vec/extract_if.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::ops::{Range, RangeBounds};
use core::{ptr, slice};
use core::{fmt, ptr, slice};

use super::Vec;
use crate::alloc::{Allocator, Global};
Expand All @@ -16,7 +16,6 @@ use crate::alloc::{Allocator, Global};
/// let iter: std::vec::ExtractIf<'_, _, _> = v.extract_if(.., |x| *x % 2 == 0);
/// ```
#[stable(feature = "extract_if", since = "1.87.0")]
#[derive(Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct ExtractIf<
'a,
Expand Down Expand Up @@ -108,3 +107,15 @@ impl<T, F, A: Allocator> Drop for ExtractIf<'_, T, F, A> {
}
}
}

#[stable(feature = "extract_if", since = "1.87.0")]
impl<T, F, A> fmt::Debug for ExtractIf<'_, T, F, A>
where
T: fmt::Debug,
A: Allocator,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let peek = if self.idx < self.end { self.vec.get(self.idx) } else { None };
f.debug_struct("ExtractIf").field("peek", &peek).finish_non_exhaustive()
}
}
20 changes: 9 additions & 11 deletions library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ impl<K, V, S> HashMap<K, V, S> {
/// ```
#[inline]
#[rustc_lint_query_instability]
#[stable(feature = "hash_extract_if", since = "1.87.0")]
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, K, V, F>
where
F: FnMut(&K, &mut V) -> bool,
Expand Down Expand Up @@ -1680,12 +1680,9 @@ impl<'a, K, V> Drain<'a, K, V> {
/// ]);
/// let iter = map.extract_if(|_k, v| *v % 2 == 0);
/// ```
#[stable(feature = "hash_extract_if", since = "1.87.0")]
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct ExtractIf<'a, K, V, F>
where
F: FnMut(&K, &mut V) -> bool,
{
pub struct ExtractIf<'a, K, V, F> {
base: base::ExtractIf<'a, K, V, F>,
}

Expand Down Expand Up @@ -2297,7 +2294,7 @@ where
}
}

#[stable(feature = "hash_extract_if", since = "1.87.0")]
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
impl<K, V, F> Iterator for ExtractIf<'_, K, V, F>
where
F: FnMut(&K, &mut V) -> bool,
Expand All @@ -2314,13 +2311,14 @@ where
}
}

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

#[stable(feature = "hash_extract_if", since = "1.87.0")]
impl<'a, K, V, F> fmt::Debug for ExtractIf<'a, K, V, F>
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
impl<K, V, F> fmt::Debug for ExtractIf<'_, K, V, F>
where
F: FnMut(&K, &mut V) -> bool,
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ExtractIf").finish_non_exhaustive()
Expand Down
19 changes: 8 additions & 11 deletions library/std/src/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl<T, S> HashSet<T, S> {
/// ```
#[inline]
#[rustc_lint_query_instability]
#[stable(feature = "hash_extract_if", since = "1.87.0")]
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, T, F>
where
F: FnMut(&T) -> bool,
Expand Down Expand Up @@ -1390,11 +1390,8 @@ pub struct Drain<'a, K: 'a> {
///
/// let mut extract_ifed = a.extract_if(|v| v % 2 == 0);
/// ```
#[stable(feature = "hash_extract_if", since = "1.87.0")]
pub struct ExtractIf<'a, K, F>
where
F: FnMut(&K) -> bool,
{
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
pub struct ExtractIf<'a, K, F> {
base: base::ExtractIf<'a, K, F>,
}

Expand Down Expand Up @@ -1673,7 +1670,7 @@ impl<K: fmt::Debug> fmt::Debug for Drain<'_, K> {
}
}

#[stable(feature = "hash_extract_if", since = "1.87.0")]
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
impl<K, F> Iterator for ExtractIf<'_, K, F>
where
F: FnMut(&K) -> bool,
Expand All @@ -1690,13 +1687,13 @@ where
}
}

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

#[stable(feature = "hash_extract_if", since = "1.87.0")]
impl<'a, K, F> fmt::Debug for ExtractIf<'a, K, F>
#[stable(feature = "hash_extract_if", since = "CURRENT_RUSTC_VERSION")]
impl<K, F> fmt::Debug for ExtractIf<'_, K, F>
where
F: FnMut(&K) -> bool,
K: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ExtractIf").finish_non_exhaustive()
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-js-std/path-maxeditdistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const EXPECTED = [
{ 'path': 'std::vec', 'name': 'IntoIter' },
{ 'path': 'std::vec::Vec', 'name': 'from_iter' },
{ 'path': 'std::vec::Vec', 'name': 'into_iter' },
{ 'path': 'std::vec::ExtractIf', 'name': 'into_iter' },
{ 'path': 'std::vec::Drain', 'name': 'into_iter' },
{ 'path': 'std::vec::IntoIter', 'name': 'into_iter' },
{ 'path': 'std::vec::ExtractIf', 'name': 'into_iter' },
{ 'path': 'std::vec::Splice', 'name': 'into_iter' },
{ 'path': 'std::collections::VecDeque', 'name': 'iter' },
{ 'path': 'std::collections::VecDeque', 'name': 'iter_mut' },
Expand Down
Loading