make AllTypes::print return impl fmt::Display

This commit is contained in:
Yotam Ofek 2025-03-05 09:24:09 +00:00
parent 69b3959afe
commit 250a1aa7a6
3 changed files with 41 additions and 45 deletions

View file

@ -650,15 +650,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
bar.render_into(&mut sidebar).unwrap();
let v = layout::render(
&shared.layout,
&page,
sidebar,
BufDisplay(|buf: &mut String| {
all.print(buf);
}),
&shared.style_files,
);
let v = layout::render(&shared.layout, &page, sidebar, all.print(), &shared.style_files);
shared.fs.write(final_file, v)?;
// if to avoid writing help, settings files to doc root unless we're on the final invocation

View file

@ -439,44 +439,49 @@ impl AllTypes {
sections
}
fn print(&self, f: &mut String) {
fn print_entries(f: &mut String, e: &FxIndexSet<ItemEntry>, kind: ItemSection) {
if !e.is_empty() {
let mut e: Vec<&ItemEntry> = e.iter().collect();
e.sort();
write_str(
f,
format_args!(
"<h3 id=\"{id}\">{title}</h3><ul class=\"all-items\">",
id = kind.id(),
title = kind.name(),
),
);
for s in e.iter() {
write_str(f, format_args!("<li>{}</li>", s.print()));
fn print(&self) -> impl fmt::Display {
fn print_entries(e: &FxIndexSet<ItemEntry>, kind: ItemSection) -> impl fmt::Display {
fmt::from_fn(move |f| {
if e.is_empty() {
return Ok(());
}
f.push_str("</ul>");
}
let mut e: Vec<&ItemEntry> = e.iter().collect();
e.sort();
write!(
f,
"<h3 id=\"{id}\">{title}</h3><ul class=\"all-items\">",
id = kind.id(),
title = kind.name(),
)?;
for s in e.iter() {
write!(f, "<li>{}</li>", s.print())?;
}
f.write_str("</ul>")
})
}
f.push_str("<h1>List of all items</h1>");
// Note: print_entries does not escape the title, because we know the current set of titles
// doesn't require escaping.
print_entries(f, &self.structs, ItemSection::Structs);
print_entries(f, &self.enums, ItemSection::Enums);
print_entries(f, &self.unions, ItemSection::Unions);
print_entries(f, &self.primitives, ItemSection::PrimitiveTypes);
print_entries(f, &self.traits, ItemSection::Traits);
print_entries(f, &self.macros, ItemSection::Macros);
print_entries(f, &self.attribute_macros, ItemSection::AttributeMacros);
print_entries(f, &self.derive_macros, ItemSection::DeriveMacros);
print_entries(f, &self.functions, ItemSection::Functions);
print_entries(f, &self.type_aliases, ItemSection::TypeAliases);
print_entries(f, &self.trait_aliases, ItemSection::TraitAliases);
print_entries(f, &self.statics, ItemSection::Statics);
print_entries(f, &self.constants, ItemSection::Constants);
fmt::from_fn(|f| {
f.write_str("<h1>List of all items</h1>")?;
// Note: print_entries does not escape the title, because we know the current set of titles
// doesn't require escaping.
print_entries(&self.structs, ItemSection::Structs).fmt(f)?;
print_entries(&self.enums, ItemSection::Enums).fmt(f)?;
print_entries(&self.unions, ItemSection::Unions).fmt(f)?;
print_entries(&self.primitives, ItemSection::PrimitiveTypes).fmt(f)?;
print_entries(&self.traits, ItemSection::Traits).fmt(f)?;
print_entries(&self.macros, ItemSection::Macros).fmt(f)?;
print_entries(&self.attribute_macros, ItemSection::AttributeMacros).fmt(f)?;
print_entries(&self.derive_macros, ItemSection::DeriveMacros).fmt(f)?;
print_entries(&self.functions, ItemSection::Functions).fmt(f)?;
print_entries(&self.type_aliases, ItemSection::TypeAliases).fmt(f)?;
print_entries(&self.trait_aliases, ItemSection::TraitAliases).fmt(f)?;
print_entries(&self.statics, ItemSection::Statics).fmt(f)?;
print_entries(&self.constants, ItemSection::Constants).fmt(f)?;
Ok(())
})
}
}

View file

@ -47,8 +47,7 @@ fn test_all_types_prints_header_once() {
// Regression test for #82477
let all_types = AllTypes::new();
let mut buffer = String::new();
all_types.print(&mut buffer);
let buffer = all_types.print().to_string();
assert_eq!(1, buffer.matches("List of all items").count());
}