Skip to content

Consistent trait bounds for ExtractIf Debug impls#139764

Merged
bors merged 1 commit into
rust-lang:masterfrom
dtolnay:extractif
May 6, 2025
Merged

Consistent trait bounds for ExtractIf Debug impls#139764
bors merged 1 commit into
rust-lang:masterfrom
dtolnay:extractif

Conversation

@dtolnay

@dtolnay dtolnay commented Apr 13, 2025

Copy link
Copy Markdown
Member

Closes #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:

/* 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,
/* 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,
/* alloc::collections::linked_list */

    impl Debug for ExtractIf<'a, T, F,
+       A,
    >
    where
        T: Debug,
+       A: Allocator,
/* alloc::vec */

    impl Debug for ExtractIf<'a, T, F, A>
    where
        T: Debug,
-       F: Debug,
        A: Allocator,
-       A: Debug,
/* 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,
/* 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: Drop FnMut trait bounds from ExtractIf data structures hashbrown#616. API commitment: we commit to not doing RefCell voodoo inside ExtractIf to have some way for its Debug impl (which takes &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.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-rustdoc-search Area: Rustdoc's search feature disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Debug impls of ExtractIf have inconsistent trait bounds

9 participants