Skip to content

Commit b301e02

Browse files
committed
Auto merge of #25548 - sfackler:debug-builders-by-ref, r=alexcrichton
Based on feedback from https://internals.rust-lang.org/t/final-comment-period-for-debug-builders-stabilization/2007/2
2 parents eeaf2ba + e87b353 commit b301e02

File tree

9 files changed

+69
-43
lines changed

9 files changed

+69
-43
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: 46 additions & 21 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
","
@@ -93,10 +93,9 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
9393
self
9494
}
9595

96-
/// Consumes the `DebugStruct`, finishing output and returning any error
97-
/// encountered.
96+
/// Finishes output and returns any error encountered.
9897
#[unstable(feature = "debug_builders", reason = "method was just created")]
99-
pub fn finish(mut self) -> fmt::Result {
98+
pub fn finish(&mut self) -> fmt::Result {
10099
if self.has_fields {
101100
self.result = self.result.and_then(|_| {
102101
if self.is_pretty() {
@@ -136,7 +135,7 @@ pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> D
136135
impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
137136
/// Adds a new field to the generated tuple struct output.
138137
#[unstable(feature = "debug_builders", reason = "method was just created")]
139-
pub fn field(mut self, value: &fmt::Debug) -> DebugTuple<'a, 'b> {
138+
pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
140139
self.result = self.result.and_then(|_| {
141140
let (prefix, space) = if self.has_fields {
142141
(",", " ")
@@ -156,10 +155,9 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
156155
self
157156
}
158157

159-
/// Consumes the `DebugTuple`, finishing output and returning any error
160-
/// encountered.
158+
/// Finishes output and returns any error encountered.
161159
#[unstable(feature = "debug_builders", reason = "method was just created")]
162-
pub fn finish(mut self) -> fmt::Result {
160+
pub fn finish(&mut self) -> fmt::Result {
163161
if self.has_fields {
164162
self.result = self.result.and_then(|_| {
165163
if self.is_pretty() {
@@ -231,15 +229,24 @@ pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b
231229
impl<'a, 'b: 'a> DebugSet<'a, 'b> {
232230
/// Adds a new entry to the set output.
233231
#[unstable(feature = "debug_builders", reason = "method was just created")]
234-
pub fn entry(mut self, entry: &fmt::Debug) -> DebugSet<'a, 'b> {
232+
pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> {
235233
self.inner.entry(entry);
236234
self
237235
}
238236

239-
/// Consumes the `DebugSet`, finishing output and returning any error
240-
/// encountered.
237+
/// Adds the contents of an iterator of entries to the set output.
241238
#[unstable(feature = "debug_builders", reason = "method was just created")]
242-
pub fn finish(mut self) -> fmt::Result {
239+
pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
240+
where D: fmt::Debug, I: IntoIterator<Item=D> {
241+
for entry in entries {
242+
self.entry(&entry);
243+
}
244+
self
245+
}
246+
247+
/// Finishes output and returns any error encountered.
248+
#[unstable(feature = "debug_builders", reason = "method was just created")]
249+
pub fn finish(&mut self) -> fmt::Result {
243250
self.inner.finish();
244251
self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
245252
}
@@ -265,17 +272,26 @@ pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a,
265272
}
266273

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

275-
/// Consumes the `DebugSet`, finishing output and returning any error
276-
/// encountered.
282+
/// Adds the contents of an iterator of entries to the list output.
283+
#[unstable(feature = "debug_builders", reason = "method was just created")]
284+
pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
285+
where D: fmt::Debug, I: IntoIterator<Item=D> {
286+
for entry in entries {
287+
self.entry(&entry);
288+
}
289+
self
290+
}
291+
292+
/// Finishes output and returns any error encountered.
277293
#[unstable(feature = "debug_builders", reason = "method was just created")]
278-
pub fn finish(mut self) -> fmt::Result {
294+
pub fn finish(&mut self) -> fmt::Result {
279295
self.inner.finish();
280296
self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
281297
}
@@ -303,7 +319,7 @@ pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b
303319
impl<'a, 'b: 'a> DebugMap<'a, 'b> {
304320
/// Adds a new entry to the map output.
305321
#[unstable(feature = "debug_builders", reason = "method was just created")]
306-
pub fn entry(mut self, key: &fmt::Debug, value: &fmt::Debug) -> DebugMap<'a, 'b> {
322+
pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'a, 'b> {
307323
self.result = self.result.and_then(|_| {
308324
if self.is_pretty() {
309325
let mut writer = PadAdapter::new(self.fmt);
@@ -319,10 +335,19 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
319335
self
320336
}
321337

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

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+
/// fmt.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().map(|&(ref k, ref v)| (k, v))).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)