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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
10 changes: 1 addition & 9 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ dependencies = [
"core",
]

[[package]]
name = "allocator-api2"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"

[[package]]
name = "alloctests"
version = "0.0.0"
Expand Down Expand Up @@ -135,10 +129,8 @@ dependencies = [
[[package]]
name = "hashbrown"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
source = "git+https://github.com/rust-lang/hashbrown?rev=refs%2Fpull%2F616%2Fhead#82cd86cc31a8f3a753dda0178f12946485e5fbbb"
dependencies = [
"allocator-api2",
"compiler_builtins",
"rustc-std-workspace-alloc",
"rustc-std-workspace-core",
Expand Down
3 changes: 3 additions & 0 deletions library/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ rustc-demangle.opt-level = "s"
rustc-std-workspace-core = { path = 'rustc-std-workspace-core' }
rustc-std-workspace-alloc = { path = 'rustc-std-workspace-alloc' }
rustc-std-workspace-std = { path = 'rustc-std-workspace-std' }

# FIXME https://github.com/rust-lang/hashbrown/pull/616
hashbrown = { git = "https://github.com/rust-lang/hashbrown", rev = "refs/pull/616/head" }
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()
}
}
10 changes: 4 additions & 6 deletions library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,10 +1679,7 @@ impl<'a, K, V> Drain<'a, K, V> {
/// ```
#[stable(feature = "hash_extract_if", since = "1.87.0")]
#[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 @@ -2315,9 +2312,10 @@ where
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>
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
9 changes: 3 additions & 6 deletions library/std/src/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,10 +1391,7 @@ 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,
{
pub struct ExtractIf<'a, K, F> {
base: base::ExtractIf<'a, K, F>,
}

Expand Down Expand Up @@ -1694,9 +1691,9 @@ where
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>
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
Loading