Skip to content

Commit aabffef

Browse files
Rollup merge of #112243 - GuillaumeGomez:rm-unneeded-buffer-creations, 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`
2 parents 7452822 + 48c46f2 commit aabffef

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

src/librustdoc/html/render/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1040,9 +1040,9 @@ fn render_attributes_in_pre<'a, 'b: 'a>(
10401040

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

src/librustdoc/html/render/print_item.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -1431,30 +1431,28 @@ fn item_proc_macro(
14311431
it: &clean::Item,
14321432
m: &clean::ProcMacro,
14331433
) {
1434-
let mut buffer = Buffer::new();
1435-
wrap_item(&mut buffer, |buffer| {
1434+
wrap_item(w, |buffer| {
14361435
let name = it.name.expect("proc-macros always have names");
14371436
match m.kind {
14381437
MacroKind::Bang => {
1439-
write!(buffer, "{}!() {{ /* proc-macro */ }}", name);
1438+
write!(buffer, "{name}!() {{ /* proc-macro */ }}").unwrap();
14401439
}
14411440
MacroKind::Attr => {
1442-
write!(buffer, "#[{}]", name);
1441+
write!(buffer, "#[{name}]").unwrap();
14431442
}
14441443
MacroKind::Derive => {
1445-
write!(buffer, "#[derive({})]", name);
1444+
write!(buffer, "#[derive({name})]").unwrap();
14461445
if !m.helpers.is_empty() {
1447-
buffer.push_str("\n{\n");
1448-
buffer.push_str(" // Attributes available to this derive:\n");
1446+
buffer.write_str("\n{\n // Attributes available to this derive:\n").unwrap();
14491447
for attr in &m.helpers {
1450-
writeln!(buffer, " #[{}]", attr);
1448+
writeln!(buffer, " #[{attr}]").unwrap();
14511449
}
1452-
buffer.push_str("}\n");
1450+
buffer.write_str("}\n").unwrap();
14531451
}
14541452
}
14551453
}
14561454
});
1457-
write!(w, "{}{}", buffer.into_inner(), document(cx, it, None, HeadingOffset::H2)).unwrap();
1455+
write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap();
14581456
}
14591457

14601458
fn item_primitive(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item) {
@@ -1571,8 +1569,7 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
15711569
}
15721570

15731571
fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) {
1574-
let mut buffer = Buffer::new();
1575-
wrap_item(&mut buffer, |buffer| {
1572+
wrap_item(w, |buffer| {
15761573
render_attributes_in_code(buffer, it, cx.tcx());
15771574
write!(
15781575
buffer,
@@ -1581,29 +1578,27 @@ fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item,
15811578
mutability = s.mutability.print_with_space(),
15821579
name = it.name.unwrap(),
15831580
typ = s.type_.print(cx)
1584-
);
1581+
)
1582+
.unwrap();
15851583
});
15861584

1587-
write!(w, "{}", buffer.into_inner()).unwrap();
1588-
15891585
write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap();
15901586
}
15911587

15921588
fn item_foreign_type(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item) {
1593-
let mut buffer = Buffer::new();
1594-
wrap_item(&mut buffer, |buffer| {
1595-
buffer.write_str("extern {\n");
1589+
wrap_item(w, |buffer| {
1590+
buffer.write_str("extern {\n").unwrap();
15961591
render_attributes_in_code(buffer, it, cx.tcx());
15971592
write!(
15981593
buffer,
15991594
" {}type {};\n}}",
16001595
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
16011596
it.name.unwrap(),
1602-
);
1597+
)
1598+
.unwrap();
16031599
});
16041600

1605-
write!(w, "{}{}", buffer.into_inner(), document(cx, it, None, HeadingOffset::H2)).unwrap();
1606-
1601+
write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap();
16071602
write!(w, "{}", render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All))
16081603
.unwrap();
16091604
}

0 commit comments

Comments
 (0)