Skip to content

Commit 9a4fce9

Browse files
authored
Rollup merge of #111946 - nicklimmm:askama-item-template-trait, r=GuillaumeGomez
rustdoc: Add `ItemTemplate` trait and related functions to avoid repetitively wrapping existing functions Context: #111430 (comment) This trait will be used extensively in performing migrations to Askama templates (tracking issue: #108868)
2 parents 5a4c04c + fbdc511 commit 9a4fce9

File tree

2 files changed

+61
-42
lines changed

2 files changed

+61
-42
lines changed

src/librustdoc/html/render/print_item.rs

+57-38
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ 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 std::borrow::Borrow;
13+
use std::cell::{RefCell, RefMut};
1214
use std::cmp::Ordering;
1315
use std::fmt;
1416
use std::rc::Rc;
@@ -216,6 +218,53 @@ fn toggle_close(mut w: impl fmt::Write) {
216218
w.write_str("</details>").unwrap();
217219
}
218220

221+
trait ItemTemplate<'a, 'cx: 'a>: askama::Template + fmt::Display {
222+
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>);
223+
}
224+
225+
fn item_template_document<'a: 'b, 'b, 'cx: 'a>(
226+
templ: &'b impl ItemTemplate<'a, 'cx>,
227+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
228+
display_fn(move |f| {
229+
let (item, mut cx) = templ.item_and_mut_cx();
230+
let v = document(*cx, item, None, HeadingOffset::H2);
231+
write!(f, "{v}")
232+
})
233+
}
234+
235+
fn item_template_document_type_layout<'a: 'b, 'b, 'cx: 'a>(
236+
templ: &'b impl ItemTemplate<'a, 'cx>,
237+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
238+
display_fn(move |f| {
239+
let (item, cx) = templ.item_and_mut_cx();
240+
let def_id = item.item_id.expect_def_id();
241+
let v = document_type_layout(*cx, def_id);
242+
write!(f, "{v}")
243+
})
244+
}
245+
246+
fn item_template_render_attributes_in_pre<'a: 'b, 'b, 'cx: 'a>(
247+
templ: &'b impl ItemTemplate<'a, 'cx>,
248+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
249+
display_fn(move |f| {
250+
let (item, cx) = templ.item_and_mut_cx();
251+
let tcx = cx.tcx();
252+
let v = render_attributes_in_pre(item, "", tcx);
253+
write!(f, "{v}")
254+
})
255+
}
256+
257+
fn item_template_render_assoc_items<'a: 'b, 'b, 'cx: 'a>(
258+
templ: &'b impl ItemTemplate<'a, 'cx>,
259+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
260+
display_fn(move |f| {
261+
let (item, mut cx) = templ.item_and_mut_cx();
262+
let def_id = item.item_id.expect_def_id();
263+
let v = render_assoc_items(*cx, item, def_id, AssocItemRender::All);
264+
write!(f, "{v}")
265+
})
266+
}
267+
219268
fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items: &[clean::Item]) {
220269
write!(w, "{}", document(cx, item, None, HeadingOffset::H2));
221270

@@ -1131,55 +1180,25 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
11311180
#[derive(Template)]
11321181
#[template(path = "item_union.html")]
11331182
struct ItemUnion<'a, 'cx> {
1134-
cx: std::cell::RefCell<&'a mut Context<'cx>>,
1183+
cx: RefCell<&'a mut Context<'cx>>,
11351184
it: &'a clean::Item,
11361185
s: &'a clean::Union,
11371186
}
11381187

1139-
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
1140-
fn render_assoc_items<'b>(
1141-
&'b self,
1142-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1143-
display_fn(move |f| {
1144-
let def_id = self.it.item_id.expect_def_id();
1145-
let mut cx = self.cx.borrow_mut();
1146-
let v = render_assoc_items(*cx, self.it, def_id, AssocItemRender::All);
1147-
write!(f, "{v}")
1148-
})
1149-
}
1150-
fn document_type_layout<'b>(
1151-
&'b self,
1152-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1153-
display_fn(move |f| {
1154-
let def_id = self.it.item_id.expect_def_id();
1155-
let cx = self.cx.borrow_mut();
1156-
let v = document_type_layout(*cx, def_id);
1157-
write!(f, "{v}")
1158-
})
1188+
impl<'a, 'cx: 'a> ItemTemplate<'a, 'cx> for ItemUnion<'a, 'cx> {
1189+
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>) {
1190+
(self.it, self.cx.borrow_mut())
11591191
}
1192+
}
1193+
1194+
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
11601195
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
11611196
display_fn(move |f| {
11621197
let cx = self.cx.borrow_mut();
11631198
let v = render_union(self.it, Some(&self.s.generics), &self.s.fields, *cx);
11641199
write!(f, "{v}")
11651200
})
11661201
}
1167-
fn render_attributes_in_pre<'b>(
1168-
&'b self,
1169-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1170-
display_fn(move |f| {
1171-
let tcx = self.cx.borrow().tcx();
1172-
let v = render_attributes_in_pre(self.it, "", tcx);
1173-
write!(f, "{v}")
1174-
})
1175-
}
1176-
fn document<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1177-
display_fn(move |f| {
1178-
let mut cx = self.cx.borrow_mut();
1179-
let v = document(*cx, self.it, None, HeadingOffset::H2);
1180-
write!(f, "{v}")
1181-
})
1182-
}
11831202
fn document_field<'b>(
11841203
&'b self,
11851204
field: &'a clean::Item,
@@ -1219,7 +1238,7 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
12191238
}
12201239
}
12211240

1222-
ItemUnion { cx: std::cell::RefCell::new(cx), it, s }.render_into(w).unwrap();
1241+
ItemUnion { cx: RefCell::new(cx), it, s }.render_into(w).unwrap();
12231242
}
12241243

12251244
fn print_tuple_struct_fields<'a, 'cx: 'a>(

src/librustdoc/html/templates/item_union.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<pre class="rust item-decl"><code>
2-
{{ self.render_attributes_in_pre() | safe }}
2+
{{ self::item_template_render_attributes_in_pre(self.borrow()) | safe }}
33
{{ self.render_union() | safe }}
44
</code></pre>
5-
{{ self.document() | safe }}
5+
{{ self::item_template_document(self.borrow()) | safe }}
66
{% if self.fields_iter().peek().is_some() %}
77
<h2 id="fields" class="fields small-section-header">
88
Fields<a href="#fields" class="anchor">§</a>
@@ -19,5 +19,5 @@ <h2 id="fields" class="fields small-section-header">
1919
{{ self.document_field(field) | safe }}
2020
{% endfor %}
2121
{% endif %}
22-
{{ self.render_assoc_items() | safe }}
23-
{{ self.document_type_layout() | safe }}
22+
{{ self::item_template_render_assoc_items(self.borrow()) | safe }}
23+
{{ self::item_template_document_type_layout(self.borrow()) | safe }}

0 commit comments

Comments
 (0)