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, '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> {
26 fn as_formatter(&mut self) -> fmt::Formatter {
28 // These only exist in the struct for the `Formatter::run` method,
29 // which won’t be used.
30 curarg: self.fmt.curarg.clone(),
33 // We want to preserve these
34 flags: self.fmt.flags,
36 align: self.fmt.align,
37 width: self.fmt.width,
38 precision: self.fmt.precision,
46 impl<'a, 'b: 'a> fmt::Write for PadAdapter<'a, 'b> {
47 fn write_str(&mut self, mut s: &str) -> fmt::Result {
50 self.fmt.write_str(" ")?;
53 let split = match s.find('\n') {
55 self.on_newline = true;
59 self.on_newline = false;
63 self.fmt.write_str(&s[..split])?;
71 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
73 /// This is useful when you wish to output a formatted struct as a part of your
74 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
76 /// This can be constructed by the
77 /// [`Formatter::debug_struct`](struct.Formatter.html#method.debug_struct)
90 /// impl fmt::Debug for Foo {
91 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
92 /// fmt.debug_struct("Foo")
93 /// .field("bar", &self.bar)
94 /// .field("baz", &self.baz)
99 /// // prints "Foo { bar: 10, baz: "Hello World" }"
100 /// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
103 #[allow(missing_debug_implementations)]
104 #[stable(feature = "debug_builders", since = "1.2.0")]
105 pub struct DebugStruct<'a, 'b: 'a> {
106 fmt: &'a mut fmt::Formatter<'b>,
111 pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
113 -> DebugStruct<'a, 'b> {
114 let result = fmt.write_str(name);
122 impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
123 /// Adds a new field to the generated struct output.
124 #[stable(feature = "debug_builders", since = "1.2.0")]
125 pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a, 'b> {
126 self.result = self.result.and_then(|_| {
127 let prefix = if self.has_fields {
133 if self.is_pretty() {
135 let mut writer = PadAdapter::new(self.fmt);
136 writer.write_str(prefix)?;
137 writer.write_str("\n")?;
138 writer.write_str(name)?;
139 writer.write_str(": ")?;
140 value.fmt(&mut writer.as_formatter())
142 write!(self.fmt, "{} {}: ", prefix, name)?;
147 self.has_fields = true;
151 /// Finishes output and returns any error encountered.
152 #[stable(feature = "debug_builders", since = "1.2.0")]
153 pub fn finish(&mut self) -> fmt::Result {
155 self.result = self.result.and_then(|_| {
156 if self.is_pretty() {
157 self.fmt.write_str("\n}")
159 self.fmt.write_str(" }")
166 fn is_pretty(&self) -> bool {
171 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
173 /// This is useful when you wish to output a formatted tuple as a part of your
174 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
176 /// This can be constructed by the
177 /// [`Formatter::debug_tuple`](struct.Formatter.html#method.debug_tuple)
185 /// struct Foo(i32, String);
187 /// impl fmt::Debug for Foo {
188 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
189 /// fmt.debug_tuple("Foo")
196 /// // prints "Foo(10, "Hello World")"
197 /// println!("{:?}", Foo(10, "Hello World".to_string()));
200 #[allow(missing_debug_implementations)]
201 #[stable(feature = "debug_builders", since = "1.2.0")]
202 pub struct DebugTuple<'a, 'b: 'a> {
203 fmt: &'a mut fmt::Formatter<'b>,
209 pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
210 let result = fmt.write_str(name);
215 empty_name: name.is_empty(),
219 impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
220 /// Adds a new field to the generated tuple struct output.
221 #[stable(feature = "debug_builders", since = "1.2.0")]
222 pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
223 self.result = self.result.and_then(|_| {
224 let (prefix, space) = if self.fields > 0 {
230 if self.is_pretty() {
232 let mut writer = PadAdapter::new(self.fmt);
233 writer.write_str(prefix)?;
234 writer.write_str("\n")?;
235 value.fmt(&mut writer.as_formatter())
237 self.fmt.write_str(prefix)?;
238 self.fmt.write_str(space)?;
247 /// Finishes output and returns any error encountered.
248 #[stable(feature = "debug_builders", since = "1.2.0")]
249 pub fn finish(&mut self) -> fmt::Result {
251 self.result = self.result.and_then(|_| {
252 if self.is_pretty() {
253 self.fmt.write_str("\n")?;
255 if self.fields == 1 && self.empty_name {
256 self.fmt.write_str(",")?;
258 self.fmt.write_str(")")
264 fn is_pretty(&self) -> bool {
269 struct DebugInner<'a, 'b: 'a> {
270 fmt: &'a mut fmt::Formatter<'b>,
275 impl<'a, 'b: 'a> DebugInner<'a, 'b> {
276 fn entry(&mut self, entry: &fmt::Debug) {
277 self.result = self.result.and_then(|_| {
278 if self.is_pretty() {
280 let mut writer = PadAdapter::new(self.fmt);
281 writer.write_str(if self.has_fields {
286 entry.fmt(&mut writer.as_formatter())
289 self.fmt.write_str(", ")?
295 self.has_fields = true;
298 pub fn finish(&mut self) {
299 let prefix = if self.is_pretty() && self.has_fields {
304 self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
307 fn is_pretty(&self) -> bool {
312 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
314 /// This is useful when you wish to output a formatted set of items as a part
315 /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
317 /// This can be constructed by the
318 /// [`Formatter::debug_set`](struct.Formatter.html#method.debug_set)
326 /// struct Foo(Vec<i32>);
328 /// impl fmt::Debug for Foo {
329 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
330 /// fmt.debug_set().entries(self.0.iter()).finish()
334 /// // prints "{10, 11}"
335 /// println!("{:?}", Foo(vec![10, 11]));
338 #[allow(missing_debug_implementations)]
339 #[stable(feature = "debug_builders", since = "1.2.0")]
340 pub struct DebugSet<'a, 'b: 'a> {
341 inner: DebugInner<'a, 'b>,
344 pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
345 let result = write!(fmt, "{{");
355 impl<'a, 'b: 'a> DebugSet<'a, 'b> {
356 /// Adds a new entry to the set output.
357 #[stable(feature = "debug_builders", since = "1.2.0")]
358 pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> {
359 self.inner.entry(entry);
363 /// Adds the contents of an iterator of entries to the set output.
364 #[stable(feature = "debug_builders", since = "1.2.0")]
365 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
367 I: IntoIterator<Item = D>
369 for entry in entries {
375 /// Finishes output and returns any error encountered.
376 #[stable(feature = "debug_builders", since = "1.2.0")]
377 pub fn finish(&mut self) -> fmt::Result {
379 self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
383 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
385 /// This is useful when you wish to output a formatted list of items as a part
386 /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
388 /// This can be constructed by the
389 /// [`Formatter::debug_list`](struct.Formatter.html#method.debug_list)
397 /// struct Foo(Vec<i32>);
399 /// impl fmt::Debug for Foo {
400 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
401 /// fmt.debug_list().entries(self.0.iter()).finish()
405 /// // prints "[10, 11]"
406 /// println!("{:?}", Foo(vec![10, 11]));
409 #[allow(missing_debug_implementations)]
410 #[stable(feature = "debug_builders", since = "1.2.0")]
411 pub struct DebugList<'a, 'b: 'a> {
412 inner: DebugInner<'a, 'b>,
415 pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
416 let result = write!(fmt, "[");
426 impl<'a, 'b: 'a> DebugList<'a, 'b> {
427 /// Adds a new entry to the list output.
428 #[stable(feature = "debug_builders", since = "1.2.0")]
429 pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugList<'a, 'b> {
430 self.inner.entry(entry);
434 /// Adds the contents of an iterator of entries to the list output.
435 #[stable(feature = "debug_builders", since = "1.2.0")]
436 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
438 I: IntoIterator<Item = D>
440 for entry in entries {
446 /// Finishes output and returns any error encountered.
447 #[stable(feature = "debug_builders", since = "1.2.0")]
448 pub fn finish(&mut self) -> fmt::Result {
450 self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
454 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
456 /// This is useful when you wish to output a formatted map as a part of your
457 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
459 /// This can be constructed by the
460 /// [`Formatter::debug_map`](struct.Formatter.html#method.debug_map)
468 /// struct Foo(Vec<(String, i32)>);
470 /// impl fmt::Debug for Foo {
471 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
472 /// fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish()
476 /// // prints "{"A": 10, "B": 11}"
477 /// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
480 #[allow(missing_debug_implementations)]
481 #[stable(feature = "debug_builders", since = "1.2.0")]
482 pub struct DebugMap<'a, 'b: 'a> {
483 fmt: &'a mut fmt::Formatter<'b>,
488 pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
489 let result = write!(fmt, "{{");
497 impl<'a, 'b: 'a> DebugMap<'a, 'b> {
498 /// Adds a new entry to the map output.
499 #[stable(feature = "debug_builders", since = "1.2.0")]
500 pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'a, 'b> {
501 self.result = self.result.and_then(|_| {
502 if self.is_pretty() {
504 let mut writer = PadAdapter::new(self.fmt);
505 writer.write_str(if self.has_fields {
510 key.fmt(&mut writer.as_formatter())?;
511 writer.write_str(": ")?;
512 value.fmt(&mut writer.as_formatter())
515 self.fmt.write_str(", ")?
518 self.fmt.write_str(": ")?;
523 self.has_fields = true;
527 /// Adds the contents of an iterator of entries to the map output.
528 #[stable(feature = "debug_builders", since = "1.2.0")]
529 pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
532 I: IntoIterator<Item = (K, V)>
534 for (k, v) in entries {
540 /// Finishes output and returns any error encountered.
541 #[stable(feature = "debug_builders", since = "1.2.0")]
542 pub fn finish(&mut self) -> fmt::Result {
543 let prefix = if self.is_pretty() && self.has_fields {
548 self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
551 fn is_pretty(&self) -> bool {