Skip to content

Commit 2b728c1

Browse files
committed
rustdoc: factor document_type_layout into its own module
1 parent 99e1cdb commit 2b728c1

File tree

3 files changed

+90
-77
lines changed

3 files changed

+90
-77
lines changed

src/librustdoc/html/render/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mod context;
3232
mod print_item;
3333
mod sidebar;
3434
mod span_map;
35+
mod type_layout;
3536
mod write_shared;
3637

3738
pub(crate) use self::context::*;

src/librustdoc/html/render/print_item.rs

+2-77
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ use rustc_hir as hir;
66
use rustc_hir::def::CtorKind;
77
use rustc_hir::def_id::DefId;
88
use rustc_middle::middle::stability;
9-
use rustc_middle::span_bug;
10-
use rustc_middle::ty::layout::LayoutError;
11-
use rustc_middle::ty::{self, Adt, TyCtxt};
9+
use rustc_middle::ty::{self, TyCtxt};
1210
use rustc_span::hygiene::MacroKind;
1311
use rustc_span::symbol::{kw, sym, Symbol};
14-
use rustc_target::abi::{Primitive, TagEncoding, Variants};
1512
use std::cmp::Ordering;
1613
use std::fmt;
1714
use std::rc::Rc;
1815

16+
use super::type_layout::document_type_layout;
1917
use super::{
2018
collect_paths_for_type, document, ensure_trailing_slash, get_filtered_impls_for_reference,
2119
item_ty_to_section, notable_traits_button, notable_traits_json, render_all_impls,
@@ -1932,79 +1930,6 @@ fn document_non_exhaustive<'a>(item: &'a clean::Item) -> impl fmt::Display + 'a
19321930
})
19331931
}
19341932

