[rustdoc] Replace print methods with functions to improve code readability#148585
[rustdoc] Replace print methods with functions to improve code readability#148585bors merged 2 commits intorust-lang:masterfrom
print methods with functions to improve code readability#148585Conversation
This comment has been minimized.
This comment has been minimized.
7d251ec to
12ea654
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. |
|
I somehow replaced a |
|
I dunno, personally, I kinda prefer the method scheme over free fns. On an unrelated note, I wish there was a nice way to reduce all the #[display_fn]
fn print_term(term: ..., f: &mut Formatter) -> fmt::Result {
// ...
}and that would be expanded into a |
|
Not a big fan either of changing a signature like that. I'll squash down commits once you're ok with the code to make it easier for you to review. And having methods is better... until you have a lot of types implementing the same method and then you're completely lost when tracking down a bug. ^^' |
| let mut real_params = generics.params.iter().filter(|p| !p.is_synthetic_param()).peekable(); | ||
| if real_params.peek().is_none() { | ||
| None | ||
| } else { | ||
| Some(Wrapped::with_angle_brackets().wrap_fn(move |f| { | ||
| real_params.clone().map(|g| print_generic_param_def(g, cx)).joined(", ", f) | ||
| })) | ||
| } | ||
| .maybe_display() |
There was a problem hiding this comment.
Not directly related to your changes, but a drive-by thought while reviewing this. (also, this is probably something I wrote, originally).
This can be re-written as:
| let mut real_params = generics.params.iter().filter(|p| !p.is_synthetic_param()).peekable(); | |
| if real_params.peek().is_none() { | |
| None | |
| } else { | |
| Some(Wrapped::with_angle_brackets().wrap_fn(move |f| { | |
| real_params.clone().map(|g| print_generic_param_def(g, cx)).joined(", ", f) | |
| })) | |
| } | |
| .maybe_display() | |
| let mut real_params = self.params.iter().filter(|p| !p.is_synthetic_param()).peekable(); | |
| Wrapped::with_angle_brackets() | |
| .when(real_params.peek().is_some()) | |
| .wrap_fn(move |f| real_params.clone().map(|g| g.print(cx)).joined(", ", f)) |
WDYT?
👍
Yeah I agree, at some point it's sorta like a poor-man's trait. |
src/librustdoc/html/format.rs
Outdated
| } | ||
| fn print_generic_arg(generic_arg: &clean::GenericArg, cx: &Context<'_>) -> impl Display { | ||
| fmt::from_fn(move |f| match generic_arg { | ||
| clean::GenericArg::Lifetime(lt) => print_lifetime(lt).fmt(f), |
There was a problem hiding this comment.
Nit - maybe?
| clean::GenericArg::Lifetime(lt) => print_lifetime(lt).fmt(f), | |
| clean::GenericArg::Lifetime(lt) => f.write_str(print_lifetime(lt)), |
There was a problem hiding this comment.
You've already made this change elsewhere
src/librustdoc/html/format.rs
Outdated
| clean::GenericArg::Lifetime(lt) => print_lifetime(lt).fmt(f), | ||
| clean::GenericArg::Type(ty) => print_type(ty, cx).fmt(f), | ||
| clean::GenericArg::Const(ct) => print_constant_kind(ct, cx.tcx()).fmt(f), | ||
| clean::GenericArg::Infer => Display::fmt("_", f), |
There was a problem hiding this comment.
Nit (unrelated to PR):
| clean::GenericArg::Infer => Display::fmt("_", f), | |
| clean::GenericArg::Infer => f.write_char('_'), |
|
Okay, turned out to be a pretty easy review :) |
12ea654 to
0f03429
Compare
|
Squashed commits and applied suggestions. Thanks for the review! @bors r=yotamofek rollup |
…, r=yotamofek [rustdoc] Replace `print` methods with functions to improve code readability We have a lot of `print` methods, making it quite tricky to know what the types we're manipulating are. We did something similar with `Clean` trait a few years ago (the first PR was rust-lang#99638, followed by a lot of them). Each commit replaces one type for easier review. r? `@yotamofek`
Rollup of 5 pull requests Successful merges: - #145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - #148204 (Modify contributor email entries in .mailmap) - #148556 (Fix suggestion for returning async closures) - #148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - #148608 (Add test for --test-builder success path) r? `@ghost` `@rustbot` modify labels: rollup
…, r=yotamofek [rustdoc] Replace `print` methods with functions to improve code readability We have a lot of `print` methods, making it quite tricky to know what the types we're manipulating are. We did something similar with `Clean` trait a few years ago (the first PR was rust-lang#99638, followed by a lot of them). Each commit replaces one type for easier review. r? ``@yotamofek``
Rollup of 7 pull requests Successful merges: - #145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - #148204 (Modify contributor email entries in .mailmap) - #148556 (Fix suggestion for returning async closures) - #148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - #148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) - #148612 (Add note for identifier with attempted hygiene violation) - #148613 (Switch hexagon targets to rust-lld) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 7 pull requests Successful merges: - #145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - #148204 (Modify contributor email entries in .mailmap) - #148556 (Fix suggestion for returning async closures) - #148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - #148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) - #148612 (Add note for identifier with attempted hygiene violation) - #148613 (Switch hexagon targets to rust-lld) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 7 pull requests Successful merges: - #145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - #148204 (Modify contributor email entries in .mailmap) - #148556 (Fix suggestion for returning async closures) - #148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - #148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) - #148612 (Add note for identifier with attempted hygiene violation) - #148613 (Switch hexagon targets to rust-lld) r? `@ghost` `@rustbot` modify labels: rollup
…, r=yotamofek [rustdoc] Replace `print` methods with functions to improve code readability We have a lot of `print` methods, making it quite tricky to know what the types we're manipulating are. We did something similar with `Clean` trait a few years ago (the first PR was rust-lang#99638, followed by a lot of them). Each commit replaces one type for easier review. r? ```@yotamofek```
Rollup of 15 pull requests Successful merges: - #147404 (Fix issue with callsite inline attribute not being applied sometimes.) - #147534 (Implement SIMD funnel shifts in const-eval/Miri) - #147686 (update isolate_highest_one for NonZero<T>) - #148020 (Show backtrace on allocation failures when possible) - #148204 (Modify contributor email entries in .mailmap) - #148230 (rustdoc: Properly highlight shebang, frontmatter & weak keywords in source code pages and code blocks) - #148555 (Fix rust-by-example spanish translation) - #148556 (Fix suggestion for returning async closures) - #148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - #148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) - #148612 (Add note for identifier with attempted hygiene violation) - #148613 (Switch hexagon targets to rust-lld) - #148644 ([bootstrap] Make `--open` option work with `doc src/tools/error_index_generator`) - #148649 (don't completely reset `HeadUsages`) - #148675 (Remove eslint-js from npm dependencies) r? `@ghost` `@rustbot` modify labels: rollup
…, r=yotamofek [rustdoc] Replace `print` methods with functions to improve code readability We have a lot of `print` methods, making it quite tricky to know what the types we're manipulating are. We did something similar with `Clean` trait a few years ago (the first PR was rust-lang#99638, followed by a lot of them). Each commit replaces one type for easier review. r? ````@yotamofek````
Rollup of 16 pull requests Successful merges: - #147534 (Implement SIMD funnel shifts in const-eval/Miri) - #147686 (update isolate_highest_one for NonZero<T>) - #148020 (Show backtrace on allocation failures when possible) - #148204 (Modify contributor email entries in .mailmap) - #148230 (rustdoc: Properly highlight shebang, frontmatter & weak keywords in source code pages and code blocks) - #148279 (rustc_builtin_macros: rename bench parameter to avoid collisions with user-defined function names) - #148555 (Fix rust-by-example spanish translation) - #148556 (Fix suggestion for returning async closures) - #148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - #148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) - #148612 (Add note for identifier with attempted hygiene violation) - #148613 (Switch hexagon targets to rust-lld) - #148619 (Enable std locking functions on AIX) - #148644 ([bootstrap] Make `--open` option work with `doc src/tools/error_index_generator`) - #148649 (don't completely reset `HeadUsages`) - #148675 (Remove eslint-js from npm dependencies) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 10 pull requests Successful merges: - #145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - #147024 (std_detect: Support run-time detection on OpenBSD using elf_aux_info) - #147534 (Implement SIMD funnel shifts in const-eval/Miri) - #147540 (Stabilise `as_array` in `[_]` and `*const [_]`; stabilise `as_mut_array` in `[_]` and `*mut [_]`.) - #147686 (update isolate_highest_one for NonZero<T>) - #148230 (rustdoc: Properly highlight shebang, frontmatter & weak keywords in source code pages and code blocks) - #148555 (Fix rust-by-example spanish translation) - #148556 (Fix suggestion for returning async closures) - #148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - #148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 10 pull requests Successful merges: - #145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - #147024 (std_detect: Support run-time detection on OpenBSD using elf_aux_info) - #147534 (Implement SIMD funnel shifts in const-eval/Miri) - #147540 (Stabilise `as_array` in `[_]` and `*const [_]`; stabilise `as_mut_array` in `[_]` and `*mut [_]`.) - #147686 (update isolate_highest_one for NonZero<T>) - #148230 (rustdoc: Properly highlight shebang, frontmatter & weak keywords in source code pages and code blocks) - #148555 (Fix rust-by-example spanish translation) - #148556 (Fix suggestion for returning async closures) - #148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - #148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 10 pull requests Successful merges: - #145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - #147024 (std_detect: Support run-time detection on OpenBSD using elf_aux_info) - #147534 (Implement SIMD funnel shifts in const-eval/Miri) - #147540 (Stabilise `as_array` in `[_]` and `*const [_]`; stabilise `as_mut_array` in `[_]` and `*mut [_]`.) - #147686 (update isolate_highest_one for NonZero<T>) - #148230 (rustdoc: Properly highlight shebang, frontmatter & weak keywords in source code pages and code blocks) - #148555 (Fix rust-by-example spanish translation) - #148556 (Fix suggestion for returning async closures) - #148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - #148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #148585 - GuillaumeGomez:improve-format-code, r=yotamofek [rustdoc] Replace `print` methods with functions to improve code readability We have a lot of `print` methods, making it quite tricky to know what the types we're manipulating are. We did something similar with `Clean` trait a few years ago (the first PR was #99638, followed by a lot of them). Each commit replaces one type for easier review. r? `````@yotamofek`````
Rollup of 10 pull requests Successful merges: - rust-lang/rust#145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - rust-lang/rust#147024 (std_detect: Support run-time detection on OpenBSD using elf_aux_info) - rust-lang/rust#147534 (Implement SIMD funnel shifts in const-eval/Miri) - rust-lang/rust#147540 (Stabilise `as_array` in `[_]` and `*const [_]`; stabilise `as_mut_array` in `[_]` and `*mut [_]`.) - rust-lang/rust#147686 (update isolate_highest_one for NonZero<T>) - rust-lang/rust#148230 (rustdoc: Properly highlight shebang, frontmatter & weak keywords in source code pages and code blocks) - rust-lang/rust#148555 (Fix rust-by-example spanish translation) - rust-lang/rust#148556 (Fix suggestion for returning async closures) - rust-lang/rust#148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - rust-lang/rust#148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 10 pull requests Successful merges: - rust-lang/rust#145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - rust-lang/rust#147024 (std_detect: Support run-time detection on OpenBSD using elf_aux_info) - rust-lang/rust#147534 (Implement SIMD funnel shifts in const-eval/Miri) - rust-lang/rust#147540 (Stabilise `as_array` in `[_]` and `*const [_]`; stabilise `as_mut_array` in `[_]` and `*mut [_]`.) - rust-lang/rust#147686 (update isolate_highest_one for NonZero<T>) - rust-lang/rust#148230 (rustdoc: Properly highlight shebang, frontmatter & weak keywords in source code pages and code blocks) - rust-lang/rust#148555 (Fix rust-by-example spanish translation) - rust-lang/rust#148556 (Fix suggestion for returning async closures) - rust-lang/rust#148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - rust-lang/rust#148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 10 pull requests Successful merges: - rust-lang/rust#145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - rust-lang/rust#147024 (std_detect: Support run-time detection on OpenBSD using elf_aux_info) - rust-lang/rust#147534 (Implement SIMD funnel shifts in const-eval/Miri) - rust-lang/rust#147540 (Stabilise `as_array` in `[_]` and `*const [_]`; stabilise `as_mut_array` in `[_]` and `*mut [_]`.) - rust-lang/rust#147686 (update isolate_highest_one for NonZero<T>) - rust-lang/rust#148230 (rustdoc: Properly highlight shebang, frontmatter & weak keywords in source code pages and code blocks) - rust-lang/rust#148555 (Fix rust-by-example spanish translation) - rust-lang/rust#148556 (Fix suggestion for returning async closures) - rust-lang/rust#148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - rust-lang/rust#148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 10 pull requests Successful merges: - rust-lang/rust#145656 (Stabilize s390x `vector` target feature and `is_s390x_feature_detected!` macro) - rust-lang/rust#147024 (std_detect: Support run-time detection on OpenBSD using elf_aux_info) - rust-lang/rust#147534 (Implement SIMD funnel shifts in const-eval/Miri) - rust-lang/rust#147540 (Stabilise `as_array` in `[_]` and `*const [_]`; stabilise `as_mut_array` in `[_]` and `*mut [_]`.) - rust-lang/rust#147686 (update isolate_highest_one for NonZero<T>) - rust-lang/rust#148230 (rustdoc: Properly highlight shebang, frontmatter & weak keywords in source code pages and code blocks) - rust-lang/rust#148555 (Fix rust-by-example spanish translation) - rust-lang/rust#148556 (Fix suggestion for returning async closures) - rust-lang/rust#148585 ([rustdoc] Replace `print` methods with functions to improve code readability) - rust-lang/rust#148600 (re-use `self.get_all_attrs` result for pass indirectly attribute) r? `@ghost` `@rustbot` modify labels: rollup
We have a lot of
printmethods, making it quite tricky to know what the types we're manipulating are.We did something similar with
Cleantrait a few years ago (the first PR was #99638, followed by a lot of them).Each commit replaces one type for easier review.
r? @yotamofek