Skip to content

Commit

Permalink
Rollup merge of #112243 - GuillaumeGomez:rm-unneeded-buffer-creations…
Browse files Browse the repository at this point in the history
…, r=notriddle

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

With the recent changes, `wrap_item` can now directly take `&mut Write`, which makes some `Buffer` creations unneeded.

r? `@notriddle`
  • Loading branch information
GuillaumeGomez committed Jun 5, 2023
2 parents 7452822 + 48c46f2 commit aabffef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,9 @@ fn render_attributes_in_pre<'a, 'b: 'a>(

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

Expand Down
37 changes: 16 additions & 21 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,30 +1431,28 @@ fn item_proc_macro(
it: &clean::Item,
m: &clean::ProcMacro,
) {
let mut buffer = Buffer::new();
wrap_item(&mut buffer, |buffer| {
wrap_item(w, |buffer| {
let name = it.name.expect("proc-macros always have names");
match m.kind {
MacroKind::Bang => {
write!(buffer, "{}!() {{ /* proc-macro */ }}", name);
write!(buffer, "{name}!() {{ /* proc-macro */ }}").unwrap();
}
MacroKind::Attr => {
write!(buffer, "#[{}]", name);
write!(buffer, "#[{name}]").unwrap();
}
MacroKind::Derive => {
write!(buffer, "#[derive({})]", name);
write!(buffer, "#[derive({name})]").unwrap();
if !m.helpers.is_empty() {
buffer.push_str("\n{\n");
buffer.push_str(" // Attributes available to this derive:\n");
buffer.write_str("\n{\n // Attributes available to this derive:\n").unwrap();
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 impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item) {
Expand Down Expand Up @@ -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) {
let mut buffer = Buffer::new();
wrap_item(&mut buffer, |buffer| {
wrap_item(w, |buffer| {
render_attributes_in_code(buffer, it, cx.tcx());
write!(
buffer,
Expand All @@ -1581,29 +1578,27 @@ fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item,
mutability = s.mutability.print_with_space(),
name = it.name.unwrap(),
typ = s.type_.print(cx)
);
)
.unwrap();
});

write!(w, "{}", buffer.into_inner()).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) {
let mut buffer = Buffer::new();
wrap_item(&mut buffer, |buffer| {
buffer.write_str("extern {\n");
wrap_item(w, |buffer| {
buffer.write_str("extern {\n").unwrap();
render_attributes_in_code(buffer, it, cx.tcx());
write!(
buffer,
" {}type {};\n}}",
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
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))
.unwrap();
}
Expand Down

0 comments on commit aabffef

Please sign in to comment.