diff options
| author | bors <bors@rust-lang.org> | 2015-05-19 05:39:29 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-05-19 05:39:29 +0000 |
| commit | b301e02f37127da993dd2cf370aa1066d48b042e (patch) | |
| tree | 17d7f20cea3410938fa108d33203660c55162cc2 /src/libcore | |
| parent | eeaf2ba489e1c9b9d6d3e5f4566c3ecbdcaa14f2 (diff) | |
| parent | e87b353d65c67a5e7326b2216feba9c1ddf2402c (diff) | |
| download | rust-b301e02f37127da993dd2cf370aa1066d48b042e.tar.gz rust-b301e02f37127da993dd2cf370aa1066d48b042e.zip | |
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
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/fmt/builders.rs | 67 | ||||
| -rw-r--r-- | src/libcore/fmt/mod.rs | 8 |
2 files changed, 50 insertions, 25 deletions
diff --git a/src/libcore/fmt/builders.rs b/src/libcore/fmt/builders.rs index f61a7f2d30c..c1786dc4c28 100644 --- a/src/libcore/fmt/builders.rs +++ b/src/libcore/fmt/builders.rs @@ -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 { "," @@ -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() { @@ -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 { (",", " ") @@ -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() { @@ -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 + } + + /// 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("}")) } @@ -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("]")) } @@ -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); @@ -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)) } diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index a87f6619fe8..22575f340d7 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -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() /// } /// } /// @@ -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() /// } /// } /// @@ -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() /// } /// } /// @@ -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() } } |
