Skip to content

Commit 5e3980b

Browse files
committed
Move item template structs outside of function
1 parent faacad5 commit 5e3980b

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
@@ -1331,67 +1331,68 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
13311331
write!(w, "{}", document_type_layout(cx, def_id));
13321332
}
13331333

1334-
fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
1335-
item_template!(
1336-
#[template(path = "item_union.html")]
1337-
struct ItemUnion<'a, 'cx> {
1338-
cx: RefCell<&'a mut Context<'cx>>,
1339-
it: &'a clean::Item,
1340-
s: &'a clean::Union,
1341-
},
1342-
methods = [document, document_type_layout, render_attributes_in_pre, render_assoc_items]
1343-
);
1334+
// Only to be used by the `item_union()` function
1335+
item_template!(
1336+
#[template(path = "item_union.html")]
1337+
struct ItemUnion<'a, 'cx> {
1338+
cx: RefCell<&'a mut Context<'cx>>,
1339+
it: &'a clean::Item,
1340+
s: &'a clean::Union,
1341+
},
1342+
methods = [document, document_type_layout, render_attributes_in_pre, render_assoc_items]
1343+
);
1344+
1345+
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
1346+
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1347+
display_fn(move |f| {
1348+
let cx = self.cx.borrow_mut();
1349+
let v = render_union(self.it, Some(&self.s.generics), &self.s.fields, *cx);
1350+
write!(f, "{v}")
1351+
})
1352+
}
13441353

1345-
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
1346-
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1347-
display_fn(move |f| {
1348-
let cx = self.cx.borrow_mut();
1349-
let v = render_union(self.it, Some(&self.s.generics), &self.s.fields, *cx);
1350-
write!(f, "{v}")
1351-
})
1352-
}
1354+
fn document_field<'b>(
1355+
&'b self,
1356+
field: &'a clean::Item,
1357+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1358+
display_fn(move |f| {
1359+
let mut cx = self.cx.borrow_mut();
1360+
let v = document(*cx, field, Some(self.it), HeadingOffset::H3);
1361+
write!(f, "{v}")
1362+
})
1363+
}
13531364

1354-
fn document_field<'b>(
1355-
&'b self,
1356-
field: &'a clean::Item,
1357-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1358-
display_fn(move |f| {
1359-
let mut cx = self.cx.borrow_mut();
1360-
let v = document(*cx, field, Some(self.it), HeadingOffset::H3);
1361-
write!(f, "{v}")
1362-
})
1363-
}
1365+
fn stability_field(&self, field: &clean::Item) -> Option<String> {
1366+
let cx = self.cx.borrow();
1367+
field.stability_class(cx.tcx())
1368+
}
13641369

1365-
fn stability_field(&self, field: &clean::Item) -> Option<String> {
1370+
fn print_ty<'b>(
1371+
&'b self,
1372+
ty: &'a clean::Type,
1373+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1374+
display_fn(move |f| {
13661375
let cx = self.cx.borrow();
1367-
field.stability_class(cx.tcx())
1368-
}
1376+
let v = ty.print(*cx);
1377+
write!(f, "{v}")
1378+
})
1379+
}
13691380

1370-
fn print_ty<'b>(
1371-
&'b self,
1372-
ty: &'a clean::Type,
1373-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1374-
display_fn(move |f| {
1375-
let cx = self.cx.borrow();
1376-
let v = ty.print(*cx);
1377-
write!(f, "{v}")
1381+
fn fields_iter(
1382+
&self,
1383+
) -> std::iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>> {
1384+
self.s
1385+
.fields
1386+
.iter()
1387+
.filter_map(|f| match *f.kind {
1388+
clean::StructFieldItem(ref ty) => Some((f, ty)),
1389+
_ => None,
13781390
})
1379-
}
1380-
1381-
fn fields_iter(
1382-
&self,
1383-
) -> std::iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>> {
1384-
self.s
1385-
.fields
1386-
.iter()
1387-
.filter_map(|f| match *f.kind {
1388-
clean::StructFieldItem(ref ty) => Some((f, ty)),
1389-
_ => None,
1390-
})
1391-
.peekable()
1392-
}
1391+
.peekable()
13931392
}
1393+
}
13941394

1395+
fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
13951396
ItemUnion { cx: RefCell::new(cx), it, s }.render_into(w).unwrap();
13961397
}
13971398

@@ -1820,77 +1821,78 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
18201821
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
18211822
}
18221823

