Update debug helpers and add list builder
The collections debug helpers no longer prefix output with the collection name, in line with the current conventions for Debug implementations. Implementations that want to preserve the current behavior can simply add a `try!(write!(fmt, "TypeName "));` at the beginning of the `fmt` method. [breaking-change]
This commit is contained in:
parent
3e7385aae9
commit
4037f2a368
10 changed files with 277 additions and 131 deletions
|
@ -904,14 +904,11 @@ impl<K: Ord, V: Ord> Ord for BTreeMap<K, V> {
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
|
impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
try!(write!(f, "{{"));
|
let mut builder = f.debug_map();
|
||||||
|
for (k, v) in self {
|
||||||
for (i, (k, v)) in self.iter().enumerate() {
|
builder = builder.entry(k, v);
|
||||||
if i != 0 { try!(write!(f, ", ")); }
|
|
||||||
try!(write!(f, "{:?}: {:?}", *k, *v));
|
|
||||||
}
|
}
|
||||||
|
builder.finish()
|
||||||
write!(f, "}}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -628,14 +628,11 @@ impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<T: Debug> Debug for BTreeSet<T> {
|
impl<T: Debug> Debug for BTreeSet<T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
try!(write!(f, "{{"));
|
let mut builder = f.debug_set();
|
||||||
|
for x in self {
|
||||||
for (i, x) in self.iter().enumerate() {
|
builder = builder.entry(x);
|
||||||
if i != 0 { try!(write!(f, ", ")); }
|
|
||||||
try!(write!(f, "{:?}", *x));
|
|
||||||
}
|
}
|
||||||
|
builder.finish()
|
||||||
write!(f, "}}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@
|
||||||
#![feature(str_char)]
|
#![feature(str_char)]
|
||||||
#![feature(convert)]
|
#![feature(convert)]
|
||||||
#![feature(slice_patterns)]
|
#![feature(slice_patterns)]
|
||||||
|
#![feature(debug_builders)]
|
||||||
#![cfg_attr(test, feature(rand, rustc_private, test, hash, collections))]
|
#![cfg_attr(test, feature(rand, rustc_private, test, hash, collections))]
|
||||||
#![cfg_attr(test, allow(deprecated))] // rand
|
#![cfg_attr(test, allow(deprecated))] // rand
|
||||||
|
|
||||||
|
|
|
@ -927,14 +927,11 @@ impl<A: Clone> Clone for LinkedList<A> {
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<A: fmt::Debug> fmt::Debug for LinkedList<A> {
|
impl<A: fmt::Debug> fmt::Debug for LinkedList<A> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
try!(write!(f, "["));
|
let mut builder = f.debug_list();
|
||||||
|
for e in self {
|
||||||
for (i, e) in self.iter().enumerate() {
|
builder = builder.entry(e);
|
||||||
if i != 0 { try!(write!(f, ", ")); }
|
|
||||||
try!(write!(f, "{:?}", *e));
|
|
||||||
}
|
}
|
||||||
|
builder.finish()
|
||||||
write!(f, "]")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -177,22 +177,54 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A struct to help with `fmt::Debug` implementations.
|
struct DebugInner<'a, 'b: 'a> {
|
||||||
///
|
|
||||||
/// Constructed by the `Formatter::debug_set` method.
|
|
||||||
#[must_use]
|
|
||||||
pub struct DebugSet<'a, 'b: 'a> {
|
|
||||||
fmt: &'a mut fmt::Formatter<'b>,
|
fmt: &'a mut fmt::Formatter<'b>,
|
||||||
result: fmt::Result,
|
result: fmt::Result,
|
||||||
has_fields: bool,
|
has_fields: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugSet<'a, 'b> {
|
impl<'a, 'b: 'a> DebugInner<'a, 'b> {
|
||||||
let result = write!(fmt, "{} {{", name);
|
fn entry(&mut self, entry: &fmt::Debug) {
|
||||||
|
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, entry))
|
||||||
|
} else {
|
||||||
|
let prefix = if self.has_fields { ", " } else { "" };
|
||||||
|
write!(self.fmt, "{}{:?}", prefix, entry)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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.flags() & (1 << (FlagV1::Alternate as usize)) != 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A struct to help with `fmt::Debug` implementations.
|
||||||
|
///
|
||||||
|
/// Constructed by the `Formatter::debug_set` method.
|
||||||
|
#[must_use]
|
||||||
|
pub struct DebugSet<'a, 'b: 'a> {
|
||||||
|
inner: DebugInner<'a, 'b>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
|
||||||
|
let result = write!(fmt, "{{");
|
||||||
DebugSet {
|
DebugSet {
|
||||||
fmt: fmt,
|
inner: DebugInner {
|
||||||
result: result,
|
fmt: fmt,
|
||||||
has_fields: false,
|
result: result,
|
||||||
|
has_fields: false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,41 +232,52 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> {
|
||||||
/// Adds a new entry to the set output.
|
/// Adds a new entry to the set output.
|
||||||
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
pub fn entry(mut self, entry: &fmt::Debug) -> DebugSet<'a, 'b> {
|
pub fn entry(mut self, entry: &fmt::Debug) -> DebugSet<'a, 'b> {
|
||||||
self.result = self.result.and_then(|_| {
|
self.inner.entry(entry);
|
||||||
let prefix = if self.has_fields {
|
|
||||||
","
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
};
|
|
||||||
|
|
||||||
if self.is_pretty() {
|
|
||||||
let mut writer = PadAdapter::new(self.fmt);
|
|
||||||
fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, entry))
|
|
||||||
} else {
|
|
||||||
write!(self.fmt, "{} {:?}", prefix, entry)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
self.has_fields = true;
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consumes the `DebugSet`, finishing output and returning any error
|
/// Consumes the `DebugSet`, finishing output and returning any error
|
||||||
/// encountered.
|
/// encountered.
|
||||||
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
pub fn finish(self) -> fmt::Result {
|
pub fn finish(mut self) -> fmt::Result {
|
||||||
self.result.and_then(|_| {
|
self.inner.finish();
|
||||||
let end = match (self.has_fields, self.is_pretty()) {
|
self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
|
||||||
(false, _) => "}",
|
}
|
||||||
(true, false) => " }",
|
}
|
||||||
(true, true) => "\n}",
|
|
||||||
};
|
/// A struct to help with `fmt::Debug` implementations.
|
||||||
self.fmt.write_str(end)
|
///
|
||||||
})
|
/// Constructed by the `Formatter::debug_list` method.
|
||||||
|
#[must_use]
|
||||||
|
pub struct DebugList<'a, 'b: 'a> {
|
||||||
|
inner: DebugInner<'a, 'b>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
|
||||||
|
let result = write!(fmt, "[");
|
||||||
|
DebugList {
|
||||||
|
inner: DebugInner {
|
||||||
|
fmt: fmt,
|
||||||
|
result: result,
|
||||||
|
has_fields: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b: 'a> DebugList<'a, 'b> {
|
||||||
|
/// Adds a new entry to the set output.
|
||||||
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
|
pub fn entry(mut self, entry: &fmt::Debug) -> DebugList<'a, 'b> {
|
||||||
|
self.inner.entry(entry);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_pretty(&self) -> bool {
|
/// Consumes the `DebugSet`, finishing output and returning any error
|
||||||
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
|
/// encountered.
|
||||||
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
|
pub fn finish(mut self) -> fmt::Result {
|
||||||
|
self.inner.finish();
|
||||||
|
self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,8 +291,8 @@ pub struct DebugMap<'a, 'b: 'a> {
|
||||||
has_fields: bool,
|
has_fields: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugMap<'a, 'b> {
|
pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
|
||||||
let result = write!(fmt, "{} {{", name);
|
let result = write!(fmt, "{{");
|
||||||
DebugMap {
|
DebugMap {
|
||||||
fmt: fmt,
|
fmt: fmt,
|
||||||
result: result,
|
result: result,
|
||||||
|
@ -262,22 +305,17 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
|
||||||
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
pub fn entry(mut self, key: &fmt::Debug, value: &fmt::Debug) -> DebugMap<'a, 'b> {
|
pub fn entry(mut self, key: &fmt::Debug, value: &fmt::Debug) -> DebugMap<'a, 'b> {
|
||||||
self.result = self.result.and_then(|_| {
|
self.result = self.result.and_then(|_| {
|
||||||
let prefix = if self.has_fields {
|
|
||||||
","
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
};
|
|
||||||
|
|
||||||
if self.is_pretty() {
|
if self.is_pretty() {
|
||||||
let mut writer = PadAdapter::new(self.fmt);
|
let mut writer = PadAdapter::new(self.fmt);
|
||||||
|
let prefix = if self.has_fields { "," } else { "" };
|
||||||
fmt::write(&mut writer, format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
|
fmt::write(&mut writer, format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
|
||||||
} else {
|
} else {
|
||||||
write!(self.fmt, "{} {:?}: {:?}", prefix, key, value)
|
let prefix = if self.has_fields { ", " } else { "" };
|
||||||
|
write!(self.fmt, "{}{:?}: {:?}", prefix, key, value)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
self.has_fields = true;
|
self.has_fields = true;
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,14 +323,8 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
|
||||||
/// encountered.
|
/// encountered.
|
||||||
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
pub fn finish(self) -> fmt::Result {
|
pub fn finish(self) -> fmt::Result {
|
||||||
self.result.and_then(|_| {
|
let prefix = if self.is_pretty() && self.has_fields { "\n" } else { "" };
|
||||||
let end = match (self.has_fields, self.is_pretty()) {
|
self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
|
||||||
(false, _) => "}",
|
|
||||||
(true, false) => " }",
|
|
||||||
(true, true) => "\n}",
|
|
||||||
};
|
|
||||||
self.fmt.write_str(end)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_pretty(&self) -> bool {
|
fn is_pretty(&self) -> bool {
|
||||||
|
|
|
@ -32,7 +32,7 @@ pub use self::num::radix;
|
||||||
pub use self::num::Radix;
|
pub use self::num::Radix;
|
||||||
pub use self::num::RadixFmt;
|
pub use self::num::RadixFmt;
|
||||||
|
|
||||||
pub use self::builders::{DebugStruct, DebugTuple, DebugSet, DebugMap};
|
pub use self::builders::{DebugStruct, DebugTuple, DebugSet, DebugList, DebugMap};
|
||||||
|
|
||||||
mod num;
|
mod num;
|
||||||
mod float;
|
mod float;
|
||||||
|
@ -644,7 +644,7 @@ impl<'a> Formatter<'a> {
|
||||||
/// // prints "Foo { bar: 10, baz: "Hello World" }"
|
/// // prints "Foo { bar: 10, baz: "Hello World" }"
|
||||||
/// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
|
/// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "core", reason = "method was just created")]
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn debug_struct<'b>(&'b mut self, name: &str) -> DebugStruct<'b, 'a> {
|
pub fn debug_struct<'b>(&'b mut self, name: &str) -> DebugStruct<'b, 'a> {
|
||||||
builders::debug_struct_new(self, name)
|
builders::debug_struct_new(self, name)
|
||||||
|
@ -673,12 +673,42 @@ impl<'a> Formatter<'a> {
|
||||||
/// // prints "Foo(10, "Hello World")"
|
/// // prints "Foo(10, "Hello World")"
|
||||||
/// println!("{:?}", Foo(10, "Hello World".to_string()));
|
/// println!("{:?}", Foo(10, "Hello World".to_string()));
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "core", reason = "method was just created")]
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn debug_tuple<'b>(&'b mut self, name: &str) -> DebugTuple<'b, 'a> {
|
pub fn debug_tuple<'b>(&'b mut self, name: &str) -> DebugTuple<'b, 'a> {
|
||||||
builders::debug_tuple_new(self, name)
|
builders::debug_tuple_new(self, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a `DebugList` builder designed to assist with creation of
|
||||||
|
/// `fmt::Debug` implementations for list-like structures.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #![feature(debug_builders, core)]
|
||||||
|
/// use std::fmt;
|
||||||
|
///
|
||||||
|
/// struct Foo(Vec<i32>);
|
||||||
|
///
|
||||||
|
/// impl fmt::Debug for Foo {
|
||||||
|
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
/// let mut builder = fmt.debug_list();
|
||||||
|
/// for i in &self.0 {
|
||||||
|
/// builder = builder.entry(i);
|
||||||
|
/// }
|
||||||
|
/// builder.finish()
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// // prints "Foo { 10, 11 }"
|
||||||
|
/// println!("{:?}", Foo(vec![10, 11]));
|
||||||
|
/// ```
|
||||||
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
|
#[inline]
|
||||||
|
pub fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a> {
|
||||||
|
builders::debug_list_new(self)
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a `DebugSet` builder designed to assist with creation of
|
/// Creates a `DebugSet` builder designed to assist with creation of
|
||||||
/// `fmt::Debug` implementations for set-like structures.
|
/// `fmt::Debug` implementations for set-like structures.
|
||||||
///
|
///
|
||||||
|
@ -692,7 +722,7 @@ impl<'a> Formatter<'a> {
|
||||||
///
|
///
|
||||||
/// impl fmt::Debug for Foo {
|
/// impl fmt::Debug for Foo {
|
||||||
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
/// let mut builder = fmt.debug_set("Foo");
|
/// let mut builder = fmt.debug_set();
|
||||||
/// for i in &self.0 {
|
/// for i in &self.0 {
|
||||||
/// builder = builder.entry(i);
|
/// builder = builder.entry(i);
|
||||||
/// }
|
/// }
|
||||||
|
@ -703,10 +733,10 @@ impl<'a> Formatter<'a> {
|
||||||
/// // prints "Foo { 10, 11 }"
|
/// // prints "Foo { 10, 11 }"
|
||||||
/// println!("{:?}", Foo(vec![10, 11]));
|
/// println!("{:?}", Foo(vec![10, 11]));
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "core", reason = "method was just created")]
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn debug_set<'b>(&'b mut self, name: &str) -> DebugSet<'b, 'a> {
|
pub fn debug_set<'b>(&'b mut self) -> DebugSet<'b, 'a> {
|
||||||
builders::debug_set_new(self, name)
|
builders::debug_set_new(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a `DebugMap` builder designed to assist with creation of
|
/// Creates a `DebugMap` builder designed to assist with creation of
|
||||||
|
@ -722,7 +752,7 @@ impl<'a> Formatter<'a> {
|
||||||
///
|
///
|
||||||
/// impl fmt::Debug for Foo {
|
/// impl fmt::Debug for Foo {
|
||||||
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
/// let mut builder = fmt.debug_map("Foo");
|
/// let mut builder = fmt.debug_map();
|
||||||
/// for &(ref key, ref value) in &self.0 {
|
/// for &(ref key, ref value) in &self.0 {
|
||||||
/// builder = builder.entry(key, value);
|
/// builder = builder.entry(key, value);
|
||||||
/// }
|
/// }
|
||||||
|
@ -733,10 +763,10 @@ impl<'a> Formatter<'a> {
|
||||||
/// // prints "Foo { "A": 10, "B": 11 }"
|
/// // prints "Foo { "A": 10, "B": 11 }"
|
||||||
/// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
|
/// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "core", reason = "method was just created")]
|
#[unstable(feature = "debug_builders", reason = "method was just created")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn debug_map<'b>(&'b mut self, name: &str) -> DebugMap<'b, 'a> {
|
pub fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a> {
|
||||||
builders::debug_map_new(self, name)
|
builders::debug_map_new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -211,12 +211,12 @@ mod debug_map {
|
||||||
|
|
||||||
impl fmt::Debug for Foo {
|
impl fmt::Debug for Foo {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_map("Foo").finish()
|
fmt.debug_map().finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!("Foo {}", format!("{:?}", Foo));
|
assert_eq!("{}", format!("{:?}", Foo));
|
||||||
assert_eq!("Foo {}", format!("{:#?}", Foo));
|
assert_eq!("{}", format!("{:#?}", Foo));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -225,15 +225,15 @@ mod debug_map {
|
||||||
|
|
||||||
impl fmt::Debug for Foo {
|
impl fmt::Debug for Foo {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_map("Foo")
|
fmt.debug_map()
|
||||||
.entry(&"bar", &true)
|
.entry(&"bar", &true)
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!("Foo { \"bar\": true }", format!("{:?}", Foo));
|
assert_eq!("{\"bar\": true}", format!("{:?}", Foo));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"Foo {
|
"{
|
||||||
\"bar\": true
|
\"bar\": true
|
||||||
}",
|
}",
|
||||||
format!("{:#?}", Foo));
|
format!("{:#?}", Foo));
|
||||||
|
@ -245,16 +245,16 @@ mod debug_map {
|
||||||
|
|
||||||
impl fmt::Debug for Foo {
|
impl fmt::Debug for Foo {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_map("Foo")
|
fmt.debug_map()
|
||||||
.entry(&"bar", &true)
|
.entry(&"bar", &true)
|
||||||
.entry(&10i32, &format_args!("{}/{}", 10i32, 20i32))
|
.entry(&10i32, &format_args!("{}/{}", 10i32, 20i32))
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!("Foo { \"bar\": true, 10: 10/20 }", format!("{:?}", Foo));
|
assert_eq!("{\"bar\": true, 10: 10/20}", format!("{:?}", Foo));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"Foo {
|
"{
|
||||||
\"bar\": true,
|
\"bar\": true,
|
||||||
10: 10/20
|
10: 10/20
|
||||||
}",
|
}",
|
||||||
|
@ -267,7 +267,7 @@ mod debug_map {
|
||||||
|
|
||||||
impl fmt::Debug for Foo {
|
impl fmt::Debug for Foo {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_map("Foo")
|
fmt.debug_map()
|
||||||
.entry(&"bar", &true)
|
.entry(&"bar", &true)
|
||||||
.entry(&10i32, &format_args!("{}/{}", 10i32, 20i32))
|
.entry(&10i32, &format_args!("{}/{}", 10i32, 20i32))
|
||||||
.finish()
|
.finish()
|
||||||
|
@ -278,23 +278,23 @@ mod debug_map {
|
||||||
|
|
||||||
impl fmt::Debug for Bar {
|
impl fmt::Debug for Bar {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_map("Bar")
|
fmt.debug_map()
|
||||||
.entry(&"foo", &Foo)
|
.entry(&"foo", &Foo)
|
||||||
.entry(&Foo, &"world")
|
.entry(&Foo, &"world")
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!("Bar { \"foo\": Foo { \"bar\": true, 10: 10/20 }, \
|
assert_eq!("{\"foo\": {\"bar\": true, 10: 10/20}, \
|
||||||
Foo { \"bar\": true, 10: 10/20 }: \"world\" }",
|
{\"bar\": true, 10: 10/20}: \"world\"}",
|
||||||
format!("{:?}", Bar));
|
format!("{:?}", Bar));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"Bar {
|
"{
|
||||||
\"foo\": Foo {
|
\"foo\": {
|
||||||
\"bar\": true,
|
\"bar\": true,
|
||||||
10: 10/20
|
10: 10/20
|
||||||
},
|
},
|
||||||
Foo {
|
{
|
||||||
\"bar\": true,
|
\"bar\": true,
|
||||||
10: 10/20
|
10: 10/20
|
||||||
}: \"world\"
|
}: \"world\"
|
||||||
|
@ -312,12 +312,12 @@ mod debug_set {
|
||||||
|
|
||||||
impl fmt::Debug for Foo {
|
impl fmt::Debug for Foo {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_set("Foo").finish()
|
fmt.debug_set().finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!("Foo {}", format!("{:?}", Foo));
|
assert_eq!("{}", format!("{:?}", Foo));
|
||||||
assert_eq!("Foo {}", format!("{:#?}", Foo));
|
assert_eq!("{}", format!("{:#?}", Foo));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -326,15 +326,15 @@ mod debug_set {
|
||||||
|
|
||||||
impl fmt::Debug for Foo {
|
impl fmt::Debug for Foo {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_set("Foo")
|
fmt.debug_set()
|
||||||
.entry(&true)
|
.entry(&true)
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!("Foo { true }", format!("{:?}", Foo));
|
assert_eq!("{true}", format!("{:?}", Foo));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"Foo {
|
"{
|
||||||
true
|
true
|
||||||
}",
|
}",
|
||||||
format!("{:#?}", Foo));
|
format!("{:#?}", Foo));
|
||||||
|
@ -346,16 +346,16 @@ mod debug_set {
|
||||||
|
|
||||||
impl fmt::Debug for Foo {
|
impl fmt::Debug for Foo {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_set("Foo")
|
fmt.debug_set()
|
||||||
.entry(&true)
|
.entry(&true)
|
||||||
.entry(&format_args!("{}/{}", 10i32, 20i32))
|
.entry(&format_args!("{}/{}", 10i32, 20i32))
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!("Foo { true, 10/20 }", format!("{:?}", Foo));
|
assert_eq!("{true, 10/20}", format!("{:?}", Foo));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"Foo {
|
"{
|
||||||
true,
|
true,
|
||||||
10/20
|
10/20
|
||||||
}",
|
}",
|
||||||
|
@ -368,7 +368,7 @@ mod debug_set {
|
||||||
|
|
||||||
impl fmt::Debug for Foo {
|
impl fmt::Debug for Foo {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_set("Foo")
|
fmt.debug_set()
|
||||||
.entry(&true)
|
.entry(&true)
|
||||||
.entry(&format_args!("{}/{}", 10i32, 20i32))
|
.entry(&format_args!("{}/{}", 10i32, 20i32))
|
||||||
.finish()
|
.finish()
|
||||||
|
@ -379,18 +379,18 @@ mod debug_set {
|
||||||
|
|
||||||
impl fmt::Debug for Bar {
|
impl fmt::Debug for Bar {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_set("Bar")
|
fmt.debug_set()
|
||||||
.entry(&Foo)
|
.entry(&Foo)
|
||||||
.entry(&"world")
|
.entry(&"world")
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!("Bar { Foo { true, 10/20 }, \"world\" }",
|
assert_eq!("{{true, 10/20}, \"world\"}",
|
||||||
format!("{:?}", Bar));
|
format!("{:?}", Bar));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"Bar {
|
"{
|
||||||
Foo {
|
{
|
||||||
true,
|
true,
|
||||||
10/20
|
10/20
|
||||||
},
|
},
|
||||||
|
@ -399,3 +399,100 @@ mod debug_set {
|
||||||
format!("{:#?}", Bar));
|
format!("{:#?}", Bar));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod debug_list {
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_empty() {
|
||||||
|
struct Foo;
|
||||||
|
|
||||||
|
impl fmt::Debug for Foo {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
fmt.debug_list().finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!("[]", format!("{:?}", Foo));
|
||||||
|
assert_eq!("[]", format!("{:#?}", Foo));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_single() {
|
||||||
|
struct Foo;
|
||||||
|
|
||||||
|
impl fmt::Debug for Foo {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
fmt.debug_list()
|
||||||
|
.entry(&true)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!("[true]", format!("{:?}", Foo));
|
||||||
|
assert_eq!(
|
||||||
|
"[
|
||||||
|
true
|
||||||
|
]",
|
||||||
|
format!("{:#?}", Foo));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_multiple() {
|
||||||
|
struct Foo;
|
||||||
|
|
||||||
|
impl fmt::Debug for Foo {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
fmt.debug_list()
|
||||||
|
.entry(&true)
|
||||||
|
.entry(&format_args!("{}/{}", 10i32, 20i32))
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!("[true, 10/20]", format!("{:?}", Foo));
|
||||||
|
assert_eq!(
|
||||||
|
"[
|
||||||
|
true,
|
||||||
|
10/20
|
||||||
|
]",
|
||||||
|
format!("{:#?}", Foo));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_nested() {
|
||||||
|
struct Foo;
|
||||||
|
|
||||||
|
impl fmt::Debug for Foo {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
fmt.debug_list()
|
||||||
|
.entry(&true)
|
||||||
|
.entry(&format_args!("{}/{}", 10i32, 20i32))
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Bar;
|
||||||
|
|
||||||
|
impl fmt::Debug for Bar {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
fmt.debug_list()
|
||||||
|
.entry(&Foo)
|
||||||
|
.entry(&"world")
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!("[[true, 10/20], \"world\"]",
|
||||||
|
format!("{:?}", Bar));
|
||||||
|
assert_eq!(
|
||||||
|
"[
|
||||||
|
[
|
||||||
|
true,
|
||||||
|
10/20
|
||||||
|
],
|
||||||
|
\"world\"
|
||||||
|
]",
|
||||||
|
format!("{:#?}", Bar));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1226,14 +1226,11 @@ impl<K, V, S> Debug for HashMap<K, V, S>
|
||||||
where K: Eq + Hash + Debug, V: Debug, S: HashState
|
where K: Eq + Hash + Debug, V: Debug, S: HashState
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
try!(write!(f, "{{"));
|
let mut builder = f.debug_map();
|
||||||
|
for (k, v) in self.iter() {
|
||||||
for (i, (k, v)) in self.iter().enumerate() {
|
builder = builder.entry(k, v);
|
||||||
if i != 0 { try!(write!(f, ", ")); }
|
|
||||||
try!(write!(f, "{:?}: {:?}", *k, *v));
|
|
||||||
}
|
}
|
||||||
|
builder.finish()
|
||||||
write!(f, "}}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -614,14 +614,11 @@ impl<T, S> fmt::Debug for HashSet<T, S>
|
||||||
S: HashState
|
S: HashState
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
try!(write!(f, "{{"));
|
let mut builder = f.debug_set();
|
||||||
|
for x in self {
|
||||||
for (i, x) in self.iter().enumerate() {
|
builder = builder.entry(x);
|
||||||
if i != 0 { try!(write!(f, ", ")); }
|
|
||||||
try!(write!(f, "{:?}", *x));
|
|
||||||
}
|
}
|
||||||
|
builder.finish()
|
||||||
write!(f, "}}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,6 +128,7 @@
|
||||||
#![feature(into_cow)]
|
#![feature(into_cow)]
|
||||||
#![feature(slice_patterns)]
|
#![feature(slice_patterns)]
|
||||||
#![feature(std_misc)]
|
#![feature(std_misc)]
|
||||||
|
#![feature(debug_builders)]
|
||||||
#![cfg_attr(test, feature(test, rustc_private, std_misc))]
|
#![cfg_attr(test, feature(test, rustc_private, std_misc))]
|
||||||
|
|
||||||
// Don't link to std. We are std.
|
// Don't link to std. We are std.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue