Skip to content

Commit fc0094f

Browse files
committed
Auto merge of #135494 - yotamofek:rustdoc-fmt-from_fn, r=fmease
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.
2 parents cf577f3 + 48b5481 commit fc0094f

10 files changed

Lines changed: 127 additions & 172 deletions

File tree

‎src/librustdoc/clean/cfg.rs‎

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,49 @@ fn write_with_opt_paren<T: fmt::Display>(
389389
Ok(())
390390
}
391391

392+
implDisplay<'_>{
393+
fndisplay_sub_cfgs(
394+
&self,
395+
fmt:&mut fmt::Formatter<'_>,
396+
sub_cfgs:&[Cfg],
397+
separator:&str,
398+
) -> fmt::Result{
399+
let short_longhand = self.1.is_long() && {
400+
let all_crate_features =
401+
sub_cfgs.iter().all(|sub_cfg| matches!(sub_cfg,Cfg::Cfg(sym::feature,Some(_))));
402+
let all_target_features = sub_cfgs
403+
.iter()
404+
.all(|sub_cfg| matches!(sub_cfg,Cfg::Cfg(sym::target_feature,Some(_))));
405+
406+
if all_crate_features {
407+
fmt.write_str("crate features ")?;
408+
true
409+
}elseif all_target_features {
410+
fmt.write_str("target features ")?;
411+
true
412+
}else{
413+
false
414+
}
415+
};
416+
417+
for(i, sub_cfg)in sub_cfgs.iter().enumerate(){
418+
if i != 0{
419+
fmt.write_str(separator)?;
420+
}
421+
iflet(true,Cfg::Cfg(_,Some(feat))) = (short_longhand, sub_cfg){
422+
ifself.1.is_html(){
423+
write!(fmt,"<code>{feat}</code>")?;
424+
}else{
425+
write!(fmt,"`{feat}`")?;
426+
}
427+
}else{
428+
write_with_opt_paren(fmt, !sub_cfg.is_all(),Display(sub_cfg,self.1))?;
429+
}
430+
}
431+
Ok(())
432+
}
433+
}
434+
392435
impl fmt::DisplayforDisplay<'_>{
393436
fnfmt(&self,fmt:&mut fmt::Formatter<'_>) -> fmt::Result{
394437
match*self.0{
@@ -408,79 +451,9 @@ impl fmt::Display for Display<'_> {
408451

409452
Cfg::Any(ref sub_cfgs) => {
410453
let separator = if sub_cfgs.iter().all(Cfg::is_simple){" or "}else{", or "};
411-
412-
let short_longhand = self.1.is_long() && {
413-
let all_crate_features = sub_cfgs
414-
.iter()
415-
.all(|sub_cfg| matches!(sub_cfg,Cfg::Cfg(sym::feature,Some(_))));
416-
let all_target_features = sub_cfgs
417-
.iter()
418-
.all(|sub_cfg| matches!(sub_cfg,Cfg::Cfg(sym::target_feature,Some(_))));
419-
420-
if all_crate_features {
421-
fmt.write_str("crate features ")?;
422-
true
423-
}elseif all_target_features {
424-
fmt.write_str("target features ")?;
425-
true
426-
}else{
427-
false
428-
}
429-
};
430-
431-
for(i, sub_cfg)in sub_cfgs.iter().enumerate(){
432-
if i != 0{
433-
fmt.write_str(separator)?;
434-
}
435-
iflet(true,Cfg::Cfg(_,Some(feat))) = (short_longhand, sub_cfg){
436-
ifself.1.is_html(){
437-
write!(fmt,"<code>{feat}</code>")?;
438-
}else{
439-
write!(fmt,"`{feat}`")?;
440-
}
441-
}else{
442-
write_with_opt_paren(fmt, !sub_cfg.is_all(),Display(sub_cfg,self.1))?;
443-
}
444-
}
445-
Ok(())
446-
}
447-
448-
Cfg::All(ref sub_cfgs) => {
449-
let short_longhand = self.1.is_long() && {
450-
let all_crate_features = sub_cfgs
451-
.iter()
452-
.all(|sub_cfg| matches!(sub_cfg,Cfg::Cfg(sym::feature,Some(_))));
453-
let all_target_features = sub_cfgs
454-
.iter()
455-
.all(|sub_cfg| matches!(sub_cfg,Cfg::Cfg(sym::target_feature,Some(_))));
456-
457-
if all_crate_features {
458-
fmt.write_str("crate features ")?;
459-
true
460-
}elseif all_target_features {
461-
fmt.write_str("target features ")?;
462-
true
463-
}else{
464-
false
465-
}
466-
};
467-
468-
for(i, sub_cfg)in sub_cfgs.iter().enumerate(){
469-
if i != 0{
470-
fmt.write_str(" and ")?;
471-
}
472-
iflet(true,Cfg::Cfg(_,Some(feat))) = (short_longhand, sub_cfg){
473-
ifself.1.is_html(){
474-
write!(fmt,"<code>{feat}</code>")?;
475-
}else{
476-
write!(fmt,"`{feat}`")?;
477-
}
478-
}else{
479-
write_with_opt_paren(fmt, !sub_cfg.is_simple(),Display(sub_cfg,self.1))?;
480-
}
481-
}
482-
Ok(())
454+
self.display_sub_cfgs(fmt, sub_cfgs, separator)
483455
}
456+
Cfg::All(ref sub_cfgs) => self.display_sub_cfgs(fmt, sub_cfgs," and "),
484457

485458
Cfg::True => fmt.write_str("everywhere"),
486459
Cfg::False => fmt.write_str("nowhere"),

0 commit comments

Comments
 (0)