Skip to content

Drop FnMut trait bounds from ExtractIf data structures #616

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
Apr 15, 2025

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Apr 13, 2025

This is going to be required for resolving rust-lang/rust#137654.

Of the 4 distinct signatures shown in that issue, the one we'd like to standardize on is the one currently being used by alloc::collections::linked_list::ExtractIf, which is no F: Debug bound, no F: FnMut bound, only T: Debug bound.

Without this change in hashbrown, the desired Debug impls in std for std::collections::hash_map::ExtractIf and std::collections::hash_set::ExtractIf fail to compile.

error[E0277]: expected a `FnMut(&K, &mut V)` closure, found `F`
    --> library/std/src/collections/hash/map.rs:1683:11
     |
1683 |     base: base::ExtractIf<'a, K, V, F>,
     |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut(&K, &mut V)` closure, found `F`
     |
note: required by a bound in `hashbrown::hash_map::ExtractIf`
    --> hashbrown/src/map.rs:2601:8
     |
2599 | pub struct ExtractIf<'a, K, V, F, A: Allocator = Global>
     |            --------- required by a bound in this struct
2600 | where
2601 |     F: FnMut(&K, &mut V) -> bool,
     |        ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ExtractIf`
help: consider restricting type parameter `F` with trait `FnMut`
     |
1682 | pub struct ExtractIf<'a, K, V, F: for<'a, 'b> FnMut(&'a K, &'b mut V)> {
     |                                 +++++++++++++++++++++++++++++++++++++

@Amanieu Amanieu added this pull request to the merge queue Apr 15, 2025
Merged via the queue into rust-lang:master with commit 24d0480 Apr 15, 2025
26 checks passed
@dtolnay dtolnay deleted the extractif branch April 15, 2025 23:07
VlaDexa added a commit to VlaDexa/rust that referenced this pull request May 2, 2025
Update hashbrown dependency to unblock ExtractIf improvements

Release notes: https://github.com/rust-lang/hashbrown/releases/tag/v0.15.3

Relevant to me, this release includes rust-lang/hashbrown#616 which unblocks rust-lang#139764.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 3, 2025
Update hashbrown dependency to unblock ExtractIf improvements

Release notes: https://github.com/rust-lang/hashbrown/releases/tag/v0.15.3

Relevant to me, this release includes rust-lang/hashbrown#616 which unblocks rust-lang#139764.
github-actions bot pushed a commit to LegNeato/miri that referenced this pull request May 4, 2025
Update hashbrown dependency to unblock ExtractIf improvements

Release notes: https://github.com/rust-lang/hashbrown/releases/tag/v0.15.3

Relevant to me, this release includes rust-lang/hashbrown#616 which unblocks rust-lang/rust#139764.
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request May 5, 2025
Update hashbrown dependency to unblock ExtractIf improvements

Release notes: https://github.com/rust-lang/hashbrown/releases/tag/v0.15.3

Relevant to me, this release includes rust-lang/hashbrown#616 which unblocks rust-lang/rust#139764.
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request May 5, 2025
Update hashbrown dependency to unblock ExtractIf improvements

Release notes: https://github.com/rust-lang/hashbrown/releases/tag/v0.15.3

Relevant to me, this release includes rust-lang/hashbrown#616 which unblocks rust-lang/rust#139764.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 5, 2025
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.
Zalathar added a commit to Zalathar/rust that referenced this pull request May 6, 2025
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.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 6, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants