Skip to content

Commit 1942752

Browse files
committed
make item_enum and item_variants return impl fmt::Display
1 parent 13b6a1f commit 1942752

File tree

1 file changed

+108
-112
lines changed

1 file changed

+108
-112
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 108 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut String)
245245
clean::TraitItem(ref t) => write_str(buf, format_args!("{}", item_trait(cx, item, t))),
246246
clean::StructItem(ref s) => write_str(buf, format_args!("{}", item_struct(cx, item, s))),
247247
clean::UnionItem(ref s) => write_str(buf, format_args!("{}", item_union(cx, item, s))),
248-
clean::EnumItem(ref e) => item_enum(buf, cx, item, e),
248+
clean::EnumItem(ref e) => write_str(buf, format_args!("{}", item_enum(cx, item, e))),
249249
clean::TypeAliasItem(ref t) => item_type_alias(buf, cx, item, t),
250250
clean::MacroItem(ref m) => item_macro(buf, cx, item, m),
251251
clean::ProcMacroItem(ref m) => item_proc_macro(buf, cx, item, m),
@@ -1315,7 +1315,7 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean
13151315
),
13161316
)
13171317
});
1318-
item_variants(w, cx, it, variants, enum_def_id);
1318+
write_str(w, format_args!("{}", item_variants(cx, it, variants, enum_def_id)));
13191319
}
13201320
clean::TypeAliasInnerType::Union { fields } => {
13211321
wrap_item(w, |w| {
@@ -1568,13 +1568,17 @@ fn print_tuple_struct_fields<'a, 'cx: 'a>(
15681568
})
15691569
}
15701570

1571-
fn item_enum(w: &mut String, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) {
1572-
let count_variants = e.variants().count();
1573-
wrap_item(w, |w| {
1574-
render_attributes_in_code(w, it, cx);
1575-
write_str(
1576-
w,
1577-
format_args!(
1571+
fn item_enum<'a, 'tcx>(
1572+
cx: &'a Context<'tcx>,
1573+
it: &'a clean::Item,
1574+
e: &'a clean::Enum,
1575+
) -> impl fmt::Display + 'a + Captures<'tcx> {
1576+
fmt::from_fn(|w| {
1577+
let count_variants = e.variants().count();
1578+
wrap_item(w, |w| {
1579+
render_attributes_in_code(w, it, cx);
1580+
write!(
1581+
w,
15781582
"{}enum {}{}{}",
15791583
visibility_print_with_space(it, cx),
15801584
it.name.unwrap(),
@@ -1588,18 +1592,22 @@ fn item_enum(w: &mut String, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
15881592
it.is_non_exhaustive(),
15891593
it.def_id().unwrap(),
15901594
),
1591-
),
1592-
);
1593-
});
1595+
)
1596+
})?;
15941597

1595-
write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2)));
1598+
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?;
15961599

1597-
if count_variants != 0 {
1598-
item_variants(w, cx, it, &e.variants, it.def_id().unwrap());
1599-
}
1600-
let def_id = it.item_id.expect_def_id();
1601-
write_str(w, format_args!("{}", render_assoc_items(cx, it, def_id, AssocItemRender::All)));
1602-
write_str(w, format_args!("{}", document_type_layout(cx, def_id)));
1600+
if count_variants != 0 {
1601+
write!(w, "{}", item_variants(cx, it, &e.variants, it.def_id().unwrap()))?;
1602+
}
1603+
let def_id = it.item_id.expect_def_id();
1604+
write!(
1605+
w,
1606+
"{}{}",
1607+
render_assoc_items(cx, it, def_id, AssocItemRender::All),
1608+
document_type_layout(cx, def_id)
1609+
)
1610+
})
16031611
}
16041612

16051613
/// It'll return false if any variant is not a C-like variant. Otherwise it'll return true if at
@@ -1733,36 +1741,34 @@ fn render_enum_fields<'a, 'tcx>(
17331741
})
17341742
}
17351743

