Skip to content

Make debug builders take &mut self, add entries method #25548

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

Merged
merged 3 commits into from
May 19, 2015
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
2 changes: 1 addition & 1 deletion src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ impl<K: Ord, V: Ord> Ord for BTreeMap<K, V> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.iter().fold(f.debug_map(), |b, (k, v)| b.entry(k, v)).finish()
f.debug_map().entries(self.iter()).finish()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Debug> Debug for BTreeSet<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.iter().fold(f.debug_set(), |b, e| b.entry(e)).finish()
f.debug_set().entries(self.iter()).finish()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ impl<A: Clone> Clone for LinkedList<A> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: fmt::Debug> fmt::Debug for LinkedList<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.iter().fold(f.debug_list(), |b, e| b.entry(e)).finish()
f.debug_list().entries(self.iter()).finish()
}
}

Expand Down
67 changes: 46 additions & 21 deletions src/libcore/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str)
impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
/// Adds a new field to the generated struct output.
#[unstable(feature = "debug_builders", reason = "method was just created")]
pub fn field(mut self, name: &str, value: &fmt::Debug) -> DebugStruct<'a, 'b> {
pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a, 'b> {
self.result = self.result.and_then(|_| {
let prefix = if self.has_fields {
","
Expand All @@ -93,10 +93,9 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
self
}

/// Consumes the `DebugStruct`, finishing output and returning any error
/// encountered.
/// Finishes output and returns any error encountered.
#[unstable(feature = "debug_builders", reason = "method was just created")]
pub fn finish(mut self) -> fmt::Result {
pub fn finish(&mut self) -> fmt::Result {
if self.has_fields {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
Expand Down Expand Up @@ -136,7 +135,7 @@ pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> D
impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
/// Adds a new field to the generated tuple struct output.
#[unstable(feature = "debug_builders", reason = "method was just created")]
pub fn field(mut self, value: &fmt::Debug) -> DebugTuple<'a, 'b> {
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 {
(",", " ")
Expand All @@ -156,10 +155,9 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
self
}

/// Consumes the `DebugTuple`, finishing output and returning any error
/// encountered.
/// Finishes output and returns any error encountered.
#[unstable(feature = "debug_builders", reason = "method was just created")]
pub fn finish(mut self) -> fmt::Result {
pub fn finish(&mut self) -> fmt::Result {
if self.has_fields {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
Expand Down Expand Up @@ -231,15 +229,24 @@ pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b
impl<'a, 'b: 'a> DebugSet<'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) -> DebugSet<'a, 'b> {
pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> {
self.inner.entry(entry);
self
}

/// Consumes the `DebugSet`, finishing output and returning any error
/// encountered.
/// Adds the contents of an iterator of entries to the set output.
#[unstable(feature = "debug_builders", reason = "method was just created")]
pub fn finish(mut self) -> fmt::Result {
pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
where D: fmt::Debug, I: IntoIterator<Item=D> {
for entry in entries {
self.entry(&entry);
}
self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably even be entries.into_iter().fold(self, |me, a| me.entry(a)), but that's getting somewhat complicated!

}

/// Finishes output and returns any error 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("}"))
}
Expand All @@ -265,17 +272,26 @@ pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a,
}

impl<'a, 'b: 'a> DebugList<'a, 'b> {
/// Adds a new entry to the set output.
/// Adds a new entry to the list output.
#[unstable(feature = "debug_builders", reason = "method was just created")]
pub fn entry(mut self, entry: &fmt::Debug) -> DebugList<'a, 'b> {
pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugList<'a, 'b> {
self.inner.entry(entry);
self
}

/// Consumes the `DebugSet`, finishing output and returning any error
/// encountered.
/// Adds the contents of an iterator of entries to the list output.
#[unstable(feature = "debug_builders", reason = "method was just created")]
pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
where D: fmt::Debug, I: IntoIterator<Item=D> {
for entry in entries {
self.entry(&entry);
}
self
}

/// Finishes output and returns any error encountered.
#[unstable(feature = "debug_builders", reason = "method was just created")]
pub fn finish(mut self) -> fmt::Result {
pub fn finish(&mut self) -> fmt::Result {
self.inner.finish();
self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
}
Expand Down Expand Up @@ -303,7 +319,7 @@ pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b
impl<'a, 'b: 'a> DebugMap<'a, 'b> {
/// Adds a new entry to the map output.
#[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) -> &mut DebugMap<'a, 'b> {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
let mut writer = PadAdapter::new(self.fmt);
Expand All @@ -319,10 +335,19 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
self
}

/// Consumes the `DebugMap`, finishing output and returning any error
/// encountered.
/// Adds the contents of an iterator of entries to the map output.
#[unstable(feature = "debug_builders", reason = "method was just created")]
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)> {
for (k, v) in entries {
self.entry(&k, &v);
}
self
}

/// Finishes output and returns any error encountered.
#[unstable(feature = "debug_builders", reason = "method was just created")]
pub fn finish(self) -> fmt::Result {
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))
}
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ impl<'a> Formatter<'a> {
///
/// impl fmt::Debug for Foo {
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
/// self.0.iter().fold(fmt.debug_list(), |b, e| b.entry(e)).finish()
/// fmt.debug_list().entries(self.0.iter()).finish()
/// }
/// }
///
Expand All @@ -813,7 +813,7 @@ impl<'a> Formatter<'a> {
///
/// impl fmt::Debug for Foo {
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
/// self.0.iter().fold(fmt.debug_set(), |b, e| b.entry(e)).finish()
/// fmt.debug_set().entries(self.0.iter()).finish()
/// }
/// }
///
Expand All @@ -839,7 +839,7 @@ impl<'a> Formatter<'a> {
///
/// impl fmt::Debug for Foo {
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
/// self.0.iter().fold(fmt.debug_map(), |b, &(ref k, ref v)| b.entry(k, v)).finish()
/// fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish()
/// }
/// }
///
Expand Down Expand Up @@ -1120,7 +1120,7 @@ tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Debug> Debug for [T] {
fn fmt(&self, f: &mut Formatter) -> Result {
self.iter().fold(f.debug_list(), |b, e| b.entry(e)).finish()
f.debug_list().entries(self.iter()).finish()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ impl<K, V, S> Debug for HashMap<K, V, S>
where K: Eq + Hash + Debug, V: Debug, S: HashState
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.iter().fold(f.debug_map(), |b, (k, v)| b.entry(k, v)).finish()
f.debug_map().entries(self.iter()).finish()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl<T, S> fmt::Debug for HashSet<T, S>
S: HashState
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.iter().fold(f.debug_set(), |b, e| b.entry(e)).finish()
f.debug_set().entries(self.iter()).finish()
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/libstd/sys/common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,16 @@ impl fmt::Debug for TcpStream {
let mut res = f.debug_struct("TcpStream");

if let Ok(addr) = self.socket_addr() {
res = res.field("addr", &addr);
res.field("addr", &addr);
}

if let Ok(peer) = self.peer_addr() {
res = res.field("peer", &peer);
res.field("peer", &peer);
}

let name = if cfg!(windows) {"socket"} else {"fd"};
res = res.field(name, &self.inner.as_inner());
res.finish()
res.field(name, &self.inner.as_inner())
.finish()
}
}

Expand Down Expand Up @@ -351,12 +351,12 @@ impl fmt::Debug for TcpListener {
let mut res = f.debug_struct("TcpListener");

if let Ok(addr) = self.socket_addr() {
res = res.field("addr", &addr);
res.field("addr", &addr);
}

let name = if cfg!(windows) {"socket"} else {"fd"};
res = res.field(name, &self.inner.as_inner());
res.finish()
res.field(name, &self.inner.as_inner())
.finish()
}
}

Expand Down Expand Up @@ -484,11 +484,11 @@ impl fmt::Debug for UdpSocket {
let mut res = f.debug_struct("UdpSocket");

if let Ok(addr) = self.socket_addr() {
res = res.field("addr", &addr);
res.field("addr", &addr);
}

let name = if cfg!(windows) {"socket"} else {"fd"};
res = res.field(name, &self.inner.as_inner());
res.finish()
res.field(name, &self.inner.as_inner())
.finish()
}
}
7 changes: 4 additions & 3 deletions src/libstd/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,13 @@ impl fmt::Debug for File {
}

let fd = self.0.raw();
let mut b = f.debug_struct("File").field("fd", &fd);
let mut b = f.debug_struct("File");
b.field("fd", &fd);
if let Some(path) = get_path(fd) {
b = b.field("path", &path);
b.field("path", &path);
}
if let Some((read, write)) = get_mode(fd) {
b = b.field("read", &read).field("write", &write);
b.field("read", &read).field("write", &write);
}
b.finish()
}
Expand Down