Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 109d05c

Browse files
committedJan 21, 2025·
Auto merge of rust-lang#135494 - yotamofek:rustdoc-fmt-from_fn, r=<try>
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`](rust-lang#117729) instead of open-coding it. 2. Replace bespoke implementations of `Itertools::format` with the method itself. 3. 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 cd805f0 + 37c30a0 commit 109d05c

File tree

10 files changed

+127
-172
lines changed

10 files changed

+127
-172
lines changed
 

‎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+
impl Display<'_> {
393+
fn display_quantified_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+
} else if 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+
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
422+
if self.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::Display for Display<'_> {
393436
fn fmt(&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-
} else if 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-
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
436-
if self.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-
} else if 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-
if let (true, Cfg::Cfg(_, Some(feat))) = (short_longhand, sub_cfg) {
473-
if self.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_quantified_sub_cfgs(fmt, sub_cfgs, separator)
483455
}
456+
Cfg::All(ref sub_cfgs) => self.display_quantified_sub_cfgs(fmt, sub_cfgs, " and "),
484457

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

‎src/librustdoc/html/format.rs

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ pub(crate) fn comma_sep<T: Display>(
150150
items: impl Iterator<Item = T>,
151151
space_after_comma: bool,
152152
) -> impl Display {
153-
display_fn(move |f| {
154-
for (i, item) in items.enumerate() {
153+
let items = Cell::new(Some(items));
154+
fmt::from_fn(move |f| {
155+
for (i, item) in items.take().unwrap().enumerate() {
155156
if i != 0 {
156157
write!(f, ",{}", if space_after_comma { " " } else { "" })?;
157158
}
@@ -165,7 +166,7 @@ pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
165166
bounds: &'a [clean::GenericBound],
166167
cx: &'a Context<'tcx>,
167168
) -> impl Display + 'a + Captures<'tcx> {
168-
display_fn(move |f| {
169+
fmt::from_fn(move |f| {
169170
let mut bounds_dup = FxHashSet::default();
170171

171172
for (i, bound) in bounds.iter().filter(|b| bounds_dup.insert(*b)).enumerate() {
@@ -183,7 +184,7 @@ impl clean::GenericParamDef {
183184
&'a self,
184185
cx: &'a Context<'tcx>,
185186
) -> impl Display + 'a + Captures<'tcx> {
186-
display_fn(move |f| match &self.kind {
187+
fmt::from_fn(move |f| match &self.kind {
187188
clean::GenericParamDefKind::Lifetime { outlives } => {
188189
write!(f, "{}", self.name)?;
189190

@@ -238,7 +239,7 @@ impl clean::Generics {
238239
&'a self,
239240
cx: &'a Context<'tcx>,
240241
) -> impl Display + 'a + Captures<'tcx> {
241-
display_fn(move |f| {
242+
fmt::from_fn(move |f| {
242243
let mut real_params = self.params.iter().filter(|p| !p.is_synthetic_param()).peekable();
243244
if real_params.peek().is_none() {
244245
return Ok(());
@@ -268,12 +269,12 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
268269
indent: usize,
269270
ending: Ending,
270271
) -> impl Display + 'a + Captures<'tcx> {
271-
display_fn(move |f| {
272+
fmt::from_fn(move |f| {
272273
let mut where_predicates = gens
273274
.where_predicates
274275
.iter()
275276
.map(|pred| {
276-
display_fn(move |f| {
277+
fmt::from_fn(move |f| {
277278
if f.alternate() {
278279
f.write_str(" ")?;
279280
} else {
@@ -376,17 +377,15 @@ impl clean::Lifetime {
376377
impl clean::ConstantKind {
377378
pub(crate) fn print(&self, tcx: TyCtxt<'_>) -> impl Display + '_ {
378379
let expr = self.expr(tcx);
379-
display_fn(
380-
move |f| {
381-
if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) }
382-
},
383-
)
380+
fmt::from_fn(move |f| {
381+
if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) }
382+
})
384383
}
385384
}
386385

387386
impl clean::PolyTrait {
388387
fn print<'a, 'tcx: 'a>(&'a self, cx: &'a Context<'tcx>) -> impl Display + 'a + Captures<'tcx> {
389-
display_fn(move |f| {
388+
fmt::from_fn(move |f| {
390389
print_higher_ranked_params_with_space(&self.generic_params, cx, "for").fmt(f)?;
391390
self.trait_.print(cx).fmt(f)
392391
})
@@ -398,7 +397,7 @@ impl clean::GenericBound {
398397
&'a self,
399398
cx: &'a Context<'tcx>,
400399
) -> impl Display + 'a + Captures<'tcx> {
401-
display_fn(move |f| match self {
400+
fmt::from_fn(move |f| match self {
402401
clean::GenericBound::Outlives(lt) => write!(f, "{}", lt.print()),
403402
clean::GenericBound::TraitBound(ty, modifiers) => {
404403
// `const` and `~const` trait bounds are experimental; don't render them.
@@ -430,7 +429,7 @@ impl clean::GenericBound {
430429

431430
impl clean::GenericArgs {
432431
fn print<'a, 'tcx: 'a>(&'a self, cx: &'a Context<'tcx>) -> impl Display + 'a + Captures<'tcx> {
433-
display_fn(move |f| {
432+
fmt::from_fn(move |f| {
434433
match self {
435434
clean::GenericArgs::AngleBracketed { args, constraints } => {
436435
if !args.is_empty() || !constraints.is_empty() {
@@ -950,7 +949,7 @@ fn tybounds<'a, 'tcx: 'a>(
950949
lt: &'a Option<clean::Lifetime>,
951950
cx: &'a Context<'tcx>,
952951
) -> impl Display + 'a + Captures<'tcx> {
953-
display_fn(move |f| {
952+
fmt::from_fn(move |f| {
954953
for (i, bound) in bounds.iter().enumerate() {
955954
if i > 0 {
956955
write!(f, " + ")?;
@@ -971,7 +970,7 @@ fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
971970
cx: &'a Context<'tcx>,
972971
keyword: &'static str,
973972
) -> impl Display + 'a + Captures<'tcx> {
974-
display_fn(move |f| {
973+
fmt::from_fn(move |f| {
975974
if !params.is_empty() {
976975
f.write_str(keyword)?;
977976
f.write_str(if f.alternate() { "<" } else { "&lt;" })?;
@@ -982,13 +981,13 @@ fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
982981
})
983982
}
984983

985-
pub(crate) fn anchor<'a, 'cx: 'a>(
984+
pub(crate) fn anchor<'a: 'cx, 'cx>(
986985
did: DefId,
987986
text: Symbol,
988-
cx: &'cx Context<'_>,
989-
) -> impl Display + 'a {
990-
let parts = href(did, cx);
991-
display_fn(move |f| {
987+
cx: &'cx Context<'a>,
988+
) -> impl Display + Captures<'a> + 'cx {
989+
fmt::from_fn(move |f| {
990+
let parts = href(did, cx);
992991
if let Ok((url, short_ty, fqp)) = parts {
993992
write!(
994993
f,
@@ -1150,7 +1149,7 @@ fn fmt_type(
11501149
}
11511150
}
11521151
clean::BorrowedRef { lifetime: ref l, mutability, type_: ref ty } => {
1153-
let lt = display_fn(|f| match l {
1152+
let lt = fmt::from_fn(|f| match l {
11541153
Some(l) => write!(f, "{} ", l.print()),
11551154
_ => Ok(()),
11561155
});
@@ -1270,7 +1269,7 @@ impl clean::Type {
12701269
&'a self,
12711270
cx: &'a Context<'tcx>,
12721271
) -> impl Display + 'b + Captures<'tcx> {
1273-
display_fn(move |f| fmt_type(self, f, false, cx))
1272+
fmt::from_fn(move |f| fmt_type(self, f, false, cx))
12741273
}
12751274
}
12761275

@@ -1279,7 +1278,7 @@ impl clean::Path {
12791278
&'a self,
12801279
cx: &'a Context<'tcx>,
12811280
) -> impl Display + 'b + Captures<'tcx> {
1282-
display_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
1281+
fmt::from_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
12831282
}
12841283
}
12851284

@@ -1289,7 +1288,7 @@ impl clean::Impl {
12891288
use_absolute: bool,
12901289
cx: &'a Context<'tcx>,
12911290
) -> impl Display + 'a + Captures<'tcx> {
1292-
display_fn(move |f| {
1291+
fmt::from_fn(move |f| {
12931292
f.write_str("impl")?;
12941293
self.generics.print(cx).fmt(f)?;
12951294
f.write_str(" ")?;
@@ -1407,7 +1406,7 @@ impl clean::Arguments {
14071406
&'a self,
14081407
cx: &'a Context<'tcx>,
14091408
) -> impl Display + 'a + Captures<'tcx> {
1410-
display_fn(move |f| {
1409+
fmt::from_fn(move |f| {
14111410
for (i, input) in self.values.iter().enumerate() {
14121411
write!(f, "{}: ", input.name)?;
14131412
input.type_.print(cx).fmt(f)?;
@@ -1447,7 +1446,7 @@ impl clean::FnDecl {
14471446
&'a self,
14481447
cx: &'a Context<'tcx>,
14491448
) -> impl Display + 'b + Captures<'tcx> {
1450-
display_fn(move |f| {
1449+
fmt::from_fn(move |f| {
14511450
let ellipsis = if self.c_variadic { ", ..." } else { "" };
14521451
if f.alternate() {
14531452
write!(
@@ -1481,10 +1480,10 @@ impl clean::FnDecl {
14811480
indent: usize,
14821481
cx: &'a Context<'tcx>,
14831482
) -> impl Display + 'a + Captures<'tcx> {
1484-
display_fn(move |f| {
1483+
fmt::from_fn(move |f| {
14851484
// First, generate the text form of the declaration, with no line wrapping, and count the bytes.
14861485
let mut counter = WriteCounter(0);
1487-
write!(&mut counter, "{:#}", display_fn(|f| { self.inner_full_print(None, f, cx) }))
1486+
write!(&mut counter, "{:#}", fmt::from_fn(|f| { self.inner_full_print(None, f, cx) }))
14881487
.unwrap();
14891488
// If the text form was over 80 characters wide, we will line-wrap our output.
14901489
let line_wrapping_indent =
@@ -1566,7 +1565,7 @@ impl clean::FnDecl {
15661565
&'a self,
15671566
cx: &'a Context<'tcx>,
15681567
) -> impl Display + 'a + Captures<'tcx> {
1569-
display_fn(move |f| match &self.output {
1568+
fmt::from_fn(move |f| match &self.output {
15701569
clean::Tuple(tys) if tys.is_empty() => Ok(()),
15711570
ty if f.alternate() => {
15721571
write!(f, " -> {:#}", ty.print(cx))
@@ -1618,7 +1617,7 @@ pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
16181617
};
16191618

16201619
let is_doc_hidden = item.is_doc_hidden();
1621-
display_fn(move |f| {
1620+
fmt::from_fn(move |f| {
16221621
if is_doc_hidden {
16231622
f.write_str("#[doc(hidden)] ")?;
16241623
}
@@ -1692,7 +1691,7 @@ impl clean::Import {
16921691
&'a self,
16931692
cx: &'a Context<'tcx>,
16941693
) -> impl Display + 'a + Captures<'tcx> {
1695-
display_fn(move |f| match self.kind {
1694+
fmt::from_fn(move |f| match self.kind {
16961695
clean::ImportKind::Simple(name) => {
16971696
if name == self.source.path.last() {
16981697
write!(f, "use {};", self.source.print(cx))
@@ -1716,7 +1715,7 @@ impl clean::ImportSource {
17161715
&'a self,
17171716
cx: &'a Context<'tcx>,
17181717
) -> impl Display + 'a + Captures<'tcx> {
1719-
display_fn(move |f| match self.did {
1718+
fmt::from_fn(move |f| match self.did {
17201719
Some(did) => resolved_path(f, did, &self.path, true, false, cx),
17211720
_ => {
17221721
for seg in &self.path.segments[..self.path.segments.len() - 1] {
@@ -1744,7 +1743,7 @@ impl clean::AssocItemConstraint {
17441743
&'a self,
17451744
cx: &'a Context<'tcx>,
17461745
) -> impl Display + 'a + Captures<'tcx> {
1747-
display_fn(move |f| {
1746+
fmt::from_fn(move |f| {
17481747
f.write_str(self.assoc.name.as_str())?;
17491748
self.assoc.args.print(cx).fmt(f)?;
17501749
match self.kind {
@@ -1765,7 +1764,7 @@ impl clean::AssocItemConstraint {
17651764
}
17661765

17671766
pub(crate) fn print_abi_with_space(abi: ExternAbi) -> impl Display {
1768-
display_fn(move |f| {
1767+
fmt::from_fn(move |f| {
17691768
let quot = if f.alternate() { "\"" } else { "&quot;" };
17701769
match abi {
17711770
ExternAbi::Rust => Ok(()),
@@ -1783,7 +1782,7 @@ impl clean::GenericArg {
17831782
&'a self,
17841783
cx: &'a Context<'tcx>,
17851784
) -> impl Display + 'a + Captures<'tcx> {
1786-
display_fn(move |f| match self {
1785+
fmt::from_fn(move |f| match self {
17871786
clean::GenericArg::Lifetime(lt) => lt.print().fmt(f),
17881787
clean::GenericArg::Type(ty) => ty.print(cx).fmt(f),
17891788
clean::GenericArg::Const(ct) => ct.print(cx.tcx()).fmt(f),
@@ -1797,24 +1796,9 @@ impl clean::Term {
17971796
&'a self,
17981797
cx: &'a Context<'tcx>,
17991798
) -> impl Display + 'a + Captures<'tcx> {
1800-
display_fn(move |f| match self {
1799+
fmt::from_fn(move |f| match self {
18011800
clean::Term::Type(ty) => ty.print(cx).fmt(f),
18021801
clean::Term::Constant(ct) => ct.print(cx.tcx()).fmt(f),
18031802
})
18041803
}
18051804
}
1806-
1807-
pub(crate) fn display_fn(f: impl FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl Display {
1808-
struct WithFormatter<F>(Cell<Option<F>>);
1809-
1810-
impl<F> Display for WithFormatter<F>
1811-
where
1812-
F: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result,
1813-
{
1814-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1815-
(self.0.take()).unwrap()(f)
1816-
}
1817-
}
1818-
1819-
WithFormatter(Cell::new(Some(f)))
1820-
}

‎src/librustdoc/html/highlight.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub(super) fn write_code(
233233
out: &mut impl Write,
234234
src: &str,
235235
href_context: Option<HrefContext<'_, '_>>,
236-
decoration_info: Option<DecorationInfo>,
236+
decoration_info: Option<&DecorationInfo>,
237237
) {
238238
// This replace allows to fix how the code source with DOS backline characters is displayed.
239239
let src = src.replace("\r\n", "\n");
@@ -510,12 +510,12 @@ struct Decorations {
510510
}
511511

512512
impl Decorations {
513-
fn new(info: DecorationInfo) -> Self {
513+
fn new(info: &DecorationInfo) -> Self {
514514
// Extract tuples (start, end, kind) into separate sequences of (start, kind) and (end).
515515
let (mut starts, mut ends): (Vec<_>, Vec<_>) = info
516516
.0
517-
.into_iter()
518-
.flat_map(|(kind, ranges)| ranges.into_iter().map(move |(lo, hi)| ((lo, kind), hi)))
517+
.iter()
518+
.flat_map(|(&kind, ranges)| ranges.into_iter().map(move |&(lo, hi)| ((lo, kind), hi)))
519519
.unzip();
520520

521521
// Sort the sequences in document order.
@@ -542,7 +542,7 @@ struct Classifier<'src> {
542542
impl<'src> Classifier<'src> {
543543
/// Takes as argument the source code to HTML-ify, the rust edition to use and the source code
544544
/// file span which will be used later on by the `span_correspondence_map`.
545-
fn new(src: &str, file_span: Span, decoration_info: Option<DecorationInfo>) -> Classifier<'_> {
545+
fn new(src: &'src str, file_span: Span, decoration_info: Option<&DecorationInfo>) -> Self {
546546
let tokens = PeekIter::new(TokenIter { src, cursor: Cursor::new(src) });
547547
let decorations = decoration_info.map(Decorations::new);
548548
Classifier {

‎src/librustdoc/html/highlight/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ let a = 4;";
7878
decorations.insert("example2", vec![(22, 32)]);
7979

8080
let mut html = Buffer::new();
81-
write_code(&mut html, src, None, Some(DecorationInfo(decorations)));
81+
write_code(&mut html, src, None, Some(&DecorationInfo(decorations)));
8282
expect_file!["fixtures/decorations.html"].assert_eq(&html.into_inner());
8383
});
8484
}

‎src/librustdoc/html/render/mod.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ use crate::formats::cache::Cache;
6969
use crate::formats::item_type::ItemType;
7070
use crate::html::escape::Escape;
7171
use crate::html::format::{
72-
Buffer, Ending, HrefError, PrintWithSpace, display_fn, href, join_with_double_colon,
73-
print_abi_with_space, print_constness_with_space, print_default_space, print_generic_bounds,
74-
print_where_clause, visibility_print_with_space,
72+
Buffer, Ending, HrefError, PrintWithSpace, href, join_with_double_colon, print_abi_with_space,
73+
print_constness_with_space, print_default_space, print_generic_bounds, print_where_clause,
74+
visibility_print_with_space,
7575
};
7676
use crate::html::markdown::{
7777
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
@@ -82,13 +82,14 @@ use crate::scrape_examples::{CallData, CallLocation};
8282
use crate::{DOC_RUST_LANG_ORG_CHANNEL, try_none};
8383

8484
pub(crate) fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ {
85-
crate::html::format::display_fn(move |f| {
85+
fmt::from_fn(move |f| {
8686
if !v.ends_with('/') && !v.is_empty() { write!(f, "{v}/") } else { f.write_str(v) }
8787
})
8888
}
8989

9090
/// Specifies whether rendering directly implemented trait items or ones from a certain Deref
9191
/// impl.
92+
#[derive(Copy, Clone)]
9293
pub(crate) enum AssocItemRender<'a> {
9394
All,
9495
DerefFor { trait_: &'a clean::Path, type_: &'a clean::Type, deref_mut_: bool },
@@ -309,9 +310,7 @@ impl ItemEntry {
309310

310311
impl ItemEntry {
311312
pub(crate) fn print(&self) -> impl fmt::Display + '_ {
312-
crate::html::format::display_fn(move |f| {
313-
write!(f, "<a href=\"{}\">{}</a>", self.url, Escape(&self.name))
314-
})
313+
fmt::from_fn(move |f| write!(f, "<a href=\"{}\">{}</a>", self.url, Escape(&self.name)))
315314
}
316315
}
317316

@@ -513,7 +512,7 @@ fn document<'a, 'cx: 'a>(
513512
info!("Documenting {name}");
514513
}
515514

516-
display_fn(move |f| {
515+
fmt::from_fn(move |f| {
517516
document_item_info(cx, item, parent).render_into(f).unwrap();
518517
if parent.is_none() {
519518
write!(f, "{}", document_full_collapsible(item, cx, heading_offset))
@@ -530,7 +529,7 @@ fn render_markdown<'a, 'cx: 'a>(
530529
links: Vec<RenderedLink>,
531530
heading_offset: HeadingOffset,
532531
) -> impl fmt::Display + 'a + Captures<'cx> {
533-
display_fn(move |f| {
532+
fmt::from_fn(move |f| {
534533
write!(
535534
f,
536535
"<div class=\"docblock\">{}</div>",
@@ -557,7 +556,7 @@ fn document_short<'a, 'cx: 'a>(
557556
parent: &'a clean::Item,
558557
show_def_docs: bool,
559558
) -> impl fmt::Display + 'a + Captures<'cx> {
560-
display_fn(move |f| {
559+
fmt::from_fn(move |f| {
561560
document_item_info(cx, item, Some(parent)).render_into(f).unwrap();
562561
if !show_def_docs {
563562
return Ok(());
@@ -605,7 +604,7 @@ fn document_full_inner<'a, 'cx: 'a>(
605604
is_collapsible: bool,
606605
heading_offset: HeadingOffset,
607606
) -> impl fmt::Display + 'a + Captures<'cx> {
608-
display_fn(move |f| {
607+
fmt::from_fn(move |f| {
609608
if let Some(s) = item.opt_doc_value() {
610609
debug!("Doc block: =====\n{s}\n=====");
611610
if is_collapsible {
@@ -1159,7 +1158,7 @@ fn render_attributes_in_pre<'a, 'tcx: 'a>(
11591158
prefix: &'a str,
11601159
cx: &'a Context<'tcx>,
11611160
) -> impl fmt::Display + Captures<'a> + Captures<'tcx> {
1162-
crate::html::format::display_fn(move |f| {
1161+
fmt::from_fn(move |f| {
11631162
for a in it.attributes(cx.tcx(), cx.cache(), false) {
11641163
writeln!(f, "{prefix}{a}")?;
11651164
}
@@ -1256,9 +1255,9 @@ fn render_assoc_items<'a, 'cx: 'a>(
12561255
it: DefId,
12571256
what: AssocItemRender<'a>,
12581257
) -> impl fmt::Display + 'a + Captures<'cx> {
1259-
let mut derefs = DefIdSet::default();
1260-
derefs.insert(it);
1261-
display_fn(move |f| {
1258+
fmt::from_fn(move |f| {
1259+
let mut derefs = DefIdSet::default();
1260+
derefs.insert(it);
12621261
render_assoc_items_inner(f, cx, containing_item, it, what, &mut derefs);
12631262
Ok(())
12641263
})
@@ -2577,7 +2576,7 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean
25772576
file_span,
25782577
cx,
25792578
&cx.root_path(),
2580-
highlight::DecorationInfo(decoration_info),
2579+
&highlight::DecorationInfo(decoration_info),
25812580
sources::SourceContext::Embedded(sources::ScrapedInfo {
25822581
needs_expansion,
25832582
offset: line_min,

‎src/librustdoc/html/render/print_item.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::formats::Impl;
3030
use crate::formats::item_type::ItemType;
3131
use crate::html::escape::{Escape, EscapeBodyTextWithWbr};
3232
use crate::html::format::{
33-
Buffer, Ending, PrintWithSpace, display_fn, join_with_double_colon, print_abi_with_space,
33+
Buffer, Ending, PrintWithSpace, join_with_double_colon, print_abi_with_space,
3434
print_constness_with_space, print_where_clause, visibility_print_with_space,
3535
};
3636
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
@@ -92,7 +92,7 @@ macro_rules! item_template_methods {
9292
() => {};
9393
(document $($rest:tt)*) => {
9494
fn document<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
95-
display_fn(move |f| {
95+
fmt::from_fn(move |f| {
9696
let (item, cx) = self.item_and_cx();
9797
let v = document(cx, item, None, HeadingOffset::H2);
9898
write!(f, "{v}")
@@ -102,7 +102,7 @@ macro_rules! item_template_methods {
102102
};
103103
(document_type_layout $($rest:tt)*) => {
104104
fn document_type_layout<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
105-
display_fn(move |f| {
105+
fmt::from_fn(move |f| {
106106
let (item, cx) = self.item_and_cx();
107107
let def_id = item.item_id.expect_def_id();
108108
let v = document_type_layout(cx, def_id);
@@ -113,7 +113,7 @@ macro_rules! item_template_methods {
113113
};
114114
(render_attributes_in_pre $($rest:tt)*) => {
115115
fn render_attributes_in_pre<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
116-
display_fn(move |f| {
116+
fmt::from_fn(move |f| {
117117
let (item, cx) = self.item_and_cx();
118118
let v = render_attributes_in_pre(item, "", cx);
119119
write!(f, "{v}")
@@ -123,7 +123,7 @@ macro_rules! item_template_methods {
123123
};
124124
(render_assoc_items $($rest:tt)*) => {
125125
fn render_assoc_items<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
126-
display_fn(move |f| {
126+
fmt::from_fn(move |f| {
127127
let (item, cx) = self.item_and_cx();
128128
let def_id = item.item_id.expect_def_id();
129129
let v = render_assoc_items(cx, item, def_id, AssocItemRender::All);
@@ -520,13 +520,13 @@ fn extra_info_tags<'a, 'tcx: 'a>(
520520
parent: &'a clean::Item,
521521
import_def_id: Option<DefId>,
522522
) -> impl fmt::Display + 'a + Captures<'tcx> {
523-
display_fn(move |f| {
523+
fmt::from_fn(move |f| {
524524
fn tag_html<'a>(
525525
class: &'a str,
526526
title: &'a str,
527527
contents: &'a str,
528528
) -> impl fmt::Display + 'a {
529-
display_fn(move |f| {
529+
fmt::from_fn(move |f| {
530530
write!(
531531
f,
532532
r#"<wbr><span class="stab {class}" title="{title}">{contents}</span>"#,
@@ -1376,7 +1376,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
13761376

13771377
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
13781378
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1379-
display_fn(move |f| {
1379+
fmt::from_fn(move |f| {
13801380
let v = render_union(self.it, Some(&self.s.generics), &self.s.fields, self.cx);
13811381
write!(f, "{v}")
13821382
})
@@ -1386,7 +1386,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
13861386
&'b self,
13871387
field: &'a clean::Item,
13881388
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1389-
display_fn(move |f| {
1389+
fmt::from_fn(move |f| {
13901390
let v = document(self.cx, field, Some(self.it), HeadingOffset::H3);
13911391
write!(f, "{v}")
13921392
})
@@ -1400,7 +1400,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
14001400
&'b self,
14011401
ty: &'a clean::Type,
14021402
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1403-
display_fn(move |f| {
1403+
fmt::from_fn(move |f| {
14041404
let v = ty.print(self.cx);
14051405
write!(f, "{v}")
14061406
})
@@ -1427,7 +1427,7 @@ fn print_tuple_struct_fields<'a, 'cx: 'a>(
14271427
cx: &'a Context<'cx>,
14281428
s: &'a [clean::Item],
14291429
) -> impl fmt::Display + 'a + Captures<'cx> {
1430-
display_fn(|f| {
1430+
fmt::from_fn(|f| {
14311431
if !s.is_empty()
14321432
&& s.iter().all(|field| {
14331433
matches!(field.kind, clean::StrippedItem(box clean::StructFieldItem(..)))
@@ -2152,7 +2152,7 @@ fn render_union<'a, 'cx: 'a>(
21522152
fields: &'a [clean::Item],
21532153
cx: &'a Context<'cx>,
21542154
) -> impl fmt::Display + 'a + Captures<'cx> {
2155-
display_fn(move |mut f| {
2155+
fmt::from_fn(move |mut f| {
21562156
write!(f, "{}union {}", visibility_print_with_space(it, cx), it.name.unwrap(),)?;
21572157

21582158
let where_displayed = g
@@ -2332,7 +2332,7 @@ fn document_non_exhaustive_header(item: &clean::Item) -> &str {
23322332
}
23332333

23342334
fn document_non_exhaustive(item: &clean::Item) -> impl fmt::Display + '_ {
2335-
display_fn(|f| {
2335+
fmt::from_fn(|f| {
23362336
if item.is_non_exhaustive() {
23372337
write!(
23382338
f,

‎src/librustdoc/html/render/sidebar.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,17 @@ impl<'a> Link<'a> {
100100
}
101101

102102
pub(crate) mod filters {
103-
use std::fmt::Display;
103+
use std::fmt::{self, Display};
104104

105105
use rinja::filters::Safe;
106106

107107
use crate::html::escape::EscapeBodyTextWithWbr;
108-
use crate::html::render::display_fn;
109108
pub(crate) fn wrapped<T>(v: T) -> rinja::Result<Safe<impl Display>>
110109
where
111110
T: Display,
112111
{
113112
let string = v.to_string();
114-
Ok(Safe(display_fn(move |f| EscapeBodyTextWithWbr(&string).fmt(f))))
113+
Ok(Safe(fmt::from_fn(move |f| EscapeBodyTextWithWbr(&string).fmt(f))))
115114
}
116115
}
117116

‎src/librustdoc/html/render/type_layout.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_middle::ty::layout::LayoutError;
99
use rustc_middle::ty::{self};
1010
use rustc_span::symbol::Symbol;
1111

12-
use crate::html::format::display_fn;
1312
use crate::html::render::Context;
1413

1514
#[derive(Template)]
@@ -31,7 +30,7 @@ pub(crate) fn document_type_layout<'a, 'cx: 'a>(
3130
cx: &'a Context<'cx>,
3231
ty_def_id: DefId,
3332
) -> impl fmt::Display + 'a + Captures<'cx> {
34-
display_fn(move |f| {
33+
fmt::from_fn(move |f| {
3534
if !cx.shared.show_type_layout {
3635
return Ok(());
3736
}

‎src/librustdoc/html/sources.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::clean::utils::has_doc_flag;
1717
use crate::docfs::PathError;
1818
use crate::error::Error;
1919
use crate::html::render::Context;
20-
use crate::html::{format, highlight, layout};
20+
use crate::html::{highlight, layout};
2121
use crate::visit::DocVisitor;
2222

2323
pub(crate) fn render(cx: &mut Context<'_>, krate: &clean::Crate) -> Result<(), Error> {
@@ -249,7 +249,7 @@ impl SourceCollector<'_, '_> {
249249
file_span,
250250
self.cx,
251251
&root_path,
252-
highlight::DecorationInfo::default(),
252+
&highlight::DecorationInfo::default(),
253253
SourceContext::Standalone { file_path },
254254
)
255255
},
@@ -328,13 +328,13 @@ pub(crate) fn print_src(
328328
file_span: rustc_span::Span,
329329
context: &Context<'_>,
330330
root_path: &str,
331-
decoration_info: highlight::DecorationInfo,
331+
decoration_info: &highlight::DecorationInfo,
332332
source_context: SourceContext<'_>,
333333
) {
334-
let current_href = context
335-
.href_from_span(clean::Span::new(file_span), false)
336-
.expect("only local crates should have sources emitted");
337-
let code = format::display_fn(move |fmt| {
334+
let code = fmt::from_fn(move |fmt| {
335+
let current_href = context
336+
.href_from_span(clean::Span::new(file_span), false)
337+
.expect("only local crates should have sources emitted");
338338
highlight::write_code(
339339
fmt,
340340
s,

‎src/librustdoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(rustc_private)]
66
#![feature(assert_matches)]
77
#![feature(box_patterns)]
8+
#![feature(debug_closure_helpers)]
89
#![feature(file_buffered)]
910
#![feature(if_let_guard)]
1011
#![feature(impl_trait_in_assoc_type)]

0 commit comments

Comments
 (0)
This repository has been archived.