1823-
fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) {
1824-
item_template!(
1825-
#[template(path = "item_struct.html")]
1826-
struct ItemStruct<'a, 'cx> {
1827-
cx: RefCell<&'a mut Context<'cx>>,
1828-
it: &'a clean::Item,
1829-
s: &'a clean::Struct,
1830-
should_render_fields: bool,
1831-
},
1832-
methods = [render_attributes_in_code, document, render_assoc_items, document_type_layout]
1833-
);
1834-
1835-
struct Field<'a> {
1836-
item: &'a clean::Item,
1837-
name: String,
1838-
id: String,
1839-
ty: String,
1824+
// Only to be used by the `item_struct()` function
1825+
item_template!(
1826+
#[template(path = "item_struct.html")]
1827+
struct ItemStruct<'a, 'cx> {
1828+
cx: RefCell<&'a mut Context<'cx>>,
1829+
it: &'a clean::Item,
1830+
s: &'a clean::Struct,
1831+
should_render_fields: bool,
1832+
},
1833+
methods = [render_attributes_in_code, document, render_assoc_items, document_type_layout]
1834+
);
1835+
1836+
impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
1837+
fn new(
1838+
cx: std::cell::RefCell<&'a mut Context<'cx>>,
1839+
it: &'a clean::Item,
1840+
s: &'a clean::Struct,
1841+
) -> Self {
1842+
let should_render_fields = matches!(s.ctor_kind, None | Some(CtorKind::Fn))
1843+
&& struct_field_items(s).peekable().peek().is_some();
1844+
Self { cx, it, s, should_render_fields }
18401845
}
18411846

1842-
impl<'a, 'cx: 'a> ItemStruct<'a, 'cx> {
1843-
fn new(
1844-
cx: std::cell::RefCell<&'a mut Context<'cx>>,
1845-
it: &'a clean::Item,
1846-
s: &'a clean::Struct,
1847-
) -> Self {
1848-
let should_render_fields = matches!(s.ctor_kind, None | Some(CtorKind::Fn))
1849-
&& struct_field_items(s).peekable().peek().is_some();
1850-
Self { cx, it, s, should_render_fields }
1851-
}
1852-
1853-
fn render_struct<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1854-
display_fn(move |f| {
1855-
let cx = self.cx.borrow();
1856-
let v = render_struct(
1857-
self.it,
1858-
Some(&self.s.generics),
1859-
self.s.ctor_kind,
1860-
&self.s.fields,
1861-
"",
1862-
true,
1863-
*cx,
1864-
);
1865-
write!(f, "{v}")
1866-
})
1867-
}
1847+
fn render_struct<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1848+
display_fn(move |f| {
1849+
let cx = self.cx.borrow();
1850+
let v = render_struct(
1851+
self.it,
1852+
Some(&self.s.generics),
1853+
self.s.ctor_kind,
1854+
&self.s.fields,
1855+
"",
1856+
true,
1857+
*cx,
1858+
);
1859+
write!(f, "{v}")
1860+
})
1861+
}
18681862

1869-
fn struct_field_items_iter<'b>(
1870-
&'b self,
1871-
) -> impl Iterator<Item = Field<'a>> + Captures<'a> + 'b + Captures<'cx> {
1872-
struct_field_items(self.s).enumerate().map(|(index, (item, ty))| {
1873-
let mut cx = self.cx.borrow_mut();
1874-
let name =
1875-
item.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string());
1876-
let id = cx.derive_id(format!("{}.{}", ItemType::StructField, name));
1877-
let ty = ty.print(*cx).to_string();
1878-
Field { item, name, id, ty }
1879-
})
1880-
}
1863+
fn struct_field_items_iter<'b>(
1864+
&'b self,
1865+
) -> impl Iterator<Item = ItemStructField<'a>> + Captures<'a> + 'b + Captures<'cx> {
1866+
struct_field_items(self.s).enumerate().map(|(index, (item, ty))| {
1867+
let mut cx = self.cx.borrow_mut();
1868+
let name = item.name.map_or_else(|| index.to_string(), |sym| sym.as_str().to_string());
1869+
let id = cx.derive_id(format!("{}.{}", ItemType::StructField, name));
1870+
let ty = ty.print(*cx).to_string();
1871+
ItemStructField { item, name, id, ty }
1872+
})
1873+
}
18811874

1882-
fn document_field<'b>(
1883-
&'b self,
1884-
field: &'b clean::Item,
1885-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1886-
display_fn(move |f| {
1887-
let mut cx = self.cx.borrow_mut();
1888-
let v = document(*cx, field, Some(self.it), HeadingOffset::H3);
1889-
write!(f, "{v}")
1890-
})
1891-
}
1875+
fn document_field<'b>(
1876+
&'b self,
1877+
field: &'b clean::Item,
1878+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1879+
display_fn(move |f| {
1880+
let mut cx = self.cx.borrow_mut();
1881+
let v = document(*cx, field, Some(self.it), HeadingOffset::H3);
1882+
write!(f, "{v}")
1883+
})
18921884
}
1885+
}
1886+
1887+
// Only to be used by the `ItemStruct` struct
1888+
struct ItemStructField<'a> {
1889+
item: &'a clean::Item,
1890+
name: String,
1891+
id: String,
1892+
ty: String,
1893+
}
18931894

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

0 commit comments

Comments
 (0)