Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include trailing comma in multiline Debug representation #59076

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 32 additions & 58 deletions src/libcore/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl<'a> PadAdapter<'a> {
fmt.wrap_buf(move |buf| {
*slot = Some(PadAdapter {
buf,
on_newline: false,
on_newline: true,
});
slot.as_mut().unwrap()
})
Expand Down Expand Up @@ -128,22 +128,21 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn field(&mut self, name: &str, value: &dyn fmt::Debug) -> &mut DebugStruct<'a, 'b> {
self.result = self.result.and_then(|_| {
let prefix = if self.has_fields {
","
} else {
" {"
};

if self.is_pretty() {
if !self.has_fields {
self.fmt.write_str(" {\n")?;
}
let mut slot = None;
let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
writer.write_str(prefix)?;
writer.write_str("\n")?;
writer.write_str(name)?;
writer.write_str(": ")?;
value.fmt(&mut writer)
value.fmt(&mut writer)?;
writer.write_str(",\n")
} else {
write!(self.fmt, "{} {}: ", prefix, name)?;
let prefix = if self.has_fields { ", " } else { " { " };
self.fmt.write_str(prefix)?;
self.fmt.write_str(name)?;
self.fmt.write_str(": ")?;
value.fmt(self.fmt)
}
});
Expand Down Expand Up @@ -184,7 +183,7 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
if self.has_fields {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
self.fmt.write_str("\n}")
self.fmt.write_str("}")
} else {
self.fmt.write_str(" }")
}
Expand Down Expand Up @@ -275,21 +274,17 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn field(&mut self, value: &dyn fmt::Debug) -> &mut DebugTuple<'a, 'b> {
self.result = self.result.and_then(|_| {
let (prefix, space) = if self.fields > 0 {
(",", " ")
} else {
("(", "")
};

if self.is_pretty() {
if self.fields == 0 {
self.fmt.write_str("(\n")?;
}
let mut slot = None;
let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
writer.write_str(prefix)?;
writer.write_str("\n")?;
value.fmt(&mut writer)
value.fmt(&mut writer)?;
writer.write_str(",\n")
} else {
let prefix = if self.fields == 0 { "(" } else { ", " };
self.fmt.write_str(prefix)?;
self.fmt.write_str(space)?;
value.fmt(self.fmt)
}
});
Expand Down Expand Up @@ -326,10 +321,7 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
pub fn finish(&mut self) -> fmt::Result {
if self.fields > 0 {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
self.fmt.write_str("\n")?;
}
if self.fields == 1 && self.empty_name {
if self.fields == 1 && self.empty_name && !self.is_pretty() {
self.fmt.write_str(",")?;
}
self.fmt.write_str(")")
Expand All @@ -353,14 +345,13 @@ impl<'a, 'b: 'a> DebugInner<'a, 'b> {
fn entry(&mut self, entry: &dyn fmt::Debug) {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
if !self.has_fields {
self.fmt.write_str("\n")?;
}
let mut slot = None;
let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
writer.write_str(if self.has_fields {
",\n"
} else {
"\n"
})?;
entry.fmt(&mut writer)
entry.fmt(&mut writer)?;
writer.write_str(",\n")
} else {
if self.has_fields {
self.fmt.write_str(", ")?
Expand All @@ -372,15 +363,6 @@ impl<'a, 'b: 'a> DebugInner<'a, 'b> {
self.has_fields = true;
}

pub fn finish(&mut self) {
let prefix = if self.is_pretty() && self.has_fields {
"\n"
} else {
""
};
self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
}

fn is_pretty(&self) -> bool {
self.fmt.alternate()
}
Expand Down Expand Up @@ -421,7 +403,7 @@ pub struct DebugSet<'a, 'b: 'a> {
}

pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
let result = write!(fmt, "{{");
let result = fmt.write_str("{");
DebugSet {
inner: DebugInner {
fmt,
Expand Down Expand Up @@ -519,7 +501,6 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> {
/// ```
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn finish(&mut self) -> fmt::Result {
self.inner.finish();
self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
}
}
Expand Down Expand Up @@ -559,7 +540,7 @@ pub struct DebugList<'a, 'b: 'a> {
}

pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
let result = write!(fmt, "[");
let result = fmt.write_str("[");
DebugList {
inner: DebugInner {
fmt,
Expand Down Expand Up @@ -657,7 +638,6 @@ impl<'a, 'b: 'a> DebugList<'a, 'b> {
/// ```
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn finish(&mut self) -> fmt::Result {
self.inner.finish();
self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
}
}
Expand Down Expand Up @@ -699,7 +679,7 @@ pub struct DebugMap<'a, 'b: 'a> {
}

pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
let result = write!(fmt, "{{");
let result = fmt.write_str("{");
DebugMap {
fmt,
result,
Expand Down Expand Up @@ -734,16 +714,15 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
pub fn entry(&mut self, key: &dyn fmt::Debug, value: &dyn fmt::Debug) -> &mut DebugMap<'a, 'b> {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
if !self.has_fields {
self.fmt.write_str("\n")?;
}
let mut slot = None;
let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
writer.write_str(if self.has_fields {
",\n"
} else {
"\n"
})?;
key.fmt(&mut writer)?;
writer.write_str(": ")?;
value.fmt(&mut writer)
value.fmt(&mut writer)?;
writer.write_str(",\n")
} else {
if self.has_fields {
self.fmt.write_str(", ")?
Expand Down Expand Up @@ -818,12 +797,7 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
/// ```
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn finish(&mut self) -> fmt::Result {
let prefix = if self.is_pretty() && self.has_fields {
"\n"
} else {
""
};
self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
self.result.and_then(|_| self.fmt.write_str("}"))
}

fn is_pretty(&self) -> bool {
Expand Down
52 changes: 26 additions & 26 deletions src/libcore/tests/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod debug_struct {
assert_eq!("Foo { bar: true }", format!("{:?}", Foo));
assert_eq!(
"Foo {
bar: true
bar: true,
}",
format!("{:#?}", Foo));
}
Expand All @@ -52,7 +52,7 @@ mod debug_struct {
assert_eq!(
"Foo {
bar: true,
baz: 10/20
baz: 10/20,
}",
format!("{:#?}", Foo));
}
Expand Down Expand Up @@ -87,9 +87,9 @@ mod debug_struct {
"Bar {
foo: Foo {
bar: true,
baz: 10/20
baz: 10/20,
},
hello: \"world\"
hello: \"world\",
}",
format!("{:#?}", Bar));
}
Expand Down Expand Up @@ -127,7 +127,7 @@ mod debug_tuple {
assert_eq!("Foo(true)", format!("{:?}", Foo));
assert_eq!(
"Foo(
true
true,
)",
format!("{:#?}", Foo));
}
Expand All @@ -149,7 +149,7 @@ mod debug_tuple {
assert_eq!(
"Foo(
true,
10/20
10/20,
)",
format!("{:#?}", Foo));
}
Expand Down Expand Up @@ -184,9 +184,9 @@ mod debug_tuple {
"Bar(
Foo(
true,
10/20
10/20,
),
\"world\"
\"world\",
)",
format!("{:#?}", Bar));
}
Expand Down Expand Up @@ -224,7 +224,7 @@ mod debug_map {
assert_eq!("{\"bar\": true}", format!("{:?}", Foo));
assert_eq!(
"{
\"bar\": true
\"bar\": true,
}",
format!("{:#?}", Foo));
}
Expand All @@ -246,7 +246,7 @@ mod debug_map {
assert_eq!(
"{
\"bar\": true,
10: 10/20
10: 10/20,
}",
format!("{:#?}", Foo));
}
Expand Down Expand Up @@ -282,12 +282,12 @@ mod debug_map {
"{
\"foo\": {
\"bar\": true,
10: 10/20
10: 10/20,
},
{
\"bar\": true,
10: 10/20
}: \"world\"
10: 10/20,
}: \"world\",
}",
format!("{:#?}", Bar));
}
Expand Down Expand Up @@ -325,7 +325,7 @@ mod debug_set {
assert_eq!("{true}", format!("{:?}", Foo));
assert_eq!(
"{
true
true,
}",
format!("{:#?}", Foo));
}
Expand All @@ -347,7 +347,7 @@ mod debug_set {
assert_eq!(
"{
true,
10/20
10/20,
}",
format!("{:#?}", Foo));
}
Expand Down Expand Up @@ -382,9 +382,9 @@ mod debug_set {
"{
{
true,
10/20
10/20,
},
\"world\"
\"world\",
}",
format!("{:#?}", Bar));
}
Expand Down Expand Up @@ -422,7 +422,7 @@ mod debug_list {
assert_eq!("[true]", format!("{:?}", Foo));
assert_eq!(
"[
true
true,
]",
format!("{:#?}", Foo));
}
Expand All @@ -444,7 +444,7 @@ mod debug_list {
assert_eq!(
"[
true,
10/20
10/20,
]",
format!("{:#?}", Foo));
}
Expand Down Expand Up @@ -479,9 +479,9 @@ mod debug_list {
"[
[
true,
10/20
10/20,
],
\"world\"
\"world\",
]",
format!("{:#?}", Bar));
}
Expand Down Expand Up @@ -513,31 +513,31 @@ fn test_formatting_parameters_are_forwarded() {
assert_eq!(format!("{:#03?}", struct_), "
Foo {
bar: 1024,
baz: 007
baz: 007,
}
".trim());
assert_eq!(format!("{:#03?}", tuple), "
(
1024,
007
007,
)
".trim());
assert_eq!(format!("{:#03?}", list), "
[
1024,
007
007,
]
".trim());
assert_eq!(format!("{:#03?}", map), r#"
{
"bar": 1024,
"baz": 007
"baz": 007,
}
"#.trim());
assert_eq!(format!("{:#03?}", set), "
{
007,
1024
1024,
}
".trim());
}
Loading