Skip to content

Commit

Permalink
Rollup merge of #84390 - m-ou-se:make-debug-non-exhaustive-without-fi…
Browse files Browse the repository at this point in the history
…elds-a-little-bit-less-verbose, r=kennytm

Format `Struct { .. }` on one line even with `{:#?}`.

The result of `debug_struct("A").finish_non_exhaustive()` before this change:
```
A {
    ..
}
```
And after this change:
```
A { .. }
```

If there's any fields, the result stays unchanged:
```
A {
    field: value,
    ..
}
  • Loading branch information
m-ou-se authored Apr 21, 2021
2 parents 2cddda3 + 82dc73b commit 49a5c80
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
27 changes: 9 additions & 18 deletions library/core/src/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,28 +188,19 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
#[stable(feature = "debug_non_exhaustive", since = "1.53.0")]
pub fn finish_non_exhaustive(&mut self) -> fmt::Result {
self.result = self.result.and_then(|_| {
// Draw non-exhaustive dots (`..`), and open brace if necessary (no fields).
if self.is_pretty() {
if !self.has_fields {
self.fmt.write_str(" {\n")?;
}
let mut slot = None;
let mut state = Default::default();
let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot, &mut state);
writer.write_str("..\n")?;
} else {
if self.has_fields {
self.fmt.write_str(", ..")?;
if self.has_fields {
if self.is_pretty() {
let mut slot = None;
let mut state = Default::default();
let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot, &mut state);
writer.write_str("..\n")?;
self.fmt.write_str("}")
} else {
self.fmt.write_str(" { ..")?;
self.fmt.write_str(", .. }")
}
}
if self.is_pretty() {
self.fmt.write_str("}")?
} else {
self.fmt.write_str(" }")?;
self.fmt.write_str(" { .. }")
}
Ok(())
});
self.result
}
Expand Down
7 changes: 1 addition & 6 deletions library/core/tests/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@ mod debug_struct {
}

assert_eq!("Foo { .. }", format!("{:?}", Foo));
assert_eq!(
"Foo {
..
}",
format!("{:#?}", Foo)
);
assert_eq!("Foo { .. }", format!("{:#?}", Foo));
}

#[test]
Expand Down

0 comments on commit 49a5c80

Please sign in to comment.