#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.
#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
rust/src/librustdoc/html/render/print_item.rs
Lines 1553 to 1574 in da7ada5
The
ty_layoutvariable is aTyAndLayout. We want to handle the case wherety_layout.layout.variantsisVariants::Multiple.In that case we have access to anIndexVec<VariantIdx, Layout>.Layoutpart will give us the size of the variantAdtDefstored inTyKind::Adt, which we can hopefully get fromty_layout.ty.kind.