1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 struct PadAdapter<'a> {
14 buf: &'a mut (dyn fmt::Write + 'a),
18 impl<'a> PadAdapter<'a> {
19 fn wrap<'b, 'c: 'a+'b>(fmt: &'c mut fmt::Formatter, slot: &'b mut Option<Self>)
20 -> fmt::Formatter<'b> {
21 fmt.wrap_buf(move |buf| {
22 *slot = Some(PadAdapter {
26 slot.as_mut().unwrap()
31 impl fmt::Write for PadAdapter<'_> {
32 fn write_str(&mut self, mut s: &str) -> fmt::Result {
35 self.buf.write_str(" ")?;
38 let split = match s.find('\n') {
40 self.on_newline = true;
44 self.on_newline = false;
48 self.buf.write_str(&s[..split])?;
56 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
58 /// This is useful when you wish to output a formatted struct as a part of your
59 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
61 /// This can be constructed by the
62 /// [`Formatter::debug_struct`](struct.Formatter.html#method.debug_struct)
75 /// impl fmt::Debug for Foo {
76 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
77 /// fmt.debug_struct("Foo")
78 /// .field("bar", &self.bar)
79 /// .field("baz", &self.baz)
84 /// // prints "Foo { bar: 10, baz: "Hello World" }"
85 /// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
87 #[must_use = "must eventually call `finish()` on Debug builders"]
88 #[allow(missing_debug_implementations)]
89 #[stable(feature = "debug_builders", since = "1.2.0")]
90 pub struct DebugStruct<'a, 'b: 'a> {
91 fmt: &'a mut fmt::Formatter<'b>,
96 pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
98 -> DebugStruct<'a, 'b> {
99 let result = fmt.write_str(name);
107 impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
108 /// Adds a new field to the generated struct output.
109 #[stable(feature = "debug_builders", since = "1.2.0")]
110 pub fn field(&mut self, name: &str, value: &dyn fmt::Debug) -> &mut DebugStruct<'a, 'b> {
111 self.result = self.result.and_then(|_| {
112 let prefix = if self.has_fields {
118 if self.is_pretty() {
120 let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
121 writer.write_str(prefix)?;
122 writer.write_str("\n")?;
123 writer.write_str(name)?;
124 writer.write_str(": ")?;
125 value.fmt(&mut writer)
127 write!(self.fmt, "{} {}: ", prefix, name)?;
132 self.has_fields = true;
136 /// Finishes output and returns any error encountered.
137 #[stable(feature = "debug_builders", since = "1.2.0")]
138 pub fn finish(&mut self) -> fmt::Result {
140 self.result = self.result.and_then(|_| {
141 if self.is_pretty() {
142 self.fmt.write_str("\n}")
144 self.fmt.write_str(" }")
151 fn is_pretty(&self) -> bool {
156 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
158 /// This is useful when you wish to output a formatted tuple as a part of your
159 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
161 /// This can be constructed by the
162 /// [`Formatter::debug_tuple`](struct.Formatter.html#method.debug_tuple)
170 /// struct Foo(i32, String);
172 /// impl fmt::Debug for Foo {
173 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
174 /// fmt.debug_tuple("Foo")
181 /// // prints "Foo(10, "Hello World")"
182 /// println!("{:?}", Foo(10, "Hello World".to_string()));
184 #[must_use = "must eventually call `finish()` on Debug builders"]
185 #[allow(missing_debug_implementations)]
186 #[stable(feature = "debug_builders", since = "1.2.0")]
187 pub struct DebugTuple<'a, 'b: 'a> {
188 fmt: &'a mut fmt::Formatter<'b>,
194 pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
195 let result = fmt.write_str(name);
200 empty_name: name.is_empty(),
204 impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
205 /// Adds a new field to the generated tuple struct output.
206 #[stable(feature = "debug_builders", since = "1.2.0")]
207 pub fn field(&mut self, value: &dyn fmt::Debug) -> &mut DebugTuple<'a, 'b> {
208 self.result = self.result.and_then(|_| {
209 let (prefix, space) = if self.fields > 0 {
215 if self.is_pretty() {
217 let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
218 writer.write_str(prefix)?;
219 writer.write_str("\n")?;
220 value.fmt(&mut writer)
222 self.fmt.write_str(prefix)?;
223 self.fmt.write_str(space)?;
232 /// Finishes output and returns any error encountered.
233 #[stable(feature = "debug_builders", since = "1.2.0")]
234 pub fn finish(&mut self) -> fmt::Result {
236 self.result = self.result.and_then(|_| {
237 if self.is_pretty() {
238 self.fmt.write_str("\n")?;
240 if self.fields == 1 && self.empty_name {
241 self.fmt.write_str(",")?;
243 self.fmt.write_str(")")
249 fn is_pretty(&self) -> bool {
254 struct DebugInner<'a, 'b: 'a> {
255 fmt: &'a mut fmt::Formatter<'b>,
260 impl<'a, 'b: 'a> DebugInner<'a, 'b> {
261 fn entry(&mut self, entry: &dyn fmt::Debug) {
262 self.result = self.result.and_then(|_| {
263 if self.is_pretty() {
265 let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
266 writer.write_str(if self.has_fields {
271 entry.fmt(&mut writer)
274 self.fmt.write_str(", ")?
280 self.has_fields = true;
283 pub fn finish(&mut self) {
284 let prefix = if self.is_pretty() && self.has_fields {
289 self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
292 fn is_pretty(&self) -> bool {
297 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
299 /// This is useful when you wish to output a formatted set of items as a part
300 /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
302 /// This can be constructed by the
303 /// [`Formatter::debug_set`](struct.Formatter.html#method.debug_set)
311 /// struct Foo(Vec<i32>);
313 /// impl fmt::Debug for Foo {
314 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
315 /// fmt.debug_set().entries(self.0.iter()).finish()
319 /// // prints "{10, 11}"
320 /// println!("{:?}", Foo(vec![10, 11]));
322 #[must_use = "must eventually call `finish()` on Debug builders"]
323 #[allow(missing_debug_implementations)]
324 #[stable(feature = "debug_builders", since = "1.2.0")]
325 pub struct DebugSet<'a, 'b: 'a> {
326 inner: DebugInner<'a, 'b>,
329 pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
330 let result = write!(fmt, "{{");
340 impl<'a, 'b: 'a> DebugSet<'a, 'b> {
341 /// Adds a new entry to the set output.
342 #[stable(feature = "debug_builders", since = "1.2.0")]
343 pub fn entry(&mut self, entry: &dyn fmt::Debug) -> &mut DebugSet<'a, 'b> {
344 self.inner.entry(entry);
348 /// Adds the contents of an iterator of entries to the set output.
349 #[stable(feature = "debug_builders", since = "1.2.0")]
350 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
352 I: IntoIterator<Item = D>
354 for entry in entries {
360 /// Finishes output and returns any error encountered.
361 #[stable(feature = "debug_builders", since = "1.2.0")]
362 pub fn finish(&mut self) -> fmt::Result {
364 self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
368 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
370 /// This is useful when you wish to output a formatted list of items as a part
371 /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
373 /// This can be constructed by the
374 /// [`Formatter::debug_list`](struct.Formatter.html#method.debug_list)
382 /// struct Foo(Vec<i32>);
384 /// impl fmt::Debug for Foo {
385 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
386 /// fmt.debug_list().entries(self.0.iter()).finish()
390 /// // prints "[10, 11]"
391 /// println!("{:?}", Foo(vec![10, 11]));
393 #[must_use = "must eventually call `finish()` on Debug builders"]
394 #[allow(missing_debug_implementations)]
395 #[stable(feature = "debug_builders", since = "1.2.0")]
396 pub struct DebugList<'a, 'b: 'a> {
397 inner: DebugInner<'a, 'b>,
400 pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
401 let result = write!(fmt, "[");
411 impl<'a, 'b: 'a> DebugList<'a, 'b> {
412 /// Adds a new entry to the list output.
413 #[stable(feature = "debug_builders", since = "1.2.0")]
414 pub fn entry(&mut self, entry: &dyn fmt::Debug) -> &mut DebugList<'a, 'b> {
415 self.inner.entry(entry);
419 /// Adds the contents of an iterator of entries to the list output.
420 #[stable(feature = "debug_builders", since = "1.2.0")]
421 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
423 I: IntoIterator<Item = D>
425 for entry in entries {
431 /// Finishes output and returns any error encountered.
432 #[stable(feature = "debug_builders", since = "1.2.0")]
433 pub fn finish(&mut self) -> fmt::Result {
435 self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
439 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
441 /// This is useful when you wish to output a formatted map as a part of your
442 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
444 /// This can be constructed by the
445 /// [`Formatter::debug_map`](struct.Formatter.html#method.debug_map)
453 /// struct Foo(Vec<(String, i32)>);
455 /// impl fmt::Debug for Foo {
456 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
457 /// fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish()
461 /// // prints "{"A": 10, "B": 11}"
462 /// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
464 #[must_use = "must eventually call `finish()` on Debug builders"]
465 #[allow(missing_debug_implementations)]
466 #[stable(feature = "debug_builders", since = "1.2.0")]
467 pub struct DebugMap<'a, 'b: 'a> {
468 fmt: &'a mut fmt::Formatter<'b>,
473 pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
474 let result = write!(fmt, "{{");
482 impl<'a, 'b: 'a> DebugMap<'a, 'b> {
483 /// Adds a new entry to the map output.
484 #[stable(feature = "debug_builders", since = "1.2.0")]
485 pub fn entry(&mut self, key: &dyn fmt::Debug, value: &dyn fmt::Debug) -> &mut DebugMap<'a, 'b> {
486 self.result = self.result.and_then(|_| {
487 if self.is_pretty() {
489 let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
490 writer.write_str(if self.has_fields {
495 key.fmt(&mut writer)?;
496 writer.write_str(": ")?;
497 value.fmt(&mut writer)
500 self.fmt.write_str(", ")?
503 self.fmt.write_str(": ")?;
508 self.has_fields = true;
512 /// Adds the contents of an iterator of entries to the map output.
513 #[stable(feature = "debug_builders", since = "1.2.0")]
514 pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
517 I: IntoIterator<Item = (K, V)>
519 for (k, v) in entries {
525 /// Finishes output and returns any error encountered.
526 #[stable(feature = "debug_builders", since = "1.2.0")]
527 pub fn finish(&mut self) -> fmt::Result {
528 let prefix = if self.is_pretty() && self.has_fields {
533 self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
536 fn is_pretty(&self) -> bool {