1935-
fn document_type_layout<'a, 'cx: 'a>(
1936-
cx: &'a Context<'cx>,
1937-
ty_def_id: DefId,
1938-
) -> impl fmt::Display + 'a + Captures<'cx> {
1939-
#[derive(Template)]
1940-
#[template(path = "type_layout.html")]
1941-
struct TypeLayout<'cx> {
1942-
variants: Vec<(Symbol, TypeLayoutSize)>,
1943-
type_layout_size: Result<TypeLayoutSize, LayoutError<'cx>>,
1944-
}
1945-
1946-
#[derive(Template)]
1947-
#[template(path = "type_layout_size.html")]
1948-
struct TypeLayoutSize {
1949-
is_unsized: bool,
1950-
is_uninhabited: bool,
1951-
size: u64,
1952-
}
1953-
1954-
display_fn(move |f| {
1955-
if !cx.shared.show_type_layout {
1956-
return Ok(());
1957-
}
1958-
1959-
let tcx = cx.tcx();
1960-
let param_env = tcx.param_env(ty_def_id);
1961-
let ty = tcx.type_of(ty_def_id).subst_identity();
1962-
let type_layout = tcx.layout_of(param_env.and(ty));
1963-
1964-
let variants =
1965-
if let Ok(type_layout) = type_layout &&
1966-
let Variants::Multiple { variants, tag, tag_encoding, .. } =
1967-
type_layout.layout.variants() &&
1968-
!variants.is_empty()
1969-
{
1970-
let tag_size =
1971-
if let TagEncoding::Niche { .. } = tag_encoding {
1972-
0
1973-
} else if let Primitive::Int(i, _) = tag.primitive() {
1974-
i.size().bytes()
1975-
} else {
1976-
span_bug!(cx.tcx().def_span(ty_def_id), "tag is neither niche nor int")
1977-
};
1978-
let variants = variants
1979-
.iter_enumerated()
1980-
.map(|(variant_idx, variant_layout)| {
1981-
let Adt(adt, _) = type_layout.ty.kind() else {
1982-
span_bug!(cx.tcx().def_span(ty_def_id), "not an adt")
1983-
};
1984-
let name = adt.variant(variant_idx).name;
1985-
let is_unsized = variant_layout.abi.is_unsized();
1986-
let is_uninhabited = variant_layout.abi.is_uninhabited();
1987-
let size = variant_layout.size.bytes() - tag_size;
1988-
let type_layout_size = TypeLayoutSize { is_unsized, is_uninhabited, size };
1989-
(name, type_layout_size)
1990-
}).collect();
1991-
variants
1992-
} else {
1993-
Vec::new()
1994-
}
1995-
;
1996-
1997-
let type_layout_size = tcx.layout_of(param_env.and(ty)).map(|layout| {
1998-
let is_unsized = layout.abi.is_unsized();
1999-
let is_uninhabited = layout.abi.is_uninhabited();
2000-
let size = layout.size.bytes();
2001-
TypeLayoutSize { is_unsized, is_uninhabited, size }
2002-
});
2003-
2004-
Ok(TypeLayout { variants, type_layout_size }.render_into(f).unwrap())
2005-
})
2006-
}
2007-
20081933
fn pluralize(count: usize) -> &'static str {
20091934
if count > 1 { "s" } else { "" }
20101935
}
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use askama::Template;
2+
3+
use rustc_data_structures::captures::Captures;
4+
use rustc_hir::def_id::DefId;
5+
use rustc_middle::span_bug;
6+
use rustc_middle::ty::layout::LayoutError;
7+
use rustc_middle::ty::Adt;
8+
use rustc_span::symbol::Symbol;
9+
use rustc_target::abi::{Primitive, TagEncoding, Variants};
10+
11+
use std::fmt;
12+
13+
use crate::html::format::display_fn;
14+
use crate::html::render::Context;
15+
16+
#[derive(Template)]
17+
#[template(path = "type_layout.html")]
18+
struct TypeLayout<'cx> {
19+
variants: Vec<(Symbol, TypeLayoutSize)>,
20+
type_layout_size: Result<TypeLayoutSize, LayoutError<'cx>>,
21+
}
22+
23+
#[derive(Template)]
24+
#[template(path = "type_layout_size.html")]
25+
struct TypeLayoutSize {
26+
is_unsized: bool,
27+
is_uninhabited: bool,
28+
size: u64,
29+
}
30+
31+
pub(crate) fn document_type_layout<'a, 'cx: 'a>(
32+
cx: &'a Context<'cx>,
33+
ty_def_id: DefId,
34+
) -> impl fmt::Display + 'a + Captures<'cx> {
35+
display_fn(move |f| {
36+
if !cx.shared.show_type_layout {
37+
return Ok(());
38+
}
39+
40+
let tcx = cx.tcx();
41+
let param_env = tcx.param_env(ty_def_id);
42+
let ty = tcx.type_of(ty_def_id).subst_identity();
43+
let type_layout = tcx.layout_of(param_env.and(ty));
44+
45+
let variants =
46+
if let Ok(type_layout) = type_layout &&
47+
let Variants::Multiple { variants, tag, tag_encoding, .. } =
48+
type_layout.layout.variants() &&
49+
!variants.is_empty()
50+
{
51+
let tag_size =
52+
if let TagEncoding::Niche { .. } = tag_encoding {
53+
0
54+
} else if let Primitive::Int(i, _) = tag.primitive() {
55+
i.size().bytes()
56+
} else {
57+
span_bug!(cx.tcx().def_span(ty_def_id), "tag is neither niche nor int")
58+
};
59+
let variants = variants
60+
.iter_enumerated()
61+
.map(|(variant_idx, variant_layout)| {
62+
let Adt(adt, _) = type_layout.ty.kind() else {
63+
span_bug!(cx.tcx().def_span(ty_def_id), "not an adt")
64+
};
65+
let name = adt.variant(variant_idx).name;
66+
let is_unsized = variant_layout.abi.is_unsized();
67+
let is_uninhabited = variant_layout.abi.is_uninhabited();
68+
let size = variant_layout.size.bytes() - tag_size;
69+
let type_layout_size = TypeLayoutSize { is_unsized, is_uninhabited, size };
70+
(name, type_layout_size)
71+
}).collect();
72+
variants
73+
} else {
74+
Vec::new()
75+
}
76+
;
77+
78+
let type_layout_size = tcx.layout_of(param_env.and(ty)).map(|layout| {
79+
let is_unsized = layout.abi.is_unsized();
80+
let is_uninhabited = layout.abi.is_uninhabited();
81+
let size = layout.size.bytes();
82+
TypeLayoutSize { is_unsized, is_uninhabited, size }
83+
});
84+
85+
Ok(TypeLayout { variants, type_layout_size }.render_into(f).unwrap())
86+
})
87+
}

0 commit comments

Comments
 (0)