1736-
fn item_variants(
1737-
w: &mut String,
1738-
cx: &Context<'_>,
1739-
it: &clean::Item,
1740-
variants: &IndexVec<VariantIdx, clean::Item>,
1744+
fn item_variants<'a, 'tcx>(
1745+
cx: &'a Context<'tcx>,
1746+
it: &'a clean::Item,
1747+
variants: &'a IndexVec<VariantIdx, clean::Item>,
17411748
enum_def_id: DefId,
1742-
) {
1743-
let tcx = cx.tcx();
1744-
write_str(
1745-
w,
1746-
format_args!(
1749+
) -> impl fmt::Display + 'a + Captures<'tcx> {
1750+
fmt::from_fn(move |w| {
1751+
let tcx = cx.tcx();
1752+
write!(
1753+
w,
17471754
"{}",
17481755
write_section_heading(
17491756
&format!("Variants{}", document_non_exhaustive_header(it)),
17501757
"variants",
17511758
Some("variants"),
17521759
format!("{}<div class=\"variants\">", document_non_exhaustive(it)),
1753-
)
1754-
),
1755-
);
1760+
),
1761+
)?;
17561762

1757-
let should_show_enum_discriminant = should_show_enum_discriminant(cx, enum_def_id, variants);
1758-
for (index, variant) in variants.iter_enumerated() {
1759-
if variant.is_stripped() {
1760-
continue;
1761-
}
1762-
let id = cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.unwrap()));
1763-
write_str(
1764-
w,
1765-
format_args!(
1763+
let should_show_enum_discriminant =
1764+
should_show_enum_discriminant(cx, enum_def_id, variants);
1765+
for (index, variant) in variants.iter_enumerated() {
1766+
if variant.is_stripped() {
1767+
continue;
1768+
}
1769+
let id = cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.unwrap()));
1770+
write!(
1771+
w,
17661772
"<section id=\"{id}\" class=\"variant\">\
17671773
<a href=\"#{id}\" class=\"anchor\">§</a>\
17681774
{}\
@@ -1773,14 +1779,12 @@ fn item_variants(
17731779
" rightside",
17741780
)
17751781
.maybe_display()
1776-
),
1777-
);
1778-
if let clean::VariantItem(ref var) = variant.kind
1779-
&& let clean::VariantKind::CLike = var.kind
1780-
{
1781-
write_str(
1782-
w,
1783-
format_args!(
1782+
)?;
1783+
if let clean::VariantItem(ref var) = variant.kind
1784+
&& let clean::VariantKind::CLike = var.kind
1785+
{
1786+
write!(
1787+
w,
17841788
"{}",
17851789
display_c_like_variant(
17861790
cx,
@@ -1790,90 +1794,82 @@ fn item_variants(
17901794
should_show_enum_discriminant,
17911795
enum_def_id,
17921796
)
1793-
),
1794-
);
1795-
} else {
1796-
w.push_str(variant.name.unwrap().as_str());
1797-
}
1797+
)?;
1798+
} else {
1799+
w.write_str(variant.name.unwrap().as_str())?;
1800+
}
17981801

1799-
let clean::VariantItem(variant_data) = &variant.kind else { unreachable!() };
1802+
let clean::VariantItem(variant_data) = &variant.kind else { unreachable!() };
18001803

1801-
if let clean::VariantKind::Tuple(ref s) = variant_data.kind {
1802-
write_str(w, format_args!("({})", print_tuple_struct_fields(cx, s)));
1803-
}
1804-
w.push_str("</h3></section>");
1804+
if let clean::VariantKind::Tuple(ref s) = variant_data.kind {
1805+
write!(w, "({})", print_tuple_struct_fields(cx, s))?;
1806+
}
1807+
w.write_str("</h3></section>")?;
18051808

1806-
write_str(w, format_args!("{}", document(cx, variant, Some(it), HeadingOffset::H4)));
1809+
write!(w, "{}", document(cx, variant, Some(it), HeadingOffset::H4))?;
18071810

1808-
let heading_and_fields = match &variant_data.kind {
1809-
clean::VariantKind::Struct(s) => {
1810-
// If there is no field to display, no need to add the heading.
1811-
if s.fields.iter().any(|f| !f.is_doc_hidden()) {
1812-
Some(("Fields", &s.fields))
1813-
} else {
1814-
None
1811+
let heading_and_fields = match &variant_data.kind {
1812+
clean::VariantKind::Struct(s) => {
1813+
// If there is no field to display, no need to add the heading.
1814+
if s.fields.iter().any(|f| !f.is_doc_hidden()) {
1815+
Some(("Fields", &s.fields))
1816+
} else {
1817+
None
1818+
}
18151819
}
1816-
}
1817-
clean::VariantKind::Tuple(fields) => {
1818-
// Documentation on tuple variant fields is rare, so to reduce noise we only emit
1819-
// the section if at least one field is documented.
1820-
if fields.iter().any(|f| !f.doc_value().is_empty()) {
1821-
Some(("Tuple Fields", fields))
1822-
} else {
1823-
None
1820+
clean::VariantKind::Tuple(fields) => {
1821+
// Documentation on tuple variant fields is rare, so to reduce noise we only emit
1822+
// the section if at least one field is documented.
1823+
if fields.iter().any(|f| !f.doc_value().is_empty()) {
1824+
Some(("Tuple Fields", fields))
1825+
} else {
1826+
None
1827+
}
18241828
}
1825-
}
1826-
clean::VariantKind::CLike => None,
1827-
};
1829+
clean::VariantKind::CLike => None,
1830+
};
18281831

1829-
if let Some((heading, fields)) = heading_and_fields {
1830-
let variant_id =
1831-
cx.derive_id(format!("{}.{}.fields", ItemType::Variant, variant.name.unwrap()));
1832-
write_str(
1833-
w,
1834-
format_args!(
1832+
if let Some((heading, fields)) = heading_and_fields {
1833+
let variant_id =
1834+
cx.derive_id(format!("{}.{}.fields", ItemType::Variant, variant.name.unwrap()));
1835+
write!(
1836+
w,
18351837
"<div class=\"sub-variant\" id=\"{variant_id}\">\
18361838
<h4>{heading}</h4>\
18371839
{}",
18381840
document_non_exhaustive(variant)
1839-
),
1840-
);
1841-
for field in fields {
1842-
match field.kind {
1843-
clean::StrippedItem(box clean::StructFieldItem(_)) => {}
1844-
clean::StructFieldItem(ref ty) => {
1845-
let id = cx.derive_id(format!(
1846-
"variant.{}.field.{}",
1847-
variant.name.unwrap(),
1848-
field.name.unwrap()
1849-
));
1850-
write_str(
1851-
w,
1852-
format_args!(
1841+
)?;
1842+
for field in fields {
1843+
match field.kind {
1844+
clean::StrippedItem(box clean::StructFieldItem(_)) => {}
1845+
clean::StructFieldItem(ref ty) => {
1846+
let id = cx.derive_id(format!(
1847+
"variant.{}.field.{}",
1848+
variant.name.unwrap(),
1849+
field.name.unwrap()
1850+
));
1851+
write!(
1852+
w,
18531853
"<div class=\"sub-variant-field\">\
18541854
<span id=\"{id}\" class=\"section-header\">\
18551855
<a href=\"#{id}\" class=\"anchor field\">§</a>\
18561856
<code>{f}: {t}</code>\
1857-
</span>",
1857+
</span>\
1858+
{doc}\
1859+
</div>",
18581860
f = field.name.unwrap(),
18591861
t = ty.print(cx),
1860-
),
1861-
);
1862-
write_str(
1863-
w,
1864-
format_args!(
1865-
"{}</div>",
1866-
document(cx, field, Some(variant), HeadingOffset::H5),
1867-
),
1868-
);
1862+
doc = document(cx, field, Some(variant), HeadingOffset::H5),
1863+
)?;
1864+
}
1865+
_ => unreachable!(),
18691866
}
1870-
_ => unreachable!(),
18711867
}
1868+
w.write_str("</div>")?;
18721869
}
1873-
w.push_str("</div>");
18741870
}
1875-
}
1876-
write_str(w, format_args!("</div>"));
1871+
w.write_str("</div>")
1872+
})
18771873
}
18781874

18791875
fn item_macro(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) {

0 commit comments

Comments
 (0)