Skip to content

Commit 04626a0

Browse files
committed
Move item template structs outside of function
1 parent 784d352 commit 04626a0

File tree

1 file changed

+120
-118
lines changed

1 file changed

+120
-118
lines changed

src/librustdoc/html/render/print_item.rs

+120-118
Original file line numberDiff line numberDiff line change
@@ -1257,67 +1257,68 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
12571257
write!(w, "{}", document_type_layout(cx, def_id));
12581258
}
12591259

1260-
fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
1261-
item_template!(
1262-
#[template(path = "item_union.html")]
1263-
struct ItemUnion<'a, 'cx> {
1264-
cx: RefCell<&'a mut Context<'cx>>,
1265-
it: &'a clean::Item,
1266-
s: &'a clean::Union,
1267-
},
1268-
methods = [document, document_type_layout, render_attributes_in_pre, render_assoc_items]
1269-
);
1260+
// Only to be used by the `item_union()` function
1261+
item_template!(
1262+
#[template(path = "item_union.html")]
1263+
struct ItemUnion<'a, 'cx> {
1264+
cx: RefCell<&'a mut Context<'cx>>,
1265+
it: &'a clean::Item,
1266+
s: &'a clean::Union,
1267+
},
1268+
methods = [document, document_type_layout, render_attributes_in_pre, render_assoc_items]
1269+
);
1270+
1271+
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
1272+
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1273+
display_fn(move |f| {
1274+
let cx = self.cx.borrow_mut();
1275+
let v = render_union(self.it, Some(&self.s.generics), &self.s.fields, *cx);
1276+
write!(f, "{v}")
1277+
})
1278+
}
12701279

1271-
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
1272-
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1273-
display_fn(move |f| {
1274-
let cx = self.cx.borrow_mut();
1275-
let v = render_union(self.it, Some(&self.s.generics), &self.s.fields, *cx);
1276-
write!(f, "{v}")
1277-
})
1278-
}
1280+
fn document_field<'b>(
1281+
&'b self,
1282+
field: &'a clean::Item,
1283+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1284+
display_fn(move |f| {
1285+
let mut cx = self.cx.borrow_mut();
1286+
let v = document(*cx, field, Some(self.it), HeadingOffset::H3);
1287+
write!(f, "{v}")
1288+
})
1289+
}
12791290

1280-
fn document_field<'b>(
1281-
&'b self,
1282-
field: &'a clean::Item,
1283-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1284-
display_fn(move |f| {
1285-
let mut cx = self.cx.borrow_mut();
1286-
let v = document(*cx, field, Some(self.it), HeadingOffset::H3);
1287-
write!(f, "{v}")
1288-
})
1289-
}
1291+
fn stability_field(&self, field: &clean::Item) -> Option<String> {
1292+
let cx = self.cx.borrow();
1293+
field.stability_class(cx.tcx())
1294+
}
12901295

1291-
fn stability_field(&self, field: &clean::Item) -> Option<String> {
1296+
fn print_ty<'b>(
1297+
&'b self,
1298+
ty: &'a clean::Type,
1299+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1300+
display_fn(move |f| {
12921301
let cx = self.cx.borrow();
1293-
field.stability_class(cx.tcx())
1294-
}
1302+
let v = ty.print(*cx);
1303+
write!(f, "{v}")
1304+
})
1305+
}
12951306

1296-
fn print_ty<'b>(
1297-
&'b self,
1298-
ty: &'a clean::Type,
1299-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1300-
display_fn(move |f| {
1301-
let cx = self.cx.borrow();
1302-
let v = ty.print(*cx);
1303-
write!(f, "{v}")
1307+
fn fields_iter(
1308+
&self,
1309+
) -> std::iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>> {
1310+
self.s
1311+
.fields
1312+
.iter()
1313+
.filter_map(|f| match *f.kind {
1314+
clean::StructFieldItem(ref ty) => Some((f, ty)),
1315+
_ => None,
13041316
})
1305-
}
1306-
1307-
fn fields_iter(
1308-
&self,
1309-
) -> std::iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>> {
1310-
self.s
1311-
.fields
1312-
.iter()
1313-
.filter_map(|f| match *f.kind {
1314-
clean::StructFieldItem(ref ty) => Some((f, ty)),
1315-
_ => None,
1316-
})
1317-
.peekable()
1318-
}
1317+
.peekable()
13191318
}
1319+
}
13201320

1321+
fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
13211322
ItemUnion { cx: RefCell::new(cx), it, s }.render_into(w).unwrap();
13221323
}
13231324

@@ -1598,77 +1599,78 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
15981599
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
15991600
}
16001601

