Skip to content

Conversation

yotamofek
Copy link
Contributor

Add a few traits for streamlining places where we need to wrap certain fmt::Displays in stuff like parentheses or brackets.
Hopefully this makes the actual display logic slightly easier to read.

First two commits are small, unrelated cleanups.

I'll probably add some doc comments to the stuff in display.rs, maybe also play around with the API, but wanted to get feedback on this idea first.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc 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. labels Sep 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 21, 2025

r? @notriddle

rustbot has assigned @notriddle.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

suffix: T,
}

pub(crate) struct AltBracket {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these fields are actually useful: it always contains the same data after all. Can be handled directly into the Display implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then we would need to differentiate between the bracket used as a prefix and the one used as a suffix.
So an enum?
Or separating the generic arg for the prefix and suffix in Wrapper

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum is a very good idea. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum actually, two newtypes could be the solution here:

struct OpenBracket;
struct CloseBracket;

And that's it.

Copy link
Contributor Author

@yotamofek yotamofek Sep 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then have two generic args for the Wrapper type?
Because right now it looks like this:

#[derive(Clone, Copy)]
pub(crate) struct Wrapper<T> {
    prefix: T,
    suffix: T,
}

so prefix and suffix have to be of the same type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open and Close is better naming. 👍

Is there any need for the AngleBracket type alias though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed with AngleBracket being an enum with Open and Close variants.

(but FWIW, opaque types are just so cool)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any need for the AngleBracket type alias though?

(not really important, just an aside)

Not sure why, but this doesn't work:

impl<T> Wrapper<T> {
    pub(crate) fn with_angle_brackets() -> Self {
        Self { prefix: angle_bracket(BracketPos::Open), suffix: angle_bracket(BracketPos::Close) }
    }
}

I'm getting this error:

error[E0308]: mismatched types
  --> src/librustdoc/display.rs:74:65
   |
61 | fn angle_bracket(pos: BracketPos) -> impl Display {
   |                                      ------------ the found opaque type
...
72 | impl<T> Wrapper<T> {
   |      - expected this type parameter
73 |     pub(crate) fn with_angle_brackets() -> Self {
74 |         Self { prefix: angle_bracket(BracketPos::Open), suffix: angle_bracket(BracketPos::Close) }
   |                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found opaque type
   |
   = note: expected type parameter `T`
                 found opaque type `impl std::fmt::Display`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

Maybe it's because every instance of an opaque type is considered different, so the prefix and suffix fields don't have the same type, as far as type checking is concerned?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, let me try to hack that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works fine so I guess opaque type is mandatory:

#![feature(type_alias_impl_trait)]
#![feature(debug_closure_helpers)]

use std::fmt;

struct Wrapper<T> {
    prefix: T,
    suffix: T,
}

pub(crate) type AngleBracket = impl fmt::Display;

#[derive(Clone, Copy)]
enum BracketPos {
    Open,
    Close,
}

#[define_opaque(AngleBracket)]
fn angle_bracket(pos: BracketPos) -> AngleBracket {
    fmt::from_fn(move |f| {
        f.write_str(match (pos, f.alternate()) {
            (BracketPos::Open, true) => "&lt;",
            (BracketPos::Open, false) => "<",
            (BracketPos::Close, true) => "&gt;",
            (BracketPos::Close, false) => ">",
        })
    })
}

impl Wrapper<AngleBracket> {
    pub(crate) fn with_angle_brackets() -> Self {
        Self { prefix: angle_bracket(BracketPos::Open), suffix: angle_bracket(BracketPos::Close) }
    }
}

@rustbot

This comment has been minimized.

@lolbinarycat
Copy link
Contributor

I think the basic idea is sound, but I think the naming could be a lot better.

I think Wrapped::with_parens().when(!sub_cfg.is_all()).around(Display(sub_cfg, self.1)) is way more immediatly readable than Wrapper::parentheses().optional(!sub_cfg.is_all()).wrap(Display(sub_cfg, self.1)). Also Wrapped::with('', '') seems more readable.

I would also rename WithOpts::new to WithOpts::from.

@rustbot

This comment has been minimized.

@yotamofek
Copy link
Contributor Author

I think the basic idea is sound, but I think the naming could be a lot better.

Thanks, I completely agree 😅

I think Wrapped::with_parens().when(!sub_cfg.is_all()).around(Display(sub_cfg, self.1)) is way more immediatly readable than Wrapper::parentheses().optional(!sub_cfg.is_all()).wrap(Display(sub_cfg, self.1)).

when is much better than optional, thanks!
I do kinda prefer wrap over around, and am ambivalent about with_parens (because with_angled_brackets is kinda long?).

Also Wrapped::with('', '') seems more readable.

This part I didn't really understand.

I would also rename WithOpts::new to WithOpts::from.

👍

@lolbinarycat
Copy link
Contributor

This part I didn't really understand.

I am suggesting to rename Wrapper::new to Wrapped::with.

@lolbinarycat
Copy link
Contributor

I do kinda prefer wrap over around, and am ambivalent about with_parens (because with_angled_brackets is kinda long?).

Honestly around is the name I'm least sure of, but I think the with_* prefix does improve readability enough to be worth it (also nit: I feel like with_angle_brackets (no d) would be the name I would choose)

@yotamofek
Copy link
Contributor Author

I do kinda prefer wrap over around, and am ambivalent about with_parens (because with_angled_brackets is kinda long?).

Honestly around is the name I'm least sure of, but I think the with_* prefix does improve readability enough to be worth it (also nit: I feel like with_angle_brackets (no d) would be the name I would choose)

Applied all your suggestions, apart from around 😁
(the angled was a typo)
Thanks for the suggestions, feels like an improvement to me.

@GuillaumeGomez
Copy link
Member

Looks good to me, thanks! I'll let @lolbinarycat do the r+ once they're satisfied as well.

@rustbot
Copy link
Collaborator

rustbot commented Sep 21, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@lolbinarycat
Copy link
Contributor

lolbinarycat commented Sep 21, 2025

Applied all your suggestions, apart from around 😁

Sorry, I don't think I was very explicit, but I would like if Wrapper was renamed to Wrapped. This makes "wrapped with" read more like a sentence, and also "wrapper" just makes me think of the newtype pattern, while "wrapped" does not.

Additionally, it looks like you forgot to rename new to with.

Apologies for the inconvenience.

@yotamofek
Copy link
Contributor Author

Apologies for the inconvenience.

No need to apologize 😁

Sorry, I don't think I was very explicit, but I would like if Wrapper was renamed to Wrapped. This makes "wrapped with" read more like a sentence, and also "wrapper" just makes me think of the newtype pattern, while "wrapped" does not.

Done.

Additionally, it looks like you forgot to rename new to with.

Woops, I think I did that and then at some point undid it by mistake.

Copy link
Contributor

@lolbinarycat lolbinarycat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just waiting on CI

View changes since this review

@lolbinarycat
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 22, 2025

📌 Commit 2067160 has been approved by lolbinarycat

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 22, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 22, 2025
…olbinarycat

Introduce "wrapper" helpers to rustdoc

Add a few traits for streamlining places where we need to wrap certain `fmt::Display`s in stuff like parentheses or brackets.
Hopefully this makes the actual display logic slightly easier to read.

First two commits are small, unrelated cleanups.

I'll probably add some doc comments to the stuff in `display.rs`, maybe also play around with the API, but wanted to get feedback on this idea first.
bors added a commit that referenced this pull request Sep 22, 2025
Rollup of 8 pull requests

Successful merges:

 - #146317 (Add panic=immediate-abort)
 - #146397 (std_detect on Darwin AArch64: update features)
 - #146594 (bootstrap: Don't force -static for musl targets in cc-rs)
 - #146791 (emit attribute for readonly non-pure inline assembly)
 - #146831 (Support ctr and lr as clobber-only registers in PowerPC inline assembly)
 - #146838 (Introduce "wrapper" helpers to rustdoc)
 - #146846 (btree InternalNode::new safety comments)
 - #146858 (Make mips64el-unknown-linux-muslabi64 link dynamically)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 22, 2025
…olbinarycat

Introduce "wrapper" helpers to rustdoc

Add a few traits for streamlining places where we need to wrap certain `fmt::Display`s in stuff like parentheses or brackets.
Hopefully this makes the actual display logic slightly easier to read.

First two commits are small, unrelated cleanups.

I'll probably add some doc comments to the stuff in `display.rs`, maybe also play around with the API, but wanted to get feedback on this idea first.
bors added a commit that referenced this pull request Sep 22, 2025
Rollup of 10 pull requests

Successful merges:

 - #145411 (regression test for Cow<[u8]> layout)
 - #146317 (Add panic=immediate-abort)
 - #146397 (std_detect on Darwin AArch64: update features)
 - #146594 (bootstrap: Don't force -static for musl targets in cc-rs)
 - #146652 (Port `feature` to the new attribute system)
 - #146791 (emit attribute for readonly non-pure inline assembly)
 - #146831 (Support ctr and lr as clobber-only registers in PowerPC inline assembly)
 - #146838 (Introduce "wrapper" helpers to rustdoc)
 - #146846 (btree InternalNode::new safety comments)
 - #146858 (Make mips64el-unknown-linux-muslabi64 link dynamically)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 22, 2025
…olbinarycat

Introduce "wrapper" helpers to rustdoc

Add a few traits for streamlining places where we need to wrap certain `fmt::Display`s in stuff like parentheses or brackets.
Hopefully this makes the actual display logic slightly easier to read.

First two commits are small, unrelated cleanups.

I'll probably add some doc comments to the stuff in `display.rs`, maybe also play around with the API, but wanted to get feedback on this idea first.
bors added a commit that referenced this pull request Sep 22, 2025
Rollup of 9 pull requests

Successful merges:

 - #145411 (regression test for Cow<[u8]> layout)
 - #146317 (Add panic=immediate-abort)
 - #146397 (std_detect on Darwin AArch64: update features)
 - #146594 (bootstrap: Don't force -static for musl targets in cc-rs)
 - #146791 (emit attribute for readonly non-pure inline assembly)
 - #146831 (Support ctr and lr as clobber-only registers in PowerPC inline assembly)
 - #146838 (Introduce "wrapper" helpers to rustdoc)
 - #146846 (btree InternalNode::new safety comments)
 - #146858 (Make mips64el-unknown-linux-muslabi64 link dynamically)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Sep 22, 2025
Rollup of 9 pull requests

Successful merges:

 - #145411 (regression test for Cow<[u8]> layout)
 - #146317 (Add panic=immediate-abort)
 - #146397 (std_detect on Darwin AArch64: update features)
 - #146594 (bootstrap: Don't force -static for musl targets in cc-rs)
 - #146791 (emit attribute for readonly non-pure inline assembly)
 - #146831 (Support ctr and lr as clobber-only registers in PowerPC inline assembly)
 - #146838 (Introduce "wrapper" helpers to rustdoc)
 - #146846 (btree InternalNode::new safety comments)
 - #146858 (Make mips64el-unknown-linux-muslabi64 link dynamically)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Sep 22, 2025
Rollup of 9 pull requests

Successful merges:

 - #145411 (regression test for Cow<[u8]> layout)
 - #146397 (std_detect on Darwin AArch64: update features)
 - #146791 (emit attribute for readonly non-pure inline assembly)
 - #146831 (Support ctr and lr as clobber-only registers in PowerPC inline assembly)
 - #146838 (Introduce "wrapper" helpers to rustdoc)
 - #146845 (Add self-profile events for target-machine creation)
 - #146846 (btree InternalNode::new safety comments)
 - #146858 (Make mips64el-unknown-linux-muslabi64 link dynamically)
 - #146878 (assert_unsafe_precondition: fix some incorrect check_language_ub)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 681da13 into rust-lang:master Sep 22, 2025
10 checks passed
@rustbot rustbot added this to the 1.92.0 milestone Sep 22, 2025
rust-timer added a commit that referenced this pull request Sep 22, 2025
Rollup merge of #146838 - yotamofek:pr/rustdoc/wrappers, r=lolbinarycat

Introduce "wrapper" helpers to rustdoc

Add a few traits for streamlining places where we need to wrap certain `fmt::Display`s in stuff like parentheses or brackets.
Hopefully this makes the actual display logic slightly easier to read.

First two commits are small, unrelated cleanups.

I'll probably add some doc comments to the stuff in `display.rs`, maybe also play around with the API, but wanted to get feedback on this idea first.
@yotamofek yotamofek deleted the pr/rustdoc/wrappers branch September 22, 2025 14:39
Muscraft pushed a commit to Muscraft/rust that referenced this pull request Sep 24, 2025
…olbinarycat

Introduce "wrapper" helpers to rustdoc

Add a few traits for streamlining places where we need to wrap certain `fmt::Display`s in stuff like parentheses or brackets.
Hopefully this makes the actual display logic slightly easier to read.

First two commits are small, unrelated cleanups.

I'll probably add some doc comments to the stuff in `display.rs`, maybe also play around with the API, but wanted to get feedback on this idea first.
Muscraft pushed a commit to Muscraft/rust that referenced this pull request Sep 24, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang#145411 (regression test for Cow<[u8]> layout)
 - rust-lang#146397 (std_detect on Darwin AArch64: update features)
 - rust-lang#146791 (emit attribute for readonly non-pure inline assembly)
 - rust-lang#146831 (Support ctr and lr as clobber-only registers in PowerPC inline assembly)
 - rust-lang#146838 (Introduce "wrapper" helpers to rustdoc)
 - rust-lang#146845 (Add self-profile events for target-machine creation)
 - rust-lang#146846 (btree InternalNode::new safety comments)
 - rust-lang#146858 (Make mips64el-unknown-linux-muslabi64 link dynamically)
 - rust-lang#146878 (assert_unsafe_precondition: fix some incorrect check_language_ub)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc 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.

6 participants