Skip to content

Commit 786311c

Browse files
committed
rustdoc: migrate item_typedef to an Askama template
1 parent 66ff9a4 commit 786311c

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

src/librustdoc/html/render/print_item.rs

+30-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_middle::middle::stability;
99
use rustc_middle::ty::{self, TyCtxt};
1010
use rustc_span::hygiene::MacroKind;
1111
use rustc_span::symbol::{kw, sym, Symbol};
12-
use rustc_target::abi::{LayoutS, Primitive, TagEncoding, Variants};
1312
use std::borrow::Borrow;
1413
use std::cell::{RefCell, RefMut};
1514
use std::cmp::Ordering;
@@ -1147,32 +1146,40 @@ fn item_template_render_attributes_in_pre<'a: 'b, 'cx: 'a, 'b>(
11471146
}
11481147

11491148
fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Typedef) {
1150-
fn write_content(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) {
1151-
wrap_item(w, |w| {
1152-
write!(
1153-
w,
1154-
"{attrs}{}type {}{}{where_clause} = {type_};",
1155-
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
1156-
it.name.unwrap(),
1157-
t.generics.print(cx),
1158-
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
1159-
type_ = t.type_.print(cx),
1160-
attrs = render_attributes_in_pre(it, "", cx.tcx()),
1161-
);
1162-
});
1149+
#[derive(Template)]
1150+
#[template(path = "item_typedef.html")]
1151+
struct ItemTypedef<'a, 'cx> {
1152+
cx: RefCell<&'a mut Context<'cx>>,
1153+
it: &'a clean::Item,
1154+
t: &'a clean::Typedef,
11631155
}
11641156

1165-
write_content(w, cx, it, t);
1157+
impl<'a, 'cx: 'a> ItemTemplate<'a, 'cx> for ItemTypedef<'a, 'cx> {
1158+
fn borrow_mut(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>) {
1159+
(&self.it, self.cx.borrow_mut())
1160+
}
1161+
}
11661162

1167-
write!(w, "{}", document(cx, it, None, HeadingOffset::H2));
1163+
impl<'a, 'cx: 'a> ItemTypedef<'a, 'cx> {
1164+
fn render_typedef<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1165+
display_fn(move |f| {
1166+
let cx = self.cx.borrow_mut();
1167+
let vis = self.it.visibility(cx.tcx());
1168+
write!(
1169+
f,
1170+
"{}type {}{}{where_clause} = {type_};",
1171+
visibility_print_with_space(vis, self.it.item_id, *cx),
1172+
self.it.name.unwrap(),
1173+
self.t.generics.print(*cx),
1174+
where_clause = print_where_clause(&self.t.generics, *cx, 0, Ending::Newline),
1175+
type_ = self.t.type_.print(*cx),
1176+
)?;
1177+
Ok(())
1178+
})
1179+
}
1180+
}
11681181

1169-
let def_id = it.item_id.expect_def_id();
1170-
// Render any items associated directly to this alias, as otherwise they
1171-
// won't be visible anywhere in the docs. It would be nice to also show
1172-
// associated items from the aliased type (see discussion in #32077), but
1173-
// we need #14072 to make sense of the generics.
1174-
write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All));
1175-
write!(w, "{}", document_type_layout(cx, def_id));
1182+
ItemTypedef { cx: std::cell::RefCell::new(cx), it, t }.render_into(w).unwrap();
11761183
}
11771184

11781185
fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<pre class="rust item-decl"><code>
2+
{{ self::item_template_render_attributes_in_pre(self.borrow()) | safe }}
3+
{{ self.render_typedef() | safe }}
4+
</code></pre>
5+
{{ self::item_template_document(self.borrow()) | safe }}
6+
{{ self::item_template_render_assoc_items(self.borrow()) | safe }}
7+
{{ self::item_template_document_type_layout(self.borrow()) | safe }}

0 commit comments

Comments
 (0)