Rollup merge of #84390 - m-ou-se:make-debug-non-exhaustive-without-fields-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, .. }
This commit is contained in:
commit
49a5c80a3b
@ -188,28 +188,19 @@ pub fn field(&mut self, name: &str, value: &dyn fmt::Debug) -> &mut Self {
|
||||
#[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
|
||||
}
|
||||
|
@ -105,12 +105,7 @@ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
}
|
||||
|
||||
assert_eq!("Foo { .. }", format!("{:?}", Foo));
|
||||
assert_eq!(
|
||||
"Foo {
|
||||
..
|
||||
}",
|
||||
format!("{:#?}", Foo)
|
||||
);
|
||||
assert_eq!("Foo { .. }", format!("{:#?}", Foo));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user