Skip to content

Commit 97b561a

Browse files
authored
Auto merge of #35667 - ollie27:rustdoc_opaque_structs, r=steveklabnik
rustdoc: Don't add extra newlines for fully opaque structs Changes the definition for braced structs with only private or hidden fields to save space on the page. Before: ``` pub struct Vec<T> { // some fields omitted } ``` After: ``` pub struct Vec<T> { /* fields omitted */ } ``` This also cleans up empty braced structs. Before: ``` pub struct Foo { } ``` After: ``` pub struct Foo {} ``` [before](https://doc.rust-lang.org/nightly/std/vec/struct.Vec.html) [after](https://ollie27.github.io/rust_doc_test/std/vec/struct.Vec.html) cc #34713
2 parents 739d571 + 8154a6b commit 97b561a

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/librustdoc/html/render.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -2521,19 +2521,28 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
25212521
if let Some(g) = g {
25222522
write!(w, "{}", WhereClause(g))?
25232523
}
2524-
write!(w, " {{\n{}", tab)?;
2524+
let mut has_visible_fields = false;
2525+
write!(w, " {{")?;
25252526
for field in fields {
25262527
if let clean::StructFieldItem(ref ty) = field.inner {
2527-
write!(w, " {}{}: {},\n{}",
2528+
write!(w, "\n{} {}{}: {},",
2529+
tab,
25282530
VisSpace(&field.visibility),
25292531
field.name.as_ref().unwrap(),
2530-
*ty,
2531-
tab)?;
2532+
*ty)?;
2533+
has_visible_fields = true;
25322534
}
25332535
}
25342536

2535-
if it.has_stripped_fields().unwrap() {
2536-
write!(w, " // some fields omitted\n{}", tab)?;
2537+
if has_visible_fields {
2538+
if it.has_stripped_fields().unwrap() {
2539+
write!(w, "\n{} // some fields omitted", tab)?;
2540+
}
2541+
write!(w, "\n{}", tab)?;
2542+
} else if it.has_stripped_fields().unwrap() {
2543+
// If there are no visible fields we can just display
2544+
// `{ /* fields omitted */ }` to save space.
2545+
write!(w, " /* fields omitted */ ")?;
25372546
}
25382547
write!(w, "}}")?;
25392548
}

src/test/rustdoc/structfields.rs

+10
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,13 @@ pub enum Qux {
4848
// @has - //pre "// some fields omitted"
4949
},
5050
}
51+
52+
// @has structfields/struct.Baz.html //pre "pub struct Baz { /* fields omitted */ }"
53+
pub struct Baz {
54+
x: u8,
55+
#[doc(hidden)]
56+
pub y: u8,
57+
}
58+
59+
// @has structfields/struct.Quux.html //pre "pub struct Quux {}"
60+
pub struct Quux {}

0 commit comments

Comments
 (0)