Skip to content

Commit 6a892f8

Browse files
committed
Convert should_render_fields into a field
1 parent 6319202 commit 6a892f8

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,9 +1817,20 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
18171817
cx: std::cell::RefCell<&'a mut Context<'cx>>,
18181818
it: &'a clean::Item,
18191819
s: &'a clean::Struct,
1820+
should_render_fields: bool,
18201821
}
18211822

18221823
impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
1824+
fn new(
1825+
cx: std::cell::RefCell<&'a mut Context<'cx>>,
1826+
it: &'a clean::Item,
1827+
s: &'a clean::Struct,
1828+
) -> Self {
1829+
let should_render_fields = matches!(s.ctor_kind, None | Some(CtorKind::Fn))
1830+
&& struct_field_items(s).peekable().peek().is_some();
1831+
Self { cx, it, s, should_render_fields }
1832+
}
1833+
18231834
fn render_struct<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
18241835
display_fn(move |f| {
18251836
let cx = self.cx.borrow();
@@ -1844,18 +1855,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
18441855
})
18451856
}
18461857

1847-
fn fields(&self) -> impl Iterator<Item = (&clean::Item, &clean::Type)> {
1848-
self.s.fields.iter().filter_map(|item| match *item.kind {
1849-
clean::StructFieldItem(ref ty) => Some((item, ty)),
1850-
_ => None,
1851-
})
1852-
}
1853-
1854-
fn should_render_fields(&self) -> bool {
1855-
matches!(self.s.ctor_kind, None | Some(CtorKind::Fn))
1856-
&& self.fields().peekable().peek().is_some()
1857-
}
1858-
18591858
fn render_field_in_span<'b>(
18601859
&'b self,
18611860
index: &'b usize,
@@ -1913,7 +1912,14 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
19131912
}
19141913
}
19151914

1916-
ItemStruct { cx: std::cell::RefCell::new(cx), it, s }.render_into(w).unwrap();
1915+
ItemStruct::new(std::cell::RefCell::new(cx), it, s).render_into(w).unwrap();
1916+
}
1917+
1918+
fn struct_field_items(s: &clean::Struct) -> impl Iterator<Item = (&clean::Item, &clean::Type)> {
1919+
s.fields.iter().filter_map(|item| match *item.kind {
1920+
clean::StructFieldItem(ref ty) => Some((item, ty)),
1921+
_ => None,
1922+
})
19171923
}
19181924

19191925
fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) {

src/librustdoc/html/templates/item_struct.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{ self.render_struct() | safe }}
44
</code></pre>
55
{{ self.document() | safe }}
6-
{% if self.should_render_fields() %}
6+
{% if self.should_render_fields %}
77
<h2 id="fields" class="fields small-section header">
88
{% if self.s.ctor_kind.is_none() %}
99
Fields
@@ -14,7 +14,7 @@ <h2 id="fields" class="fields small-section header">
1414
<a href="#fields" class="anchor">§</a>
1515
</h2>
1616
{{ self::document_non_exhaustive(self.it) | safe }}
17-
{% for (index, (field, ty)) in self.fields().enumerate() %}
17+
{% for (index, (field, ty)) in self::struct_field_items(self.s).enumerate() %}
1818
{{ self.render_field_in_span(index, field, ty) | safe }}
1919
{{ self.document_field(field) | safe }}
2020
{% endfor %}

0 commit comments

Comments
 (0)