Skip to content

Commit 16e9fe6

Browse files
committed
rustdoc: Extract duplicated code into method
1 parent 147bc29 commit 16e9fe6

File tree

1 file changed

+49
-84
lines changed

1 file changed

+49
-84
lines changed

src/librustdoc/clean/cfg.rs

+49-84
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,53 @@ fn write_with_opt_paren<T: fmt::Display>(
390390
Ok(())
391391
}
392392

393+
impl Display<'_> {
394+
fn display_quantified_sub_cfgs(
395+
&self,
396+
fmt: &mut fmt::Formatter<'_>,
397+
sub_cfgs: &[Cfg],
398+
separator: &str,
399+
) -> fmt::Result {
400+
use fmt::Display as _;
401+
402+
let short_longhand = self.1.is_long() && {
403+
let all_crate_features =
404+
sub_cfgs.iter().all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::feature, Some(_))));
405+
let all_target_features = sub_cfgs
406+
.iter()
407+
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::target_feature, Some(_))));
408+
409+
if all_crate_features {
410+
fmt.write_str("crate features ")?;
411+
true
412+
} else if all_target_features {
413+
fmt.write_str("target features ")?;
414+
true
415+
} else {
416+
false
417+
}
418+
};
419+
420+
sub_cfgs
421+
.iter()
422+
.map(|sub_cfg| {
423+
fmt::from_fn(move |f| {
424+
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
425+
if self.1.is_html() {
426+
write!(f, "<code>{feat}</code>")
427+
} else {
428+
write!(f, "`{feat}`")
429+
}
430+
} else {
431+
write_with_opt_paren(f, !sub_cfg.is_all(), Display(sub_cfg, self.1))
432+
}
433+
})
434+
})
435+
.format(separator)
436+
.fmt(fmt)
437+
}
438+
}
439+
393440
impl fmt::Display for Display<'_> {
394441
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
395442
match *self.0 {
@@ -418,91 +465,9 @@ impl fmt::Display for Display<'_> {
418465

419466
Cfg::Any(ref sub_cfgs) => {
420467
let separator = if sub_cfgs.iter().all(Cfg::is_simple) { " or " } else { ", or " };
421-
422-
let short_longhand = self.1.is_long() && {
423-
let all_crate_features = sub_cfgs
424-
.iter()
425-
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::feature, Some(_))));
426-
let all_target_features = sub_cfgs
427-
.iter()
428-
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::target_feature, Some(_))));
429-
430-
if all_crate_features {
431-
fmt.write_str("crate features ")?;
432-
true
433-
} else if all_target_features {
434-
fmt.write_str("target features ")?;
435-
true
436-
} else {
437-
false
438-
}
439-
};
440-
441-
sub_cfgs
442-
.iter()
443-
.map(|sub_cfg| {
444-
fmt::from_fn(move |fmt| {
445-
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
446-
if self.1.is_html() {
447-
write!(fmt, "<code>{feat}</code>")
448-
} else {
449-
write!(fmt, "`{feat}`")
450-
}
451-
} else {
452-
write_with_opt_paren(
453-
fmt,
454-
!sub_cfg.is_all(),
455-
Display(sub_cfg, self.1),
456-
)
457-
}
458-
})
459-
})
460-
.format(separator)
461-
.fmt(fmt)
462-
}
463-
464-
Cfg::All(ref sub_cfgs) => {
465-
let short_longhand = self.1.is_long() && {
466-
let all_crate_features = sub_cfgs
467-
.iter()
468-
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::feature, Some(_))));
469-
let all_target_features = sub_cfgs
470-
.iter()
471-
.all(|sub_cfg| matches!(sub_cfg, Cfg::Cfg(sym::target_feature, Some(_))));
472-
473-
if all_crate_features {
474-
fmt.write_str("crate features ")?;
475-
true
476-
} else if all_target_features {
477-
fmt.write_str("target features ")?;
478-
true
479-
} else {
480-
false
481-
}
482-
};
483-
484-
sub_cfgs
485-
.iter()
486-
.map(|sub_cfg| {
487-
fmt::from_fn(move |fmt| {
488-
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
489-
if self.1.is_html() {
490-
write!(fmt, "<code>{feat}</code>")
491-
} else {
492-
write!(fmt, "`{feat}`")
493-
}
494-
} else {
495-
write_with_opt_paren(
496-
fmt,
497-
!sub_cfg.is_simple(),
498-
Display(sub_cfg, self.1),
499-
)
500-
}
501-
})
502-
})
503-
.format(" and ")
504-
.fmt(fmt)
468+
self.display_quantified_sub_cfgs(fmt, sub_cfgs, separator)
505469
}
470+
Cfg::All(ref sub_cfgs) => self.display_quantified_sub_cfgs(fmt, sub_cfgs, " and "),
506471

507472
Cfg::True => fmt.write_str("everywhere"),
508473
Cfg::False => fmt.write_str("nowhere"),

0 commit comments

Comments
 (0)