1601-
fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) {
1602-
item_template!(
1603-
#[template(path = "item_struct.html")]
1604-
struct ItemStruct<'a, 'cx> {
1605-
cx: RefCell<&'a mut Context<'cx>>,
1606-
it: &'a clean::Item,
1607-
s: &'a clean::Struct,
1608-
should_render_fields: bool,
1609-
},
1610-
methods = [render_attributes_in_code, document, render_assoc_items, document_type_layout]
1611-
);
1612-
1613-
struct Field<'a> {
1614-
item: &'a clean::Item,
1615-
name: String,
1616-
id: String,
1617-
ty: String,
1602+
// Only to be used by the `item_struct()` function
1603+
item_template!(
1604+
#[template(path = "item_struct.html")]
1605+
struct ItemStruct<'a, 'cx> {
1606+
cx: RefCell<&'a mut Context<'cx>>,
1607+
it: &'a clean::Item,
1608+
s: &'a clean::Struct,
1609+
should_render_fields: bool,
1610+
},
1611+
methods = [render_attributes_in_code, document, render_assoc_items, document_type_layout]
1612+
);
1613+
1614+
impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
1615+
fn new(
1616+
cx: std::cell::RefCell<&'a mut Context<'cx>>,
1617+
it: &'a clean::Item,
1618+
s: &'a clean::Struct,
1619+
) -> Self {
1620+
let should_render_fields = matches!(s.ctor_kind, None | Some(CtorKind::Fn))
1621+
&& struct_field_items(s).peekable().peek().is_some();
1622+
Self { cx, it, s, should_render_fields }
16181623
}
16191624

1620-
impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
1621-
fn new(
1622-
cx: std::cell::RefCell<&'a mut Context<'cx>>,
1623-
it: &'a clean::Item,
1624-
s: &'a clean::Struct,
1625-
) -> Self {
1626-
let should_render_fields = matches!(s.ctor_kind, None | Some(CtorKind::Fn))
1627-
&& struct_field_items(s).peekable().peek().is_some();
1628-
Self { cx, it, s, should_render_fields }
1629-
}
1630-
1631-
fn render_struct<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1632-
display_fn(move |f| {
1633-
let cx = self.cx.borrow();
1634-
let v = render_struct(
1635-
self.it,
1636-
Some(&self.s.generics),
1637-
self.s.ctor_kind,
1638-
&self.s.fields,
1639-
"",
1640-
true,
1641-
*cx,
1642-
);
1643-
write!(f, "{v}")
1644-
})
1645-
}
1625+
fn render_struct<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1626+
display_fn(move |f| {
1627+
let cx = self.cx.borrow();
1628+
let v = render_struct(
1629+
self.it,
1630+
Some(&self.s.generics),
1631+
self.s.ctor_kind,
1632+
&self.s.fields,
1633+
"",
1634+
true,
1635+
*cx,
1636+
);
1637+
write!(f, "{v}")
1638+
})
1639+
}
16461640

1647-
fn struct_field_items_iter<'b>(
1648-
&'b self,
1649-
) -> impl Iterator<Item = Field<'a>> + Captures<'a> + 'b + Captures<'cx> {
1650-
struct_field_items(self.s).enumerate().map(|(index, (item, ty))| {
1651-
let mut cx = self.cx.borrow_mut();
1652-
let name =
1653-
item.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string());
1654-
let id = cx.derive_id(format!("{}.{}", ItemType::StructField, name));
1655-
let ty = ty.print(*cx).to_string();
1656-
Field { item, name, id, ty }
1657-
})
1658-
}
1641+
fn struct_field_items_iter<'b>(
1642+
&'b self,
1643+
) -> impl Iterator<Item = ItemStructField<'a>> + Captures<'a> + 'b + Captures<'cx> {
1644+
struct_field_items(self.s).enumerate().map(|(index, (item, ty))| {
1645+
let mut cx = self.cx.borrow_mut();
1646+
let name = item.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string());
1647+
let id = cx.derive_id(format!("{}.{}", ItemType::StructField, name));
1648+
let ty = ty.print(*cx).to_string();
1649+
ItemStructField { item, name, id, ty }
1650+
})
1651+
}
16591652

1660-
fn document_field<'b>(
1661-
&'b self,
1662-
field: &'b clean::Item,
1663-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1664-
display_fn(move |f| {
1665-
let mut cx = self.cx.borrow_mut();
1666-
let v = document(*cx, field, Some(self.it), HeadingOffset::H3);
1667-
write!(f, "{v}")
1668-
})
1669-
}
1653+
fn document_field<'b>(
1654+
&'b self,
1655+
field: &'b clean::Item,
1656+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1657+
display_fn(move |f| {
1658+
let mut cx = self.cx.borrow_mut();
1659+
let v = document(*cx, field, Some(self.it), HeadingOffset::H3);
1660+
write!(f, "{v}")
1661+
})
16701662
}
1663+
}
1664+
1665+
// Only to be used by the `ItemStruct` struct
1666+
struct ItemStructField<'a> {
1667+
item: &'a clean::Item,
1668+
name: String,
1669+
id: String,
1670+
ty: String,
1671+
}
16711672

1673+
fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) {
16721674
ItemStruct::new(std::cell::RefCell::new(cx), it, s).render_into(w).unwrap();
16731675
}
16741676

0 commit comments

Comments
 (0)