Remove unneeded Buffer allocations when &mut fmt::Write can be used directly

This commit is contained in:
Guillaume Gomez 2023-06-03 17:08:29 +02:00
parent 1e17cef9e2
commit 5c77a0d7a7
2 changed files with 18 additions and 23 deletions

View File

@ -1038,9 +1038,9 @@ fn render_attributes_in_pre<'a, 'b: 'a>(
// When an attribute is rendered inside a <code> tag, it is formatted using // When an attribute is rendered inside a <code> tag, it is formatted using
// a div to produce a newline after it. // a div to produce a newline after it.
fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item, tcx: TyCtxt<'_>) { fn render_attributes_in_code(w: &mut impl fmt::Write, it: &clean::Item, tcx: TyCtxt<'_>) {
for a in it.attributes(tcx, false) { for a in it.attributes(tcx, false) {
write!(w, "<div class=\"code-attribute\">{}</div>", a); write!(w, "<div class=\"code-attribute\">{}</div>", a).unwrap();
} }
} }

View File

@ -1431,30 +1431,28 @@ fn item_proc_macro(
it: &clean::Item, it: &clean::Item,
m: &clean::ProcMacro, m: &clean::ProcMacro,
) { ) {
let mut buffer = Buffer::new(); wrap_item(w, |buffer| {
wrap_item(&mut buffer, |buffer| {
let name = it.name.expect("proc-macros always have names"); let name = it.name.expect("proc-macros always have names");
match m.kind { match m.kind {
MacroKind::Bang => { MacroKind::Bang => {
write!(buffer, "{}!() {{ /* proc-macro */ }}", name); write!(buffer, "{}!() {{ /* proc-macro */ }}", name).unwrap();
} }
MacroKind::Attr => { MacroKind::Attr => {
write!(buffer, "#[{}]", name); write!(buffer, "#[{}]", name).unwrap();
} }
MacroKind::Derive => { MacroKind::Derive => {
write!(buffer, "#[derive({})]", name); write!(buffer, "#[derive({})]", name).unwrap();
if !m.helpers.is_empty() { if !m.helpers.is_empty() {
buffer.push_str("\n{\n"); buffer.write_str("\n{\n // Attributes available to this derive:\n").unwrap();
buffer.push_str(" // Attributes available to this derive:\n");
for attr in &m.helpers { for attr in &m.helpers {
writeln!(buffer, " #[{}]", attr); writeln!(buffer, " #[{}]", attr).unwrap();
} }
buffer.push_str("}\n"); buffer.write_str("}\n").unwrap();
} }
} }
} }
}); });
write!(w, "{}{}", buffer.into_inner(), document(cx, it, None, HeadingOffset::H2)).unwrap(); write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap();
} }
fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
@ -1571,8 +1569,7 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
} }
fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) { fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) {
let mut buffer = Buffer::new(); wrap_item(w, |buffer| {
wrap_item(&mut buffer, |buffer| {
render_attributes_in_code(buffer, it, cx.tcx()); render_attributes_in_code(buffer, it, cx.tcx());
write!( write!(
buffer, buffer,
@ -1581,29 +1578,27 @@ fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item,
mutability = s.mutability.print_with_space(), mutability = s.mutability.print_with_space(),
name = it.name.unwrap(), name = it.name.unwrap(),
typ = s.type_.print(cx) typ = s.type_.print(cx)
); )
.unwrap();
}); });
write!(w, "{}", buffer.into_inner()).unwrap();
write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap(); write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap();
} }
fn item_foreign_type(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item) { fn item_foreign_type(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item) {
let mut buffer = Buffer::new(); wrap_item(w, |buffer| {
wrap_item(&mut buffer, |buffer| { buffer.write_str("extern {\n").unwrap();
buffer.write_str("extern {\n");
render_attributes_in_code(buffer, it, cx.tcx()); render_attributes_in_code(buffer, it, cx.tcx());
write!( write!(
buffer, buffer,
" {}type {};\n}}", " {}type {};\n}}",
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx), visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
it.name.unwrap(), it.name.unwrap(),
); )
.unwrap();
}); });
write!(w, "{}{}", buffer.into_inner(), document(cx, it, None, HeadingOffset::H2)).unwrap(); write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap();
write!(w, "{}", render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All)) write!(w, "{}", render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All))
.unwrap(); .unwrap();
} }