Skip to content

Commit 1e51b81

Browse files
committed
Use display_fn for render_attributes_in_code
1 parent cb882fa commit 1e51b81

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ fn assoc_method(
854854
write!(w, "{}", render_attributes_in_pre(meth, indent_str, tcx));
855855
(4, indent_str, Ending::NoNewline)
856856
} else {
857-
render_attributes_in_code(w, meth, tcx);
857+
write!(w, "{}", render_attributes_in_code(meth, tcx));
858858
(0, "", Ending::Newline)
859859
};
860860
w.reserve(header_len + "<a href=\"\" class=\"fn\">{".len() + "</a>".len());
@@ -1040,10 +1040,16 @@ 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 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();
1046-
}
1043+
fn render_attributes_in_code<'a, 'tcx>(
1044+
it: &'a clean::Item,
1045+
tcx: TyCtxt<'tcx>,
1046+
) -> impl fmt::Display + Captures<'a> + Captures<'tcx> {
1047+
display_fn(move |f| {
1048+
for a in it.attributes(tcx, false) {
1049+
write!(f, "<div class=\"code-attribute\">{}</div>", a)?;
1050+
}
1051+
Ok(())
1052+
})
10471053
}
10481054

10491055
#[derive(Copy, Clone)]

src/librustdoc/html/render/print_item.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,13 +1334,13 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
13341334
let tcx = cx.tcx();
13351335
let count_variants = e.variants().count();
13361336
wrap_item(w, |mut w| {
1337-
render_attributes_in_code(w, it, tcx);
13381337
write!(
13391338
w,
1340-
"{}enum {}{}",
1339+
"{attrs}{}enum {}{}",
13411340
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
13421341
it.name.unwrap(),
13431342
e.generics.print(cx),
1343+
attrs = render_attributes_in_code(it, tcx),
13441344
);
13451345
if !print_where_clause_and_check(w, &e.generics, cx) {
13461346
// If there wasn't a `where` clause, we add a whitespace.
@@ -1539,11 +1539,11 @@ fn item_primitive(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Ite
15391539
fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) {
15401540
wrap_item(w, |w| {
15411541
let tcx = cx.tcx();
1542-
render_attributes_in_code(w, it, tcx);
15431542

15441543
write!(
15451544
w,
1546-
"{vis}const {name}: {typ}",
1545+
"{attrs}{vis}const {name}: {typ}",
1546+
attrs = render_attributes_in_code(it, tcx),
15471547
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
15481548
name = it.name.unwrap(),
15491549
typ = c.type_.print(cx),
@@ -1586,7 +1586,7 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
15861586

15871587
fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) {
15881588
wrap_item(w, |w| {
1589-
render_attributes_in_code(w, it, cx.tcx());
1589+
write!(w, "{}", render_attributes_in_code(it, cx.tcx()));
15901590
render_struct(w, it, Some(&s.generics), s.ctor_kind, &s.fields, "", true, cx);
15911591
});
15921592

@@ -1636,10 +1636,10 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
16361636

16371637
fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) {
16381638
wrap_item(w, |buffer| {
1639-
render_attributes_in_code(buffer, it, cx.tcx());
16401639
write!(
16411640
buffer,
1642-
"{vis}static {mutability}{name}: {typ}",
1641+
"{attrs}{vis}static {mutability}{name}: {typ}",
1642+
attrs = render_attributes_in_code(it, cx.tcx()),
16431643
vis = visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
16441644
mutability = s.mutability.print_with_space(),
16451645
name = it.name.unwrap(),
@@ -1654,12 +1654,12 @@ fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item,
16541654
fn item_foreign_type(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item) {
16551655
wrap_item(w, |buffer| {
16561656
buffer.write_str("extern {\n").unwrap();
1657-
render_attributes_in_code(buffer, it, cx.tcx());
16581657
write!(
16591658
buffer,
1660-
" {}type {};\n}}",
1659+
"{attrs} {}type {};\n}}",
16611660
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
16621661
it.name.unwrap(),
1662+
attrs = render_attributes_in_code(it, cx.tcx()),
16631663
)
16641664
.unwrap();
16651665
});

0 commit comments

Comments
 (0)