has_fields: bool,
}
-pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str)
+pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
+ name: &str)
-> DebugStruct<'a, 'b> {
let result = fmt.write_str(name);
DebugStruct {
if self.is_pretty() {
let mut writer = PadAdapter::new(self.fmt);
- fmt::write(&mut writer, format_args!("{}\n{}: {:#?}", prefix, name, value))
+ fmt::write(&mut writer,
+ format_args!("{}\n{}: {:#?}", prefix, name, value))
} else {
write!(self.fmt, "{} {}: {:?}", prefix, name, value)
}
pub struct DebugTuple<'a, 'b: 'a> {
fmt: &'a mut fmt::Formatter<'b>,
result: fmt::Result,
- has_fields: bool,
+ fields: usize,
+ empty_name: bool,
}
pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
DebugTuple {
fmt: fmt,
result: result,
- has_fields: false,
+ fields: 0,
+ empty_name: name.is_empty(),
}
}
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
self.result = self.result.and_then(|_| {
- let (prefix, space) = if self.has_fields {
+ let (prefix, space) = if self.fields > 0 {
(",", " ")
} else {
("(", "")
}
});
- self.has_fields = true;
+ self.fields += 1;
self
}
/// Finishes output and returns any error encountered.
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn finish(&mut self) -> fmt::Result {
- if self.has_fields {
+ if self.fields > 0 {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
- self.fmt.write_str("\n)")
- } else {
- self.fmt.write_str(")")
+ try!(self.fmt.write_str("\n"));
+ }
+ if self.fields == 1 && self.empty_name {
+ try!(self.fmt.write_str(","));
}
+ self.fmt.write_str(")")
});
}
self.result
fn is_pretty(&self) -> bool {
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
}
-
- /// Returns the wrapped `Formatter`.
- #[unstable(feature = "debug_builder_formatter", reason = "recently added")]
- pub fn formatter(&mut self) -> &mut fmt::Formatter<'b> {
- &mut self.fmt
- }
}
struct DebugInner<'a, 'b: 'a> {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
let mut writer = PadAdapter::new(self.fmt);
- let prefix = if self.has_fields { "," } else { "" };
+ let prefix = if self.has_fields {
+ ","
+ } else {
+ ""
+ };
fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, entry))
} else {
- let prefix = if self.has_fields { ", " } else { "" };
+ let prefix = if self.has_fields {
+ ", "
+ } else {
+ ""
+ };
write!(self.fmt, "{}{:?}", prefix, entry)
}
});
}
pub fn finish(&mut self) {
- let prefix = if self.is_pretty() && self.has_fields { "\n" } else { "" };
+ let prefix = if self.is_pretty() && self.has_fields {
+ "\n"
+ } else {
+ ""
+ };
self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
}
fmt: fmt,
result: result,
has_fields: false,
- }
+ },
}
}
/// Adds the contents of an iterator of entries to the set output.
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
- where D: fmt::Debug, I: IntoIterator<Item=D> {
+ where D: fmt::Debug,
+ I: IntoIterator<Item = D>
+ {
for entry in entries {
self.entry(&entry);
}
fmt: fmt,
result: result,
has_fields: false,
- }
+ },
}
}
/// Adds the contents of an iterator of entries to the list output.
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
- where D: fmt::Debug, I: IntoIterator<Item=D> {
+ where D: fmt::Debug,
+ I: IntoIterator<Item = D>
+ {
for entry in entries {
self.entry(&entry);
}
self.result = self.result.and_then(|_| {
if self.is_pretty() {
let mut writer = PadAdapter::new(self.fmt);
- let prefix = if self.has_fields { "," } else { "" };
- fmt::write(&mut writer, format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
+ let prefix = if self.has_fields {
+ ","
+ } else {
+ ""
+ };
+ fmt::write(&mut writer,
+ format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
} else {
- let prefix = if self.has_fields { ", " } else { "" };
+ let prefix = if self.has_fields {
+ ", "
+ } else {
+ ""
+ };
write!(self.fmt, "{}{:?}: {:?}", prefix, key, value)
}
});
/// Adds the contents of an iterator of entries to the map output.
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
- where K: fmt::Debug, V: fmt::Debug, I: IntoIterator<Item=(K, V)> {
+ where K: fmt::Debug,
+ V: fmt::Debug,
+ I: IntoIterator<Item = (K, V)>
+ {
for (k, v) in entries {
self.entry(&k, &v);
}
/// Finishes output and returns any error encountered.
#[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 { "" };
+ let prefix = if self.is_pretty() && self.has_fields {
+ "\n"
+ } else {
+ ""
+ };
self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
}