rustdoc: convert print_tuple_struct_fields to return a Display

This commit is contained in:
Michael Howell 2023-04-03 15:30:28 -07:00
parent bf41e753ec
commit fc5de13d31

View File

@ -1174,17 +1174,23 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
document_type_layout(w, cx, def_id); document_type_layout(w, cx, def_id);
} }
fn print_tuple_struct_fields(w: &mut Buffer, cx: &Context<'_>, s: &[clean::Item]) { fn print_tuple_struct_fields<'a, 'cx: 'a>(
for (i, ty) in s.iter().enumerate() { cx: &'a Context<'cx>,
if i > 0 { s: &'a [clean::Item],
w.write_str(", "); ) -> impl fmt::Display + 'a + Captures<'cx> {
display_fn(|f| {
for (i, ty) in s.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
match *ty.kind {
clean::StrippedItem(box clean::StructFieldItem(_)) => f.write_str("_")?,
clean::StructFieldItem(ref ty) => write!(f, "{}", ty.print(cx))?,
_ => unreachable!(),
}
} }
match *ty.kind { Ok(())
clean::StrippedItem(box clean::StructFieldItem(_)) => w.write_str("_"), })
clean::StructFieldItem(ref ty) => write!(w, "{}", ty.print(cx)),
_ => unreachable!(),
}
}
} }
fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::Enum) { fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::Enum) {
@ -1221,9 +1227,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
clean::VariantItem(ref var) => match var.kind { clean::VariantItem(ref var) => match var.kind {
clean::VariantKind::CLike => write!(w, "{}", name), clean::VariantKind::CLike => write!(w, "{}", name),
clean::VariantKind::Tuple(ref s) => { clean::VariantKind::Tuple(ref s) => {
write!(w, "{}(", name); write!(w, "{name}({})", print_tuple_struct_fields(cx, s),);
print_tuple_struct_fields(w, cx, s);
w.write_str(")");
} }
clean::VariantKind::Struct(ref s) => { clean::VariantKind::Struct(ref s) => {
render_struct(w, v, None, None, &s.fields, " ", false, cx); render_struct(w, v, None, None, &s.fields, " ", false, cx);
@ -1276,9 +1280,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
let clean::VariantItem(variant_data) = &*variant.kind else { unreachable!() }; let clean::VariantItem(variant_data) = &*variant.kind else { unreachable!() };
if let clean::VariantKind::Tuple(ref s) = variant_data.kind { if let clean::VariantKind::Tuple(ref s) = variant_data.kind {
w.write_str("("); write!(w, "({})", print_tuple_struct_fields(cx, s),);
print_tuple_struct_fields(w, cx, s);
w.write_str(")");
} }
w.write_str("</h3></section>"); w.write_str("</h3></section>");