Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 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 Buffer, 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