Skip to content

Commit c349b79

Browse files
committed
Apply suggestions
1 parent aff4cd5 commit c349b79

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use rustc_data_structures::fx::FxHashMap;
77
use rustc_hir as hir;
88
use rustc_hir::def::CtorKind;
99
use rustc_hir::def_id::DefId;
10-
use rustc_middle::bug;
1110
use rustc_middle::middle::stability;
11+
use rustc_middle::span_bug;
1212
use rustc_middle::ty::layout::LayoutError;
1313
use rustc_middle::ty::{Adt, TyCtxt};
1414
use rustc_span::hygiene::MacroKind;
1515
use rustc_span::symbol::{kw, sym, Symbol};
16-
use rustc_target::abi::Variants;
16+
use rustc_target::abi::{Layout, Variants};
1717

1818
use super::{
1919
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
@@ -1606,6 +1606,15 @@ fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) {
16061606
}
16071607

16081608
fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
1609+
fn write_size_of_layout(w: &mut Buffer, layout: &Layout) {
1610+
if layout.abi.is_unsized() {
1611+
write!(w, "(unsized)");
1612+
} else {
1613+
let bytes = layout.size.bytes();
1614+
write!(w, "{size} byte{pl}", size = bytes, pl = if bytes == 1 { "" } else { "s" },);
1615+
}
1616+
}
1617+
16091618
if !cx.shared.show_type_layout {
16101619
return;
16111620
}
@@ -1627,17 +1636,9 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
16271636
<a href=\"https://doc.rust-lang.org/reference/type-layout.html\">“Type Layout”</a> \
16281637
chapter for details on type layout guarantees.</p></div>"
16291638
);
1630-
if ty_layout.layout.abi.is_unsized() {
1631-
writeln!(w, "<p><strong>Size:</strong> (unsized)</p>");
1632-
} else {
1633-
let bytes = ty_layout.layout.size.bytes();
1634-
writeln!(
1635-
w,
1636-
"<p><strong>Size:</strong> {size} byte{pl}</p>",
1637-
size = bytes,
1638-
pl = if bytes == 1 { "" } else { "s" },
1639-
);
1640-
}
1639+
w.write_str("<p><strong>Size:</strong> ");
1640+
write_size_of_layout(w, ty_layout.layout);
1641+
writeln!(w, "</p>");
16411642
if let Variants::Multiple { variants, .. } = &ty_layout.layout.variants {
16421643
if !variants.is_empty() {
16431644
w.write_str(
@@ -1649,23 +1650,14 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
16491650
let adt = if let Adt(adt, _) = ty_layout.ty.kind() {
16501651
adt
16511652
} else {
1652-
bug!("not an adt")
1653+
span_bug!(tcx.def_span(ty_def_id), "not an adt")
16531654
};
16541655

16551656
for (index, layout) in variants.iter_enumerated() {
16561657
let ident = adt.variants[index].ident;
1657-
if layout.abi.is_unsized() {
1658-
writeln!(w, "<li><code>{name}</code> (unsized)</li>", name = ident);
1659-
} else {
1660-
let bytes = layout.size.bytes();
1661-
writeln!(
1662-
w,
1663-
"<li><code>{name}</code>: {size} byte{pl}</li>",
1664-
name = ident,
1665-
size = bytes,
1666-
pl = if bytes == 1 { "" } else { "s" },
1667-
);
1668-
}
1658+
write!(w, "<li><code>{name}</code> ", name = ident);
1659+
write_size_of_layout(w, layout);
1660+
writeln!(w, "</li>");
16691661
}
16701662
w.write_str("</ul></p>");
16711663
}

0 commit comments

Comments
 (0)