-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Introduce "wrapper" helpers to rustdoc #146838
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
Conversation
r? @notriddle rustbot has assigned @notriddle. Use |
d3310c7
to
40c5428
Compare
src/librustdoc/display.rs
Outdated
suffix: T, | ||
} | ||
|
||
pub(crate) struct AltBracket { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. 👍
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) => "<",
(BracketPos::Open, false) => "<",
(BracketPos::Close, true) => ">",
(BracketPos::Close, false) => ">",
})
})
}
impl Wrapper<AngleBracket> {
pub(crate) fn with_angle_brackets() -> Self {
Self { prefix: angle_bracket(BracketPos::Open), suffix: angle_bracket(BracketPos::Close) }
}
}
This comment has been minimized.
This comment has been minimized.
I think the basic idea is sound, but I think the naming could be a lot better. I think I would also rename |
5af003e
to
48d72e3
Compare
This comment has been minimized.
This comment has been minimized.
Thanks, I completely agree 😅
This part I didn't really understand.
👍 |
I am suggesting to rename |
Honestly |
48d72e3
to
2c5e20d
Compare
Applied all your suggestions, apart from |
2c5e20d
to
689d7cb
Compare
Looks good to me, thanks! I'll let @lolbinarycat do the r+ once they're satisfied as well. |
689d7cb
to
073f361
Compare
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. |
Sorry, I don't think I was very explicit, but I would like if Additionally, it looks like you forgot to rename Apologies for the inconvenience. |
No need to apologize 😁
Done.
Woops, I think I did that and then at some point undid it by mistake. |
073f361
to
2067160
Compare
There was a problem hiding this 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
@bors r+ |
…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.
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
…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.
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
…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.
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
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
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
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.
…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.
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
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.