Skip to content

Commit 1270d00

Browse files
committed
Use ItemTemplate trait
1 parent 97aca51 commit 1270d00

File tree

2 files changed

+63
-43
lines changed

2 files changed

+63
-43
lines changed

src/librustdoc/html/render/print_item.rs

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

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

@@ -1827,6 +1880,12 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
18271880
ty: String,
18281881
}
18291882

1883+
impl<'a, 'cx: 'a> ItemTemplate<'a, 'cx> for ItemStruct<'a, 'cx> {
1884+
fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>) {
1885+
(self.it, self.cx.borrow_mut())
1886+
}
1887+
}
1888+
18301889
impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
18311890
fn new(
18321891
cx: std::cell::RefCell<&'a mut Context<'cx>>,
@@ -1838,15 +1897,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
18381897
Self { cx, it, s, should_render_fields }
18391898
}
18401899

1841-
fn render_attributes_in_code<'b>(
1842-
&'b self,
1843-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1844-
display_fn(move |f| {
1845-
let tcx = self.cx.borrow().tcx();
1846-
write!(f, "{}", render_attributes_in_code(self.it, tcx))
1847-
})
1848-
}
1849-
18501900
fn render_struct<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
18511901
display_fn(move |f| {
18521902
let cx = self.cx.borrow();
@@ -1863,14 +1913,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
18631913
})
18641914
}
18651915

1866-
fn document<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1867-
display_fn(move |f| {
1868-
let mut cx = self.cx.borrow_mut();
1869-
let v = document(*cx, self.it, None, HeadingOffset::H2);
1870-
write!(f, "{v}")
1871-
})
1872-
}
1873-
18741916
fn struct_field_items_iter<'b>(
18751917
&'b self,
18761918
) -> impl Iterator<Item = Field<'a>> + Captures<'a> + 'b + Captures<'cx> {
@@ -1894,28 +1936,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
18941936
write!(f, "{v}")
18951937
})
18961938
}
1897-
1898-
fn render_assoc_items<'b>(
1899-
&'b self,
1900-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1901-
display_fn(move |f| {
1902-
let mut cx = self.cx.borrow_mut();
1903-
let def_id = self.it.item_id.expect_def_id();
1904-
let v = render_assoc_items(*cx, self.it, def_id, AssocItemRender::All);
1905-
write!(f, "{v}")
1906-
})
1907-
}
1908-
1909-
fn document_type_layout<'b>(
1910-
&'b self,
1911-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1912-
display_fn(move |f| {
1913-
let cx = self.cx.borrow();
1914-
let def_id = self.it.item_id.expect_def_id();
1915-
let v = document_type_layout(*cx, def_id);
1916-
write!(f, "{v}")
1917-
})
1918-
}
19191939
}
19201940

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

src/librustdoc/html/templates/item_struct.html

Lines changed: 4 additions & 4 deletions
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)