Skip to content

Commit cb28bbd

Browse files
committed
Convert should_render_fields into a field
1 parent df40243 commit cb28bbd

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
@@ -1595,9 +1595,20 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
15951595
cx: std::cell::RefCell<&'a mut Context<'cx>>,
15961596
it: &'a clean::Item,
15971597
s: &'a clean::Struct,
1598+
should_render_fields: bool,
15981599
}
15991600

16001601
impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
1602+
fn new(
1603+
cx: std::cell::RefCell<&'a mut Context<'cx>>,
1604+
it: &'a clean::Item,
1605+
s: &'a clean::Struct,
1606+
) -> Self {
1607+
let should_render_fields = matches!(s.ctor_kind, None | Some(CtorKind::Fn))
1608+
&& struct_field_items(s).peekable().peek().is_some();
1609+
Self { cx, it, s, should_render_fields }
1610+
}
1611+
16011612
fn render_struct<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
16021613
display_fn(move |f| {
16031614
let cx = self.cx.borrow();
@@ -1622,18 +1633,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
16221633
})
16231634
}
16241635

1625-
fn fields(&self) -> impl Iterator<Item = (&clean::Item, &clean::Type)> {
1626-
self.s.fields.iter().filter_map(|item| match *item.kind {
1627-
clean::StructFieldItem(ref ty) => Some((item, ty)),
1628-
_ => None,
1629-
})
1630-
}
1631-
1632-
fn should_render_fields(&self) -> bool {
1633-
matches!(self.s.ctor_kind, None | Some(CtorKind::Fn))
1634-
&& self.fields().peekable().peek().is_some()
1635-
}
1636-
16371636
fn render_field_in_span<'b>(
16381637
&'b self,
16391638
index: &'b usize,
@@ -1691,7 +1690,14 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
16911690
}
16921691
}
16931692

1694-
ItemStruct { cx: std::cell::RefCell::new(cx), it, s }.render_into(w).unwrap();
1693+
ItemStruct::new(std::cell::RefCell::new(cx), it, s).render_into(w).unwrap();
1694+
}
1695+
1696+
fn struct_field_items(s: &clean::Struct) -> impl Iterator<Item = (&clean::Item, &clean::Type)> {
1697+
s.fields.iter().filter_map(|item| match *item.kind {
1698+
clean::StructFieldItem(ref ty) => Some((item, ty)),
1699+
_ => None,
1700+
})
16951701
}
16961702

16971703
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)