Skip to content

Commit c325fda

Browse files
committed
rustdoc: migrate item_union to an Askama template
1 parent 19f9f65 commit c325fda

File tree

4 files changed

+384
-293
lines changed

4 files changed

+384
-293
lines changed

src/librustdoc/html/format.rs

-4
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ impl Buffer {
136136
self.into_inner()
137137
}
138138

139-
pub(crate) fn is_for_html(&self) -> bool {
140-
self.for_html
141-
}
142-
143139
pub(crate) fn reserve(&mut self, additional: usize) {
144140
self.buffer.reserve(additional)
145141
}

src/librustdoc/html/render/mod.rs

+42-35
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ fn short_item_info(
660660
// "Auto Trait Implementations," "Blanket Trait Implementations" (on struct/enum pages).
661661
pub(crate) fn render_impls(
662662
cx: &mut Context<'_>,
663-
w: &mut Buffer,
663+
mut w: impl Write,
664664
impls: &[&Impl],
665665
containing_item: &clean::Item,
666666
toggle_open_by_default: bool,
@@ -672,7 +672,7 @@ pub(crate) fn render_impls(
672672
let did = i.trait_did().unwrap();
673673
let provided_trait_methods = i.inner_impl().provided_trait_methods(tcx);
674674
let assoc_link = AssocItemLink::GotoSource(did.into(), &provided_trait_methods);
675-
let mut buffer = if w.is_for_html() { Buffer::html() } else { Buffer::new() };
675+
let mut buffer = Buffer::new();
676676
render_impl(
677677
&mut buffer,
678678
cx,
@@ -693,7 +693,7 @@ pub(crate) fn render_impls(
693693
})
694694
.collect::<Vec<_>>();
695695
rendered_impls.sort();
696-
w.write_str(&rendered_impls.join(""));
696+
w.write_str(&rendered_impls.join("")).unwrap();
697697
}
698698

699699
/// Build a (possibly empty) `href` attribute (a key-value pair) for the given associated item.
@@ -1080,61 +1080,68 @@ impl<'a> AssocItemLink<'a> {
10801080
}
10811081
}
10821082

1083-
fn write_impl_section_heading(w: &mut Buffer, title: &str, id: &str) {
1083+
fn write_impl_section_heading(mut w: impl fmt::Write, title: &str, id: &str) {
10841084
write!(
10851085
w,
10861086
"<h2 id=\"{id}\" class=\"small-section-header\">\
10871087
{title}\
10881088
<a href=\"#{id}\" class=\"anchor\">§</a>\
10891089
</h2>"
1090-
);
1090+
)
1091+
.unwrap();
10911092
}
10921093

10931094
pub(crate) fn render_all_impls(
1094-
w: &mut Buffer,
1095+
mut w: impl Write,
10951096
cx: &mut Context<'_>,
10961097
containing_item: &clean::Item,
10971098
concrete: &[&Impl],
10981099
synthetic: &[&Impl],
10991100
blanket_impl: &[&Impl],
11001101
) {
1101-
let mut impls = Buffer::empty_from(w);
1102+
let mut impls = Buffer::html();
11021103
render_impls(cx, &mut impls, concrete, containing_item, true);
11031104
let impls = impls.into_inner();
11041105
if !impls.is_empty() {
1105-
write_impl_section_heading(w, "Trait Implementations", "trait-implementations");
1106-
write!(w, "<div id=\"trait-implementations-list\">{}</div>", impls);
1106+
write_impl_section_heading(&mut w, "Trait Implementations", "trait-implementations");
1107+
write!(w, "<div id=\"trait-implementations-list\">{}</div>", impls).unwrap();
11071108
}
11081109

11091110
if !synthetic.is_empty() {
1110-
write_impl_section_heading(w, "Auto Trait Implementations", "synthetic-implementations");
1111-
w.write_str("<div id=\"synthetic-implementations-list\">");
1112-
render_impls(cx, w, synthetic, containing_item, false);
1113-
w.write_str("</div>");
1111+
write_impl_section_heading(
1112+
&mut w,
1113+
"Auto Trait Implementations",
1114+
"synthetic-implementations",
1115+
);
1116+
w.write_str("<div id=\"synthetic-implementations-list\">").unwrap();
1117+
render_impls(cx, &mut w, synthetic, containing_item, false);
1118+
w.write_str("</div>").unwrap();
11141119
}
11151120

11161121
if !blanket_impl.is_empty() {
1117-
write_impl_section_heading(w, "Blanket Implementations", "blanket-implementations");
1118-
w.write_str("<div id=\"blanket-implementations-list\">");
1119-
render_impls(cx, w, blanket_impl, containing_item, false);
1120-
w.write_str("</div>");
1122+
write_impl_section_heading(&mut w, "Blanket Implementations", "blanket-implementations");
1123+
w.write_str("<div id=\"blanket-implementations-list\">").unwrap();
1124+
render_impls(cx, &mut w, blanket_impl, containing_item, false);
1125+
w.write_str("</div>").unwrap();
11211126
}
11221127
}
11231128

1124-
fn render_assoc_items(
1125-
w: &mut Buffer,
1126-
cx: &mut Context<'_>,
1127-
containing_item: &clean::Item,
1129+
fn render_assoc_items<'a, 'cx: 'a>(
1130+
cx: &'a mut Context<'cx>,
1131+
containing_item: &'a clean::Item,
11281132
it: DefId,
1129-
what: AssocItemRender<'_>,
1130-
) {
1133+
what: AssocItemRender<'a>,
1134+
) -> impl fmt::Display + 'a + Captures<'cx> {
11311135
let mut derefs = DefIdSet::default();
11321136
derefs.insert(it);
1133-
render_assoc_items_inner(w, cx, containing_item, it, what, &mut derefs)
1137+
display_fn(move |f| {
1138+
render_assoc_items_inner(f, cx, containing_item, it, what, &mut derefs);
1139+
Ok(())
1140+
})
11341141
}
11351142

11361143
fn render_assoc_items_inner(
1137-
w: &mut Buffer,
1144+
mut w: &mut dyn fmt::Write,
11381145
cx: &mut Context<'_>,
11391146
containing_item: &clean::Item,
11401147
it: DefId,
@@ -1147,7 +1154,7 @@ fn render_assoc_items_inner(
11471154
let Some(v) = cache.impls.get(&it) else { return };
11481155
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
11491156
if !non_trait.is_empty() {
1150-
let mut tmp_buf = Buffer::empty_from(w);
1157+
let mut tmp_buf = Buffer::html();
11511158
let (render_mode, id) = match what {
11521159
AssocItemRender::All => {
11531160
write_impl_section_heading(&mut tmp_buf, "Implementations", "implementations");
@@ -1171,7 +1178,7 @@ fn render_assoc_items_inner(
11711178
(RenderMode::ForDeref { mut_: deref_mut_ }, cx.derive_id(id))
11721179
}
11731180
};
1174-
let mut impls_buf = Buffer::empty_from(w);
1181+
let mut impls_buf = Buffer::html();
11751182
for i in &non_trait {
11761183
render_impl(
11771184
&mut impls_buf,
@@ -1191,10 +1198,10 @@ fn render_assoc_items_inner(
11911198
);
11921199
}
11931200
if !impls_buf.is_empty() {
1194-
w.push_buffer(tmp_buf);
1195-
write!(w, "<div id=\"{}\">", id);
1196-
w.push_buffer(impls_buf);
1197-
w.write_str("</div>");
1201+
write!(w, "{}", tmp_buf.into_inner()).unwrap();
1202+
write!(w, "<div id=\"{}\">", id).unwrap();
1203+
write!(w, "{}", impls_buf.into_inner()).unwrap();
1204+
w.write_str("</div>").unwrap();
11981205
}
11991206
}
12001207

@@ -1204,7 +1211,7 @@ fn render_assoc_items_inner(
12041211
if let Some(impl_) = deref_impl {
12051212
let has_deref_mut =
12061213
traits.iter().any(|t| t.trait_did() == cx.tcx().lang_items().deref_mut_trait());
1207-
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut, derefs);
1214+
render_deref_methods(&mut w, cx, impl_, containing_item, has_deref_mut, derefs);
12081215
}
12091216

12101217
// If we were already one level into rendering deref methods, we don't want to render
@@ -1223,7 +1230,7 @@ fn render_assoc_items_inner(
12231230
}
12241231

12251232
fn render_deref_methods(
1226-
w: &mut Buffer,
1233+
mut w: impl Write,
12271234
cx: &mut Context<'_>,
12281235
impl_: &Impl,
12291236
container_item: &clean::Item,
@@ -1255,10 +1262,10 @@ fn render_deref_methods(
12551262
return;
12561263
}
12571264
}
1258-
render_assoc_items_inner(w, cx, container_item, did, what, derefs);
1265+
render_assoc_items_inner(&mut w, cx, container_item, did, what, derefs);
12591266
} else if let Some(prim) = target.primitive_type() {
12601267
if let Some(&did) = cache.primitive_locations.get(&prim) {
1261-
render_assoc_items_inner(w, cx, container_item, did, what, derefs);
1268+
render_assoc_items_inner(&mut w, cx, container_item, did, what, derefs);
12621269
}
12631270
}
12641271
}

0 commit comments

Comments
 (0)