Skip to content

Commit eb2b7dc

Browse files
committed
Use display_fn for render_attributes_in_code
1 parent 6888929 commit eb2b7dc

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ fn assoc_method(
871871
write!(w, "{}", render_attributes_in_pre(meth, indent_str, cx));
872872
(4, indent_str, Ending::NoNewline)
873873
} else {
874-
render_attributes_in_code(w, meth, cx);
874+
write!(w, "{}", render_attributes_in_code(meth, tcx));
875875
(0, "", Ending::Newline)
876876
};
877877
w.reserve(header_len + "<a href=\"\" class=\"fn\">{".len() + "</a>".len());
@@ -1075,10 +1075,16 @@ fn render_attributes_in_pre<'a, 'tcx: 'a>(
10751075

10761076
// When an attribute is rendered inside a <code> tag, it is formatted using
10771077
// a div to produce a newline after it.
1078-
fn render_attributes_in_code(w: &mut impl fmt::Write, it: &clean::Item, cx: &Context<'_>) {
1079-
for attr in it.attributes(cx.tcx(), cx.cache(), false) {
1080-
write!(w, "<div class=\"code-attribute\">{attr}</div>").unwrap();
1081-
}
1078+
fn render_attributes_in_code<'a, 'tcx>(
1079+
it: &'a clean::Item,
1080+
tcx: TyCtxt<'tcx>,
1081+
) -> impl fmt::Display + Captures<'a> + Captures<'tcx> {
1082+
display_fn(move |f| {
1083+
for a in it.attributes(tcx, false) {
1084+
write!(f, "<div class=\"code-attribute\">{}</div>", a)?;
1085+
}
1086+
Ok(())
1087+
})
10821088
}
10831089

10841090
#[derive(Copy, Clone)]

src/librustdoc/html/render/print_item.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,14 +1413,14 @@ fn print_tuple_struct_fields<'a, 'cx: 'a>(
14131413
fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::Enum) {
14141414
let tcx = cx.tcx();
14151415
let count_variants = e.variants().count();
1416-
wrap_item(w, |w| {
1417-
render_attributes_in_code(w, it, cx);
1416+
wrap_item(w, |mut w| {
14181417
write!(
14191418
w,
1420-
"{}enum {}{}",
1419+
"{attrs}{}enum {}{}",
14211420
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
14221421
it.name.unwrap(),
14231422
e.generics.print(cx),
1423+
attrs = render_attributes_in_code(it, tcx),
14241424
);
14251425

14261426
render_enum_fields(
@@ -1733,11 +1733,11 @@ fn item_primitive(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Ite
17331733
fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) {
17341734
wrap_item(w, |w| {
17351735
let tcx = cx.tcx();
1736-
render_attributes_in_code(w, it, cx);
17371736

17381737
write!(
17391738
w,
1740-
"{vis}const {name}{generics}: {typ}{where_clause}",
1739+
"{attrs}{vis}const {name}: {typ}",
1740+
attrs = render_attributes_in_code(it, tcx),
17411741
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
17421742
name = it.name.unwrap(),
17431743
generics = c.generics.print(cx),
@@ -1782,7 +1782,7 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
17821782

17831783
fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) {
17841784
wrap_item(w, |w| {
1785-
render_attributes_in_code(w, it, cx);
1785+
write!(w, "{}", render_attributes_in_code(it, cx.tcx()));
17861786
render_struct(w, it, Some(&s.generics), s.ctor_kind, &s.fields, "", true, cx);
17871787
});
17881788

@@ -1842,10 +1842,10 @@ fn item_fields(
18421842

18431843
fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) {
18441844
wrap_item(w, |buffer| {
1845-
render_attributes_in_code(buffer, it, cx);
18461845
write!(
18471846
buffer,
1848-
"{vis}static {mutability}{name}: {typ}",
1847+
"{attrs}{vis}static {mutability}{name}: {typ}",
1848+
attrs = render_attributes_in_code(it, cx.tcx()),
18491849
vis = visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
18501850
mutability = s.mutability.print_with_space(),
18511851
name = it.name.unwrap(),
@@ -1860,12 +1860,12 @@ fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item,
18601860
fn item_foreign_type(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item) {
18611861
wrap_item(w, |buffer| {
18621862
buffer.write_str("extern {\n").unwrap();
1863-
render_attributes_in_code(buffer, it, cx);
18641863
write!(
18651864
buffer,
1866-
" {}type {};\n}}",
1865+
"{attrs} {}type {};\n}}",
18671866
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
18681867
it.name.unwrap(),
1868+
attrs = render_attributes_in_code(it, cx.tcx()),
18691869
)
18701870
.unwrap();
18711871
});

0 commit comments

Comments
 (0)