Skip to content

Commit 250a1aa

Browse files
committed
make AllTypes::print return impl fmt::Display
1 parent 69b3959 commit 250a1aa

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

src/librustdoc/html/render/context.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -650,15 +650,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
650650

651651
bar.render_into(&mut sidebar).unwrap();
652652

653-
let v = layout::render(
654-
&shared.layout,
655-
&page,
656-
sidebar,
657-
BufDisplay(|buf: &mut String| {
658-
all.print(buf);
659-
}),
660-
&shared.style_files,
661-
);
653+
let v = layout::render(&shared.layout, &page, sidebar, all.print(), &shared.style_files);
662654
shared.fs.write(final_file, v)?;
663655

664656
// if to avoid writing help, settings files to doc root unless we're on the final invocation

src/librustdoc/html/render/mod.rs

+34-29
Original file line numberDiff line numberDiff line change
@@ -439,44 +439,49 @@ impl AllTypes {
439439
sections
440440
}
441441

442-
fn print(&self, f: &mut String) {
443-
fn print_entries(f: &mut String, e: &FxIndexSet<ItemEntry>, kind: ItemSection) {
444-
if !e.is_empty() {
442+
fn print(&self) -> impl fmt::Display {
443+
fn print_entries(e: &FxIndexSet<ItemEntry>, kind: ItemSection) -> impl fmt::Display {
444+
fmt::from_fn(move |f| {
445+
if e.is_empty() {
446+
return Ok(());
447+
}
448+
445449
let mut e: Vec<&ItemEntry> = e.iter().collect();
446450
e.sort();
447-
write_str(
451+
write!(
448452
f,
449-
format_args!(
450-
"<h3 id=\"{id}\">{title}</h3><ul class=\"all-items\">",
451-
id = kind.id(),
452-
title = kind.name(),
453-
),
454-
);
453+
"<h3 id=\"{id}\">{title}</h3><ul class=\"all-items\">",
454+
id = kind.id(),
455+
title = kind.name(),
456+
)?;
455457

456458
for s in e.iter() {
457-
write_str(f, format_args!("<li>{}</li>", s.print()));
459+
write!(f, "<li>{}</li>", s.print())?;
458460
}
459461

460-
f.push_str("</ul>");
461-
}
462+
f.write_str("</ul>")
463+
})
462464
}
463465

464-
f.push_str("<h1>List of all items</h1>");
465-
// Note: print_entries does not escape the title, because we know the current set of titles
466-
// doesn't require escaping.
467-
print_entries(f, &self.structs, ItemSection::Structs);
468-
print_entries(f, &self.enums, ItemSection::Enums);
469-
print_entries(f, &self.unions, ItemSection::Unions);
470-
print_entries(f, &self.primitives, ItemSection::PrimitiveTypes);
471-
print_entries(f, &self.traits, ItemSection::Traits);
472-
print_entries(f, &self.macros, ItemSection::Macros);
473-
print_entries(f, &self.attribute_macros, ItemSection::AttributeMacros);
474-
print_entries(f, &self.derive_macros, ItemSection::DeriveMacros);
475-
print_entries(f, &self.functions, ItemSection::Functions);
476-
print_entries(f, &self.type_aliases, ItemSection::TypeAliases);
477-
print_entries(f, &self.trait_aliases, ItemSection::TraitAliases);
478-
print_entries(f, &self.statics, ItemSection::Statics);
479-
print_entries(f, &self.constants, ItemSection::Constants);
466+
fmt::from_fn(|f| {
467+
f.write_str("<h1>List of all items</h1>")?;
468+
// Note: print_entries does not escape the title, because we know the current set of titles
469+
// doesn't require escaping.
470+
print_entries(&self.structs, ItemSection::Structs).fmt(f)?;
471+
print_entries(&self.enums, ItemSection::Enums).fmt(f)?;
472+
print_entries(&self.unions, ItemSection::Unions).fmt(f)?;
473+
print_entries(&self.primitives, ItemSection::PrimitiveTypes).fmt(f)?;
474+
print_entries(&self.traits, ItemSection::Traits).fmt(f)?;
475+
print_entries(&self.macros, ItemSection::Macros).fmt(f)?;
476+
print_entries(&self.attribute_macros, ItemSection::AttributeMacros).fmt(f)?;
477+
print_entries(&self.derive_macros, ItemSection::DeriveMacros).fmt(f)?;
478+
print_entries(&self.functions, ItemSection::Functions).fmt(f)?;
479+
print_entries(&self.type_aliases, ItemSection::TypeAliases).fmt(f)?;
480+
print_entries(&self.trait_aliases, ItemSection::TraitAliases).fmt(f)?;
481+
print_entries(&self.statics, ItemSection::Statics).fmt(f)?;
482+
print_entries(&self.constants, ItemSection::Constants).fmt(f)?;
483+
Ok(())
484+
})
480485
}
481486
}
482487

src/librustdoc/html/render/tests.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ fn test_all_types_prints_header_once() {
4747
// Regression test for #82477
4848
let all_types = AllTypes::new();
4949

50-
let mut buffer = String::new();
51-
all_types.print(&mut buffer);
50+
let buffer = all_types.print().to_string();
5251

5352
assert_eq!(1, buffer.matches("List of all items").count());
5453
}

0 commit comments

Comments
 (0)