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.
11 use fmt::{self, FlagV1};
13 struct PadAdapter<'a, 'b: 'a> {
14 fmt: &'a mut fmt::Formatter<'b>,
18 impl<'a, 'b: 'a> PadAdapter<'a, 'b> {
19 fn new(fmt: &'a mut fmt::Formatter<'b>) -> PadAdapter<'a, 'b> {
27 impl<'a, 'b: 'a> fmt::Write for PadAdapter<'a, 'b> {
28 fn write_str(&mut self, mut s: &str) -> fmt::Result {
31 self.fmt.write_str(" ")?;
34 let split = match s.find('\n') {
36 self.on_newline = true;
40 self.on_newline = false;
44 self.fmt.write_str(&s[..split])?;
52 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
54 /// This is useful when you wish to output a formatted struct as a part of your
55 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
57 /// This can be constructed by the
58 /// [`Formatter::debug_struct`](struct.Formatter.html#method.debug_struct)
71 /// impl fmt::Debug for Foo {
72 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
73 /// fmt.debug_struct("Foo")
74 /// .field("bar", &self.bar)
75 /// .field("baz", &self.baz)
80 /// // prints "Foo { bar: 10, baz: "Hello World" }"
81 /// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
84 #[allow(missing_debug_implementations)]
85 #[stable(feature = "debug_builders", since = "1.2.0")]
86 pub struct DebugStruct<'a, 'b: 'a> {
87 fmt: &'a mut fmt::Formatter<'b>,
92 pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
94 -> DebugStruct<'a, 'b> {
95 let result = fmt.write_str(name);
103 impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
104 /// Adds a new field to the generated struct output.
105 #[stable(feature = "debug_builders", since = "1.2.0")]
106 pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a, 'b> {
107 self.result = self.result.and_then(|_| {
108 let prefix = if self.has_fields {
114 if self.is_pretty() {
115 let mut writer = PadAdapter::new(self.fmt);
116 fmt::write(&mut writer,
117 format_args!("{}\n{}: {:#?}", prefix, name, value))
119 write!(self.fmt, "{} {}: {:?}", prefix, name, value)
123 self.has_fields = true;
127 /// Finishes output and returns any error encountered.
128 #[stable(feature = "debug_builders", since = "1.2.0")]
129 pub fn finish(&mut self) -> fmt::Result {
131 self.result = self.result.and_then(|_| {
132 if self.is_pretty() {
133 self.fmt.write_str("\n}")
135 self.fmt.write_str(" }")
142 fn is_pretty(&self) -> bool {
143 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
147 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
149 /// This is useful when you wish to output a formatted tuple as a part of your
150 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
152 /// This can be constructed by the
153 /// [`Formatter::debug_tuple`](struct.Formatter.html#method.debug_tuple)
161 /// struct Foo(i32, String);
163 /// impl fmt::Debug for Foo {
164 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
165 /// fmt.debug_tuple("Foo")
172 /// // prints "Foo(10, "Hello World")"
173 /// println!("{:?}", Foo(10, "Hello World".to_string()));
176 #[allow(missing_debug_implementations)]
177 #[stable(feature = "debug_builders", since = "1.2.0")]
178 pub struct DebugTuple<'a, 'b: 'a> {
179 fmt: &'a mut fmt::Formatter<'b>,
185 pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
186 let result = fmt.write_str(name);
191 empty_name: name.is_empty(),
195 impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
196 /// Adds a new field to the generated tuple struct output.
197 #[stable(feature = "debug_builders", since = "1.2.0")]
198 pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
199 self.result = self.result.and_then(|_| {
200 let (prefix, space) = if self.fields > 0 {
206 if self.is_pretty() {
207 let mut writer = PadAdapter::new(self.fmt);
208 fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, value))
210 write!(self.fmt, "{}{}{:?}", prefix, space, value)
218 /// Finishes output and returns any error encountered.
219 #[stable(feature = "debug_builders", since = "1.2.0")]
220 pub fn finish(&mut self) -> fmt::Result {
222 self.result = self.result.and_then(|_| {
223 if self.is_pretty() {
224 self.fmt.write_str("\n")?;
226 if self.fields == 1 && self.empty_name {
227 self.fmt.write_str(",")?;
229 self.fmt.write_str(")")
235 fn is_pretty(&self) -> bool {
236 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
240 struct DebugInner<'a, 'b: 'a> {
241 fmt: &'a mut fmt::Formatter<'b>,
246 impl<'a, 'b: 'a> DebugInner<'a, 'b> {
247 fn entry(&mut self, entry: &fmt::Debug) {
248 self.result = self.result.and_then(|_| {
249 if self.is_pretty() {
250 let mut writer = PadAdapter::new(self.fmt);
251 let prefix = if self.has_fields {
256 fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, entry))
258 let prefix = if self.has_fields {
263 write!(self.fmt, "{}{:?}", prefix, entry)
267 self.has_fields = true;
270 pub fn finish(&mut self) {
271 let prefix = if self.is_pretty() && self.has_fields {
276 self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
279 fn is_pretty(&self) -> bool {
280 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
284 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
286 /// This is useful when you wish to output a formatted set of items as a part
287 /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
289 /// This can be constructed by the
290 /// [`Formatter::debug_set`](struct.Formatter.html#method.debug_set)
298 /// struct Foo(Vec<i32>);
300 /// impl fmt::Debug for Foo {
301 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
302 /// fmt.debug_set().entries(self.0.iter()).finish()
306 /// // prints "{10, 11}"
307 /// println!("{:?}", Foo(vec![10, 11]));
310 #[allow(missing_debug_implementations)]
311 #[stable(feature = "debug_builders", since = "1.2.0")]
312 pub struct DebugSet<'a, 'b: 'a> {
313 inner: DebugInner<'a, 'b>,
316 pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
317 let result = write!(fmt, "{{");
327 impl<'a, 'b: 'a> DebugSet<'a, 'b> {
328 /// Adds a new entry to the set output.
329 #[stable(feature = "debug_builders", since = "1.2.0")]
330 pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> {
331 self.inner.entry(entry);
335 /// Adds the contents of an iterator of entries to the set output.
336 #[stable(feature = "debug_builders", since = "1.2.0")]
337 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
339 I: IntoIterator<Item = D>
341 for entry in entries {
347 /// Finishes output and returns any error encountered.
348 #[stable(feature = "debug_builders", since = "1.2.0")]
349 pub fn finish(&mut self) -> fmt::Result {
351 self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
355 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
357 /// This is useful when you wish to output a formatted list of items as a part
358 /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
360 /// This can be constructed by the
361 /// [`Formatter::debug_list`](struct.Formatter.html#method.debug_list)
369 /// struct Foo(Vec<i32>);
371 /// impl fmt::Debug for Foo {
372 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
373 /// fmt.debug_list().entries(self.0.iter()).finish()
377 /// // prints "[10, 11]"
378 /// println!("{:?}", Foo(vec![10, 11]));
381 #[allow(missing_debug_implementations)]
382 #[stable(feature = "debug_builders", since = "1.2.0")]
383 pub struct DebugList<'a, 'b: 'a> {
384 inner: DebugInner<'a, 'b>,
387 pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
388 let result = write!(fmt, "[");
398 impl<'a, 'b: 'a> DebugList<'a, 'b> {
399 /// Adds a new entry to the list output.
400 #[stable(feature = "debug_builders", since = "1.2.0")]
401 pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugList<'a, 'b> {
402 self.inner.entry(entry);
406 /// Adds the contents of an iterator of entries to the list output.
407 #[stable(feature = "debug_builders", since = "1.2.0")]
408 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
410 I: IntoIterator<Item = D>
412 for entry in entries {
418 /// Finishes output and returns any error encountered.
419 #[stable(feature = "debug_builders", since = "1.2.0")]
420 pub fn finish(&mut self) -> fmt::Result {
422 self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
426 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
428 /// This is useful when you wish to output a formatted map as a part of your
429 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
431 /// This can be constructed by the
432 /// [`Formatter::debug_map`](struct.Formatter.html#method.debug_map)
440 /// struct Foo(Vec<(String, i32)>);
442 /// impl fmt::Debug for Foo {
443 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
444 /// fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish()
448 /// // prints "{"A": 10, "B": 11}"
449 /// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
452 #[allow(missing_debug_implementations)]
453 #[stable(feature = "debug_builders", since = "1.2.0")]
454 pub struct DebugMap<'a, 'b: 'a> {
455 fmt: &'a mut fmt::Formatter<'b>,
460 pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
461 let result = write!(fmt, "{{");
469 impl<'a, 'b: 'a> DebugMap<'a, 'b> {
470 /// Adds a new entry to the map output.
471 #[stable(feature = "debug_builders", since = "1.2.0")]
472 pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'a, 'b> {
473 self.result = self.result.and_then(|_| {
474 if self.is_pretty() {
475 let mut writer = PadAdapter::new(self.fmt);
476 let prefix = if self.has_fields {
481 fmt::write(&mut writer,
482 format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
484 let prefix = if self.has_fields {
489 write!(self.fmt, "{}{:?}: {:?}", prefix, key, value)
493 self.has_fields = true;
497 /// Adds the contents of an iterator of entries to the map output.
498 #[stable(feature = "debug_builders", since = "1.2.0")]
499 pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
502 I: IntoIterator<Item = (K, V)>
504 for (k, v) in entries {
510 /// Finishes output and returns any error encountered.
511 #[stable(feature = "debug_builders", since = "1.2.0")]
512 pub fn finish(&mut self) -> fmt::Result {
513 let prefix = if self.is_pretty() && self.has_fields {
518 self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
521 fn is_pretty(&self) -> bool {
522 self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0