Skip to content

Commit 8edf854

Browse files
committed
make AllTypes::print return impl fmt::Display
1 parent f820b75 commit 8edf854

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
@@ -436,44 +436,49 @@ impl AllTypes {
436436
sections
437437
}
438438

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

453455
for s in e.iter() {
454-
write_str(f, format_args!("<li>{}</li>", s.print()));
456+
write!(f, "<li>{}</li>", s.print())?;
455457
}
456458

457-
f.push_str("</ul>");
458-
}
459+
f.write_str("</ul>")
460+
})
459461
}
460462

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

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)