Skip to content

Commit af05b25

Browse files
committed
Use ItemTemplate trait
1 parent 27ebd02 commit af05b25

File tree

2 files changed

+63
-43
lines changed

2 files changed

+63
-43
lines changed

src/librustdoc/html/render/print_item.rs

+59-39
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,59 @@ trait ItemTemplate<'a, 'cx: 'a>: askama::Template + fmt::Display {
325325
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>);
326326
}
327327

328+
fn item_template_document<'a: 'b, 'b, 'cx: 'a>(
329+
templ: &'b impl ItemTemplate<'a, 'cx>,
330+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
331+
display_fn(move |f| {
332+
let (item, mut cx) = templ.item_and_mut_cx();
333+
let v = document(*cx, item, None, HeadingOffset::H2);
334+
write!(f, "{v}")
335+
})
336+
}
337+
338+
fn item_template_document_type_layout<'a: 'b, 'b, 'cx: 'a>(
339+
templ: &'b impl ItemTemplate<'a, 'cx>,
340+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
341+
display_fn(move |f| {
342+
let (item, cx) = templ.item_and_mut_cx();
343+
let def_id = item.item_id.expect_def_id();
344+
let v = document_type_layout(*cx, def_id);
345+
write!(f, "{v}")
346+
})
347+
}
348+
349+
fn item_template_render_attributes_in_pre<'a: 'b, 'b, 'cx: 'a>(
350+
templ: &'b impl ItemTemplate<'a, 'cx>,
351+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
352+
display_fn(move |f| {
353+
let (item, cx) = templ.item_and_mut_cx();
354+
let tcx = cx.tcx();
355+
let v = render_attributes_in_pre(item, "", tcx);
356+
write!(f, "{v}")
357+
})
358+
}
359+
360+
fn item_template_render_attributes_in_code<'a: 'b, 'b, 'cx: 'a>(
361+
templ: &'b impl ItemTemplate<'a, 'cx>,
362+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
363+
display_fn(move |f| {
364+
let (it, cx) = templ.item_and_mut_cx();
365+
let v = render_attributes_in_code(it, cx.tcx());
366+
write!(f, "{v}")
367+
})
368+
}
369+
370+
fn item_template_render_assoc_items<'a: 'b, 'b, 'cx: 'a>(
371+
templ: &'b impl ItemTemplate<'a, 'cx>,
372+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
373+
display_fn(move |f| {
374+
let (item, mut cx) = templ.item_and_mut_cx();
375+
let def_id = item.item_id.expect_def_id();
376+
let v = render_assoc_items(*cx, item, def_id, AssocItemRender::All, None);
377+
write!(f, "{v}")
378+
})
379+
}
380+
328381
fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items: &[clean::Item]) {
329382
write!(w, "{}", document(cx, item, None, HeadingOffset::H2));
330383

@@ -1605,6 +1658,12 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
16051658
ty: String,
16061659
}
16071660

1661+
impl<'a, 'cx: 'a> ItemTemplate<'a, 'cx> for ItemStruct<'a, 'cx> {
1662+
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>) {
1663+
(self.it, self.cx.borrow_mut())
1664+
}
1665+
}
1666+
16081667
impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
16091668
fn new(
16101669
cx: std::cell::RefCell<&'a mut Context<'cx>>,
@@ -1616,15 +1675,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
16161675
Self { cx, it, s, should_render_fields }
16171676
}
16181677

1619-
fn render_attributes_in_code<'b>(
1620-
&'b self,
1621-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1622-
display_fn(move |f| {
1623-
let tcx = self.cx.borrow().tcx();
1624-
write!(f, "{}", render_attributes_in_code(self.it, tcx))
1625-
})
1626-
}
1627-
16281678
fn render_struct<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
16291679
display_fn(move |f| {
16301680
let cx = self.cx.borrow();
@@ -1641,14 +1691,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
16411691
})
16421692
}
16431693

1644-
fn document<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1645-
display_fn(move |f| {
1646-
let mut cx = self.cx.borrow_mut();
1647-
let v = document(*cx, self.it, None, HeadingOffset::H2);
1648-
write!(f, "{v}")
1649-
})
1650-
}
1651-
16521694
fn struct_field_items_iter<'b>(
16531695
&'b self,
16541696
) -> impl Iterator<Item = Field<'a>> + Captures<'a> + 'b + Captures<'cx> {
@@ -1672,28 +1714,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
16721714
write!(f, "{v}")
16731715
})
16741716
}
1675-
1676-
fn render_assoc_items<'b>(
1677-
&'b self,
1678-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1679-
display_fn(move |f| {
1680-
let mut cx = self.cx.borrow_mut();
1681-
let def_id = self.it.item_id.expect_def_id();
1682-
let v = render_assoc_items(*cx, self.it, def_id, AssocItemRender::All);
1683-
write!(f, "{v}")
1684-
})
1685-
}
1686-
1687-
fn document_type_layout<'b>(
1688-
&'b self,
1689-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1690-
display_fn(move |f| {
1691-
let cx = self.cx.borrow();
1692-
let def_id = self.it.item_id.expect_def_id();
1693-
let v = document_type_layout(*cx, def_id);
1694-
write!(f, "{v}")
1695-
})
1696-
}
16971717
}
16981718

16991719
ItemStruct::new(std::cell::RefCell::new(cx), it, s).render_into(w).unwrap();

src/librustdoc/html/templates/item_struct.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_code() | safe }}
2+
{{ self::item_template_render_attributes_in_code(self.borrow()) | safe }}
33
{{ self.render_struct() | safe }}
44
</code></pre>
5-
{{ self.document() | safe }}
5+
{{ self::item_template_document(self.borrow()) | safe }}
66
{% if self.should_render_fields %}
77
<h2 id="fields" class="fields small-section header">
88
{% if self.s.ctor_kind.is_none() %}
@@ -22,5 +22,5 @@ <h2 id="fields" class="fields small-section header">
2222
{{ self.document_field(field.item) | safe }}
2323
{% endfor %}
2424
{% endif %}
25-
{{ self.render_assoc_items() | safe }}
26-
{{ self.document_type_layout() | safe }}
25+
{{ self::item_template_render_assoc_items(self.borrow()) | safe }}
26+
{{ self::item_template_document_type_layout(self.borrow()) | safe }}

0 commit comments

Comments
 (0)