Skip to content

Commit

Permalink
Auto merge of #135494 - yotamofek:rustdoc-fmt-from_fn, r=fmease
Browse files Browse the repository at this point in the history
Refactor `fmt::Display` impls in rustdoc

This PR does a couple of things, with the intention of cleaning up and streamlining some of the `fmt::Display` impls in rustdoc:
1. Use the unstable [`fmt::from_fn`](#117729) instead of open-coding it.
2. ~~Replace bespoke implementations of `Itertools::format` with the method itself.~~
4. Some more minor cleanups - DRY, remove unnecessary calls to `Symbol::as_str()`, replace some `format!()` calls with lazier options

The changes are mostly cosmetic but some of them might have a slight positive effect on performance.
  • Loading branch information
bors committed Jan 23, 2025
2 parents cf577f3 + 48b5481 commit fc0094f
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 172 deletions.
117 changes: 45 additions & 72 deletions src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,49 @@ fn write_with_opt_paren<T: fmt::Display>(
Ok(())
}

impl Display<'_> {
fn display_sub_cfgs(
&self,
fmt: &mut fmt::Formatter<'_>,
sub_cfgs: &[Cfg],
separator: &str,
) -> fmt::Result {
let short_longhand = self.1.is_long() && {
let all_crate_features =
sub_cfgs.iter().all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::feature, Some(_))));
let all_target_features = sub_cfgs
.iter()
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::target_feature, Some(_))));

if all_crate_features {
fmt.write_str("crate features ")?;
true
} else if all_target_features {
fmt.write_str("target features ")?;
true
} else {
false
}
};

for (i, sub_cfg) in sub_cfgs.iter().enumerate() {
if i != 0 {
fmt.write_str(separator)?;
}
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
if self.1.is_html() {
write!(fmt, "<code>{feat}</code>")?;
} else {
write!(fmt, "`{feat}`")?;
}
} else {
write_with_opt_paren(fmt, !sub_cfg.is_all(), Display(sub_cfg, self.1))?;
}
}
Ok(())
}
}

impl fmt::Display for Display<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self.0 {
Expand All @@ -408,79 +451,9 @@ impl fmt::Display for Display<'_> {

Cfg::Any(ref sub_cfgs) => {
let separator = if sub_cfgs.iter().all(Cfg::is_simple) { " or " } else { ", or " };

let short_longhand = self.1.is_long() && {
let all_crate_features = sub_cfgs
.iter()
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::feature, Some(_))));
let all_target_features = sub_cfgs
.iter()
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::target_feature, Some(_))));

if all_crate_features {
fmt.write_str("crate features ")?;
true
} else if all_target_features {
fmt.write_str("target features ")?;
true
} else {
false
}
};

for (i, sub_cfg) in sub_cfgs.iter().enumerate() {
if i != 0 {
fmt.write_str(separator)?;
}
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
if self.1.is_html() {
write!(fmt, "<code>{feat}</code>")?;
} else {
write!(fmt, "`{feat}`")?;
}
} else {
write_with_opt_paren(fmt, !sub_cfg.is_all(), Display(sub_cfg, self.1))?;
}
}
Ok(())
}

Cfg::All(ref sub_cfgs) => {
let short_longhand = self.1.is_long() && {
let all_crate_features = sub_cfgs
.iter()
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::feature, Some(_))));
let all_target_features = sub_cfgs
.iter()
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::target_feature, Some(_))));

if all_crate_features {
fmt.write_str("crate features ")?;
true
} else if all_target_features {
fmt.write_str("target features ")?;
true
} else {
false
}
};

for (i, sub_cfg) in sub_cfgs.iter().enumerate() {
if i != 0 {
fmt.write_str(" and ")?;
}
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
if self.1.is_html() {
write!(fmt, "<code>{feat}</code>")?;
} else {
write!(fmt, "`{feat}`")?;
}
} else {
write_with_opt_paren(fmt, !sub_cfg.is_simple(), Display(sub_cfg, self.1))?;
}
}
Ok(())
self.display_sub_cfgs(fmt, sub_cfgs, separator)
}
Cfg::All(ref sub_cfgs) => self.display_sub_cfgs(fmt, sub_cfgs, " and "),

Cfg::True => fmt.write_str("everywhere"),
Cfg::False => fmt.write_str("nowhere"),
Expand Down
Loading

0 comments on commit fc0094f

Please sign in to comment.