Skip to content

Commit 94faa5c

Browse files
committed
rustdoc: convert render_attributes_in_pre to return a Display
1 parent fc5de13 commit 94faa5c

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/librustdoc/html/render/mod.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use std::string::ToString;
5050
use askama::Template;
5151
use rustc_ast_pretty::pprust;
5252
use rustc_attr::{ConstStability, Deprecation, StabilityLevel};
53+
use rustc_data_structures::captures::Captures;
5354
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5455
use rustc_hir::def_id::{DefId, DefIdSet};
5556
use rustc_hir::Mutability;
@@ -842,7 +843,7 @@ fn assoc_method(
842843
let (indent, indent_str, end_newline) = if parent == ItemType::Trait {
843844
header_len += 4;
844845
let indent_str = " ";
845-
render_attributes_in_pre(w, meth, indent_str);
846+
write!(w, "{}", render_attributes_in_pre(meth, indent_str));
846847
(4, indent_str, Ending::NoNewline)
847848
} else {
848849
render_attributes_in_code(w, meth);
@@ -1038,10 +1039,16 @@ fn attributes(it: &clean::Item) -> Vec<String> {
10381039

10391040
// When an attribute is rendered inside a `<pre>` tag, it is formatted using
10401041
// a whitespace prefix and newline.
1041-
fn render_attributes_in_pre(w: &mut Buffer, it: &clean::Item, prefix: &str) {
1042-
for a in attributes(it) {
1043-
writeln!(w, "{}{}", prefix, a);
1044-
}
1042+
fn render_attributes_in_pre<'a>(
1043+
it: &'a clean::Item,
1044+
prefix: &'a str,
1045+
) -> impl fmt::Display + Captures<'a> {
1046+
crate::html::format::display_fn(move |f| {
1047+
for a in attributes(it) {
1048+
writeln!(f, "{}{}", prefix, a)?;
1049+
}
1050+
Ok(())
1051+
})
10451052
}
10461053

10471054
// When an attribute is rendered inside a <code> tag, it is formatted using

src/librustdoc/html/render/print_item.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,12 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
544544
f.decl.output.as_return().and_then(|output| notable_traits_button(output, cx));
545545

546546
wrap_item(w, |w| {
547-
render_attributes_in_pre(w, it, "");
548547
w.reserve(header_len);
549548
write!(
550549
w,
551-
"{vis}{constness}{asyncness}{unsafety}{abi}fn \
550+
"{attrs}{vis}{constness}{asyncness}{unsafety}{abi}fn \
552551
{name}{generics}{decl}{notable_traits}{where_clause}",
552+
attrs = render_attributes_in_pre(it, ""),
553553
vis = visibility,
554554
constness = constness,
555555
asyncness = asyncness,
@@ -581,16 +581,16 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
581581

582582
// Output the trait definition
583583
wrap_item(w, |w| {
584-
render_attributes_in_pre(w, it, "");
585584
write!(
586585
w,
587-
"{}{}{}trait {}{}{}",
586+
"{attrs}{}{}{}trait {}{}{}",
588587
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
589588
t.unsafety(tcx).print_with_space(),
590589
if t.is_auto(tcx) { "auto " } else { "" },
591590
it.name.unwrap(),
592591
t.generics.print(cx),
593-
bounds
592+
bounds,
593+
attrs = render_attributes_in_pre(it, ""),
594594
);
595595

596596
if !t.generics.where_predicates.is_empty() {
@@ -1057,14 +1057,14 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
10571057

10581058
fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::TraitAlias) {
10591059
wrap_item(w, |w| {
1060-
render_attributes_in_pre(w, it, "");
10611060
write!(
10621061
w,
1063-
"trait {}{}{} = {};",
1062+
"{attrs}trait {}{}{} = {};",
10641063
it.name.unwrap(),
10651064
t.generics.print(cx),
10661065
print_where_clause(&t.generics, cx, 0, Ending::Newline),
1067-
bounds(&t.bounds, true, cx)
1066+
bounds(&t.bounds, true, cx),
1067+
attrs = render_attributes_in_pre(it, ""),
10681068
);
10691069
});
10701070

@@ -1079,14 +1079,14 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &
10791079

10801080
fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) {
10811081
wrap_item(w, |w| {
1082-
render_attributes_in_pre(w, it, "");
10831082
write!(
10841083
w,
1085-
"type {}{}{where_clause} = impl {bounds};",
1084+
"{attrs}type {}{}{where_clause} = impl {bounds};",
10861085
it.name.unwrap(),
10871086
t.generics.print(cx),
10881087
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
10891088
bounds = bounds(&t.bounds, false, cx),
1089+
attrs = render_attributes_in_pre(it, ""),
10901090
);
10911091
});
10921092

@@ -1102,15 +1102,15 @@ fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &cl
11021102
fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Typedef) {
11031103
fn write_content(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) {
11041104
wrap_item(w, |w| {
1105-
render_attributes_in_pre(w, it, "");
11061105
write!(
11071106
w,
1108-
"{}type {}{}{where_clause} = {type_};",
1107+
"{attrs}{}type {}{}{where_clause} = {type_};",
11091108
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
11101109
it.name.unwrap(),
11111110
t.generics.print(cx),
11121111
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
11131112
type_ = t.type_.print(cx),
1113+
attrs = render_attributes_in_pre(it, ""),
11141114
);
11151115
});
11161116
}
@@ -1130,7 +1130,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
11301130

11311131
fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
11321132
wrap_item(w, |w| {
1133-
render_attributes_in_pre(w, it, "");
1133+
write!(w, "{}", render_attributes_in_pre(it, ""));
11341134
render_union(w, it, Some(&s.generics), &s.fields, cx);
11351135
});
11361136

@@ -1197,13 +1197,13 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
11971197
let tcx = cx.tcx();
11981198
let count_variants = e.variants().count();
11991199
wrap_item(w, |w| {
1200-
render_attributes_in_pre(w, it, "");
12011200
write!(
12021201
w,
1203-
"{}enum {}{}",
1202+
"{attrs}{}enum {}{}",
12041203
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
12051204
it.name.unwrap(),
12061205
e.generics.print(cx),
1206+
attrs = render_attributes_in_pre(it, ""),
12071207
);
12081208
if !print_where_clause_and_check(w, &e.generics, cx) {
12091209
// If there wasn't a `where` clause, we add a whitespace.

0 commit comments

Comments
 (0)