Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 001f0dd

Browse files
committed
rustdoc: Show basic type layout information
Right now it just shows the size.
1 parent 2bafe96 commit 001f0dd

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
846846
});
847847

848848
document(w, cx, it, None);
849+
849850
let mut fields = s
850851
.fields
851852
.iter()
@@ -880,7 +881,9 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
880881
document(w, cx, field, Some(it));
881882
}
882883
}
883-
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All)
884+
let def_id = it.def_id.expect_real();
885+
render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
886+
document_ty_layout(w, cx, def_id);
884887
}
885888

886889
fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) {
@@ -940,6 +943,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
940943
});
941944

942945
document(w, cx, it, None);
946+
943947
if !e.variants.is_empty() {
944948
write!(
945949
w,
@@ -1014,7 +1018,9 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
10141018
render_stability_since(w, variant, it, cx.tcx());
10151019
}
10161020
}
1017-
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All)
1021+
let def_id = it.def_id.expect_real();
1022+
render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
1023+
document_ty_layout(w, cx, def_id);
10181024
}
10191025

10201026
fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) {
@@ -1114,6 +1120,7 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
11141120
});
11151121

11161122
document(w, cx, it, None);
1123+
11171124
let mut fields = s
11181125
.fields
11191126
.iter()
@@ -1152,7 +1159,9 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
11521159
}
11531160
}
11541161
}
1155-
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All)
1162+
let def_id = it.def_id.expect_real();
1163+
render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
1164+
document_ty_layout(w, cx, def_id);
11561165
}
11571166

11581167
fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Static) {
@@ -1522,3 +1531,27 @@ fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) {
15221531
w.write_str("</div></details>");
15231532
}
15241533
}
1534+
1535+
fn document_ty_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
1536+
let param_env = cx.tcx().param_env(ty_def_id);
1537+
let ty = cx.tcx().type_of(ty_def_id);
1538+
match cx.tcx().layout_of(param_env.and(ty)) {
1539+
Ok(ty_layout) => {
1540+
writeln!(w, r#"<h2 class="small-section-header">Layout</h2>"#);
1541+
writeln!(w, "<div>");
1542+
if ty_layout.layout.abi.is_unsized() {
1543+
writeln!(w, "<strong>Sized:</strong> (unsized)");
1544+
} else {
1545+
writeln!(
1546+
w,
1547+
"<strong>Size:</strong> {size} byte{pl}",
1548+
size = ty_layout.layout.size.bytes(),
1549+
pl = if ty_layout.layout.size.bytes() == 1 { "" } else { "s" },
1550+
);
1551+
}
1552+
writeln!(w, "</div>");
1553+
}
1554+
// FIXME: should we crash instead? or report an error?
1555+
Err(_layout_err) => {}
1556+
}
1557+
}

0 commit comments

Comments
 (0)