Skip to content

Rustdoc: report layout of enum variants #86253

Closed
@LeSeulArtichaut

Description

@LeSeulArtichaut

#83501 introduced a basic section on layout information, with only one piece of information: the total size of the type. I think one useful but easy extension to this is reporting the size of each variant for an enum.

cc @camelid

Implementation hints

The "Layout" section is rendered in

Ok(ty_layout) => {
writeln!(
w,
"<div class=\"warning\"><p><strong>Note:</strong> Most layout information is \
completely unstable and may be different between compiler versions and platforms. \
The only exception is types with certain <code>repr(...)</code> attributes. \
Please see the Rust Reference’s \
<a href=\"https://doc.rust-lang.org/reference/type-layout.html\">“Type Layout”</a> \
chapter for details on type layout guarantees.</p></div>"
);
if ty_layout.layout.abi.is_unsized() {
writeln!(w, "<p><strong>Size:</strong> (unsized)</p>");
} else {
let bytes = ty_layout.layout.size.bytes();
writeln!(
w,
"<p><strong>Size:</strong> {size} byte{pl}</p>",
size = bytes,
pl = if bytes == 1 { "" } else { "s" },
);
}
}

The ty_layout variable is a TyAndLayout. We want to handle the case where ty_layout.layout.variants is Variants::Multiple.In that case we have access to an IndexVec<VariantIdx, Layout>.

  • The Layout part will give us the size of the variant
  • To get the name of the variant, we'll need the AdtDef stored in TyKind::Adt, which we can hopefully get from ty_layout.ty.kind.

Metadata

Metadata

Assignees

Labels

A-rustdoc-type-layoutArea: `rustdoc --show-type-layout` (nightly-only)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions