Skip to content

Commit bd85983

Browse files
committed
Make debug builders take &mut self, add entries method
[breaking-change]
1 parent 8b7c17d commit bd85983

File tree

9 files changed

+64
-33
lines changed

9 files changed

+64
-33
lines changed

src/libcollections/btree/map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ impl<K: Ord, V: Ord> Ord for BTreeMap<K, V> {
926926
#[stable(feature = "rust1", since = "1.0.0")]
927927
impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
928928
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
929-
self.iter().fold(f.debug_map(), |b, (k, v)| b.entry(k, v)).finish()
929+
f.debug_map().entries(self.iter()).finish()
930930
}
931931
}
932932

src/libcollections/btree/set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
622622
#[stable(feature = "rust1", since = "1.0.0")]
623623
impl<T: Debug> Debug for BTreeSet<T> {
624624
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
625-
self.iter().fold(f.debug_set(), |b, e| b.entry(e)).finish()
625+
f.debug_set().entries(self.iter()).finish()
626626
}
627627
}
628628

src/libcollections/linked_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ impl<A: Clone> Clone for LinkedList<A> {
917917
#[stable(feature = "rust1", since = "1.0.0")]
918918
impl<A: fmt::Debug> fmt::Debug for LinkedList<A> {
919919
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
920-
self.iter().fold(f.debug_list(), |b, e| b.entry(e)).finish()
920+
f.debug_list().entries(self.iter()).finish()
921921
}
922922
}
923923

