Fold collections debug impls

Also convert [T]'s Debug impl. The behavior of the alternate flag here's
changing.
This commit is contained in:
Steven Fackler
2015-03-28 11:24:26 -07:00
parent 4037f2a368
commit b82bcec7ce
6 changed files with 6 additions and 41 deletions

View File

@@ -904,11 +904,7 @@ 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 {
let mut builder = f.debug_map(); self.iter().fold(f.debug_map(), |b, (k, v)| b.entry(k, v)).finish()
for (k, v) in self {
builder = builder.entry(k, v);
}
builder.finish()
} }
} }

View File

@@ -628,11 +628,7 @@ 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 {
let mut builder = f.debug_set(); self.iter().fold(f.debug_set(), |b, e| b.entry(e)).finish()
for x in self {
builder = builder.entry(x);
}
builder.finish()
} }
} }

View File

@@ -927,11 +927,7 @@ 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 {
let mut builder = f.debug_list(); self.iter().fold(f.debug_list(), |b, e| b.entry(e)).finish()
for e in self {
builder = builder.entry(e);
}
builder.finish()
} }
} }

View File

@@ -1017,22 +1017,7 @@ impl<'a> Debug for &'a (any::Any+'a) {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: Debug> Debug for [T] { impl<T: Debug> Debug for [T] {
fn fmt(&self, f: &mut Formatter) -> Result { fn fmt(&self, f: &mut Formatter) -> Result {
if f.flags & (1 << (FlagV1::Alternate as u32)) == 0 { self.iter().fold(f.debug_list(), |b, e| b.entry(e)).finish()
try!(write!(f, "["));
}
let mut is_first = true;
for x in self {
if is_first {
is_first = false;
} else {
try!(write!(f, ", "));
}
try!(write!(f, "{:?}", *x))
}
if f.flags & (1 << (FlagV1::Alternate as u32)) == 0 {
try!(write!(f, "]"));
}
Ok(())
} }
} }

View File

@@ -1226,11 +1226,7 @@ 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 {
let mut builder = f.debug_map(); self.iter().fold(f.debug_map(), |b, (k, v)| b.entry(k, v)).finish()
for (k, v) in self.iter() {
builder = builder.entry(k, v);
}
builder.finish()
} }
} }

View File

@@ -614,11 +614,7 @@ 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 {
let mut builder = f.debug_set(); self.iter().fold(f.debug_set(), |b, e| b.entry(e)).finish()
for x in self {
builder = builder.entry(x);
}
builder.finish()
} }
} }