rustdoc: use a separate template for type layout size

This commit is contained in:
Michael Howell 2023-04-18 10:21:04 -07:00
parent 08f204e17f
commit 5f9746b947
2 changed files with 24 additions and 12 deletions

View file

@ -1945,6 +1945,14 @@ fn document_type_layout<'a, 'cx: 'a>(
ty_def_id: DefId,
}
#[derive(Template)]
#[template(path = "type_layout_size.html")]
struct TypeLayoutSize {
is_unsized: bool,
is_uninhabited: bool,
size: u64,
}
impl<'a, 'cx: 'a> TypeLayout<'a, 'cx> {
fn variants<'b: 'a>(&'b self) -> Option<&'b IndexVec<VariantIdx, LayoutS>> {
if let Variants::Multiple { variants, .. } =
@ -1986,18 +1994,10 @@ fn document_type_layout<'a, 'cx: 'a>(
tag_size: u64,
) -> impl fmt::Display + Captures<'cx> + Captures<'b> {
display_fn(move |f| {
if layout.abi.is_unsized() {
write!(f, "(unsized)")?;
} else {
let size = layout.size.bytes() - tag_size;
write!(f, "{size} byte{pl}", pl = if size == 1 { "" } else { "s" })?;
if layout.abi.is_uninhabited() {
write!(
f,
" (<a href=\"https://doc.rust-lang.org/stable/reference/glossary.html#uninhabited\">uninhabited</a>)"
)?;
}
}
let is_unsized = layout.abi.is_unsized();
let is_uninhabited = layout.abi.is_uninhabited();
let size = layout.size.bytes() - tag_size;
TypeLayoutSize { is_unsized, is_uninhabited, size }.render_into(f).unwrap();
Ok(())
})
}

View file

@ -0,0 +1,12 @@
{% if is_unsized %}
(unsized)
{% else %}
{% if size == 1 %}
1 byte
{% else %}
{{ size +}} bytes
{% endif %}
{% if is_uninhabited %}
{# +#} (<a href="https://doc.rust-lang.org/stable/reference/glossary.html#uninhabited">uninhabited</a>)
{% endif %}
{% endif %}