src/libcore/fmt/builders.rs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str)
7373
impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
7474
/// Adds a new field to the generated struct output.
7575
#[unstable(feature = "debug_builders", reason = "method was just created")]
76-
pub fn field(mut self, name: &str, value: &fmt::Debug) -> DebugStruct<'a, 'b> {
76+
pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a, 'b> {
7777
self.result = self.result.and_then(|_| {
7878
let prefix = if self.has_fields {
7979
","
@@ -96,7 +96,7 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
9696
/// Consumes the `DebugStruct`, finishing output and returning any error
9797
/// encountered.
9898
#[unstable(feature = "debug_builders", reason = "method was just created")]
99-
pub fn finish(mut self) -> fmt::Result {
99+
pub fn finish(&mut self) -> fmt::Result {
100100
if self.has_fields {
101101
self.result = self.result.and_then(|_| {
102102
if self.is_pretty() {
@@ -136,7 +136,7 @@ pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> D
136136
impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
137137
/// Adds a new field to the generated tuple struct output.
138138
#[unstable(feature = "debug_builders", reason = "method was just created")]
139-
pub fn field(mut self, value: &fmt::Debug) -> DebugTuple<'a, 'b> {
139+
pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
140140
self.result = self.result.and_then(|_| {
141141
let (prefix, space) = if self.has_fields {
142142
(",", " ")
@@ -159,7 +159,7 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
159159
/// Consumes the `DebugTuple`, finishing output and returning any error
160160
/// encountered.
161161
#[unstable(feature = "debug_builders", reason = "method was just created")]
162-
pub fn finish(mut self) -> fmt::Result {
162+
pub fn finish(&mut self) -> fmt::Result {
163163
if self.has_fields {
164164
self.result = self.result.and_then(|_| {
165165
if self.is_pretty() {
@@ -231,15 +231,25 @@ pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b
231231
impl<'a, 'b: 'a> DebugSet<'a, 'b> {
232232
/// Adds a new entry to the set output.
233233
#[unstable(feature = "debug_builders", reason = "method was just created")]
234-
pub fn entry(mut self, entry: &fmt::Debug) -> DebugSet<'a, 'b> {
234+
pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> {
235235
self.inner.entry(entry);
236236
self
237237
}
238238

239+
/// Adds the contents of an iterator of entries to the set output.
240+
#[unstable(feature = "debug_builders", reason = "method was just created")]
241+
pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
242+
where D: fmt::Debug, I: IntoIterator<Item=D> {
243+
for entry in entries {
244+
self.entry(&entry);
245+
}
246+
self
247+
}
248+
239249
/// Consumes the `DebugSet`, finishing output and returning any error
240250
/// encountered.
241251
#[unstable(feature = "debug_builders", reason = "method was just created")]
242-
pub fn finish(mut self) -> fmt::Result {
252+
pub fn finish(&mut self) -> fmt::Result {
243253
self.inner.finish();
244254
self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
245255
}
@@ -265,17 +275,27 @@ pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a,
265275
}
266276

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

285+
/// Adds the contents of an iterator of entries to the list output.
286+
#[unstable(feature = "debug_builders", reason = "method was just created")]
287+
pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
288+
where D: fmt::Debug, I: IntoIterator<Item=D> {
289+
for entry in entries {
290+
self.entry(&entry);
291+
}
292+
self
293+
}
294+
275295
/// Consumes the `DebugSet`, finishing output and returning any error
276296
/// encountered.
277297
#[unstable(feature = "debug_builders", reason = "method was just created")]
278-
pub fn finish(mut self) -> fmt::Result {
298+
pub fn finish(&mut self) -> fmt::Result {
279299
self.inner.finish();
280300
self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
281301
}
@@ -303,7 +323,7 @@ pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b
303323
impl<'a, 'b: 'a> DebugMap<'a, 'b> {
304324
/// Adds a new entry to the map output.
305325
#[unstable(feature = "debug_builders", reason = "method was just created")]
306-
pub fn entry(mut self, key: &fmt::Debug, value: &fmt::Debug) -> DebugMap<'a, 'b> {
326+
pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'a, 'b> {
307327
self.result = self.result.and_then(|_| {
308328
if self.is_pretty() {
309329
let mut writer = PadAdapter::new(self.fmt);
@@ -319,10 +339,20 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
319339
self
320340
}
321341

342+
/// Adds the contents of an iterator of entries to the map output.
343+
#[unstable(feature = "debug_builders", reason = "method was just created")]
344+
pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
345+
where K: fmt::Debug, V: fmt::Debug, I: IntoIterator<Item=(K, V)> {
346+
for (k, v) in entries {
347+
self.entry(&k, &v);
348+
}
349+
self
350+
}
351+
322352
/// Consumes the `DebugMap`, finishing output and returning any error
323353
/// encountered.
324354
#[unstable(feature = "debug_builders", reason = "method was just created")]
325-
pub fn finish(self) -> fmt::Result {
355+
pub fn finish(&mut self) -> fmt::Result {
326356
let prefix = if self.is_pretty() && self.has_fields { "\n" } else { "" };
327357
self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
328358
}

src/libcore/fmt/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ impl<'a> Formatter<'a> {
787787
///
788788
/// impl fmt::Debug for Foo {
789789
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
790-
/// self.0.iter().fold(fmt.debug_list(), |b, e| b.entry(e)).finish()
790+
/// fnt.debug_list().entries(self.0.iter()).finish()
791791
/// }
792792
/// }
793793
///
@@ -813,7 +813,7 @@ impl<'a> Formatter<'a> {
813813
///
814814
/// impl fmt::Debug for Foo {
815815
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
816-
/// self.0.iter().fold(fmt.debug_set(), |b, e| b.entry(e)).finish()
816+
/// fmt.debug_set().entries(self.0.iter()).finish()
817817
/// }
818818
/// }
819819
///
@@ -839,7 +839,7 @@ impl<'a> Formatter<'a> {
839839
///
840840
/// impl fmt::Debug for Foo {
841841
/// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
842-
/// self.0.iter().fold(fmt.debug_map(), |b, &(ref k, ref v)| b.entry(k, v)).finish()
842+
/// fmt.debug_map().entries(self.0.iter()).finish()
843843
/// }
844844
/// }
845845
///
@@ -1120,7 +1120,7 @@ tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
11201120
#[stable(feature = "rust1", since = "1.0.0")]
11211121
impl<T: Debug> Debug for [T] {
11221122
fn fmt(&self, f: &mut Formatter) -> Result {
1123-
self.iter().fold(f.debug_list(), |b, e| b.entry(e)).finish()
1123+
f.debug_list().entries(self.iter()).finish()
11241124
}
11251125
}
11261126

src/libstd/collections/hash/map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ impl<K, V, S> Debug for HashMap<K, V, S>
12221222
where K: Eq + Hash + Debug, V: Debug, S: HashState
12231223
{
12241224
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1225-
self.iter().fold(f.debug_map(), |b, (k, v)| b.entry(k, v)).finish()
1225+
f.debug_map().entries(self.iter()).finish()
12261226
}
12271227
}
12281228

src/libstd/collections/hash/set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ impl<T, S> fmt::Debug for HashSet<T, S>
585585
S: HashState
586586
{
587587
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
588-
self.iter().fold(f.debug_set(), |b, e| b.entry(e)).finish()
588+
f.debug_set().entries(self.iter()).finish()
589589
}
590590
}
591591

src/libstd/sys/common/net.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,16 @@ impl fmt::Debug for TcpStream {
274274
let mut res = f.debug_struct("TcpStream");
275275

276276
if let Ok(addr) = self.socket_addr() {
277-
res = res.field("addr", &addr);
277+
res.field("addr", &addr);
278278
}
279279

280280
if let Ok(peer) = self.peer_addr() {
281-
res = res.field("peer", &peer);
281+
res.field("peer", &peer);
282282
}
283283

284284
let name = if cfg!(windows) {"socket"} else {"fd"};
285-
res = res.field(name, &self.inner.as_inner());
286-
res.finish()
285+
res.field(name, &self.inner.as_inner())
286+
.finish()
287287
}
288288
}
289289

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

353353
if let Ok(addr) = self.socket_addr() {
354-
res = res.field("addr", &addr);
354+
res.field("addr", &addr);
355355
}
356356

357357
let name = if cfg!(windows) {"socket"} else {"fd"};
358-
res = res.field(name, &self.inner.as_inner());
359-
res.finish()
358+
res.field(name, &self.inner.as_inner())
359+
.finish()
360360
}
361361
}
362362

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

486486
if let Ok(addr) = self.socket_addr() {
487-
res = res.field("addr", &addr);
487+
res.field("addr", &addr);
488488
}
489489

490490
let name = if cfg!(windows) {"socket"} else {"fd"};
491-
res = res.field(name, &self.inner.as_inner());
492-
res.finish()
491+
res.field(name, &self.inner.as_inner())
492+
.finish()
493493
}
494494
}

src/libstd/sys/unix/fs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,13 @@ impl fmt::Debug for File {
408408
}
409409

410410
let fd = self.0.raw();
411-
let mut b = f.debug_struct("File").field("fd", &fd);
411+
let mut b = f.debug_struct("File");
412+
b.field("fd", &fd);
412413
if let Some(path) = get_path(fd) {
413-
b = b.field("path", &path);
414+
b.field("path", &path);
414415
}
415416
if let Some((read, write)) = get_mode(fd) {
416-
b = b.field("read", &read).field("write", &write);
417+
b.field("read", &read).field("write", &write);
417418
}
418419
b.finish()
419420
}

0 commit comments

Comments
 (0)