3 struct PadAdapter<'a> {
4 buf: &'a mut (dyn fmt::Write + 'a),
8 impl<'a> PadAdapter<'a> {
9 fn wrap<'b, 'c: 'a+'b>(fmt: &'c mut fmt::Formatter, slot: &'b mut Option<Self>)
10 -> fmt::Formatter<'b> {
11 fmt.wrap_buf(move |buf| {
12 *slot = Some(PadAdapter {
16 slot.as_mut().unwrap()
21 impl fmt::Write for PadAdapter<'_> {
22 fn write_str(&mut self, mut s: &str) -> fmt::Result {
25 self.buf.write_str(" ")?;
28 let split = match s.find('\n') {
30 self.on_newline = true;
34 self.on_newline = false;
38 self.buf.write_str(&s[..split])?;
46 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
48 /// This is useful when you wish to output a formatted struct as a part of your
49 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
51 /// This can be constructed by the
52 /// [`Formatter::debug_struct`](struct.Formatter.html#method.debug_struct)
65 /// impl fmt::Debug for Foo {
66 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
67 /// fmt.debug_struct("Foo")
68 /// .field("bar", &self.bar)
69 /// .field("baz", &self.baz)
74 /// // prints "Foo { bar: 10, baz: "Hello World" }"
75 /// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
77 #[must_use = "must eventually call `finish()` on Debug builders"]
78 #[allow(missing_debug_implementations)]
79 #[stable(feature = "debug_builders", since = "1.2.0")]
80 pub struct DebugStruct<'a, 'b: 'a> {
81 fmt: &'a mut fmt::Formatter<'b>,
86 pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
88 -> DebugStruct<'a, 'b> {
89 let result = fmt.write_str(name);
97 impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
98 /// Adds a new field to the generated struct output.
99 #[stable(feature = "debug_builders", since = "1.2.0")]
100 pub fn field(&mut self, name: &str, value: &dyn fmt::Debug) -> &mut DebugStruct<'a, 'b> {
101 self.result = self.result.and_then(|_| {
102 let prefix = if self.has_fields {
108 if self.is_pretty() {
110 let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
111 writer.write_str(prefix)?;
112 writer.write_str("\n")?;
113 writer.write_str(name)?;
114 writer.write_str(": ")?;
115 value.fmt(&mut writer)
117 write!(self.fmt, "{} {}: ", prefix, name)?;
122 self.has_fields = true;
126 /// Finishes output and returns any error encountered.
127 #[stable(feature = "debug_builders", since = "1.2.0")]
128 pub fn finish(&mut self) -> fmt::Result {
130 self.result = self.result.and_then(|_| {
131 if self.is_pretty() {
132 self.fmt.write_str("\n}")
134 self.fmt.write_str(" }")
141 fn is_pretty(&self) -> bool {
146 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
148 /// This is useful when you wish to output a formatted tuple as a part of your
149 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
151 /// This can be constructed by the
152 /// [`Formatter::debug_tuple`](struct.Formatter.html#method.debug_tuple)
160 /// struct Foo(i32, String);
162 /// impl fmt::Debug for Foo {
163 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
164 /// fmt.debug_tuple("Foo")
171 /// // prints "Foo(10, "Hello World")"
172 /// println!("{:?}", Foo(10, "Hello World".to_string()));
174 #[must_use = "must eventually call `finish()` on Debug builders"]
175 #[allow(missing_debug_implementations)]
176 #[stable(feature = "debug_builders", since = "1.2.0")]
177 pub struct DebugTuple<'a, 'b: 'a> {
178 fmt: &'a mut fmt::Formatter<'b>,
184 pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
185 let result = fmt.write_str(name);
190 empty_name: name.is_empty(),
194 impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
195 /// Adds a new field to the generated tuple struct output.
196 #[stable(feature = "debug_builders", since = "1.2.0")]
197 pub fn field(&mut self, value: &dyn fmt::Debug) -> &mut DebugTuple<'a, 'b> {
198 self.result = self.result.and_then(|_| {
199 let (prefix, space) = if self.fields > 0 {
205 if self.is_pretty() {
207 let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
208 writer.write_str(prefix)?;
209 writer.write_str("\n")?;
210 value.fmt(&mut writer)
212 self.fmt.write_str(prefix)?;
213 self.fmt.write_str(space)?;
222 /// Finishes output and returns any error encountered.
223 #[stable(feature = "debug_builders", since = "1.2.0")]
224 pub fn finish(&mut self) -> fmt::Result {
226 self.result = self.result.and_then(|_| {
227 if self.is_pretty() {
228 self.fmt.write_str("\n")?;
230 if self.fields == 1 && self.empty_name {
231 self.fmt.write_str(",")?;
233 self.fmt.write_str(")")
239 fn is_pretty(&self) -> bool {
244 struct DebugInner<'a, 'b: 'a> {
245 fmt: &'a mut fmt::Formatter<'b>,
250 impl<'a, 'b: 'a> DebugInner<'a, 'b> {
251 fn entry(&mut self, entry: &dyn fmt::Debug) {
252 self.result = self.result.and_then(|_| {
253 if self.is_pretty() {
255 let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
256 writer.write_str(if self.has_fields {
261 entry.fmt(&mut writer)
264 self.fmt.write_str(", ")?
270 self.has_fields = true;
273 pub fn finish(&mut self) {
274 let prefix = if self.is_pretty() && self.has_fields {
279 self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
282 fn is_pretty(&self) -> bool {
287 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
289 /// This is useful when you wish to output a formatted set of items as a part
290 /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
292 /// This can be constructed by the
293 /// [`Formatter::debug_set`](struct.Formatter.html#method.debug_set)
301 /// struct Foo(Vec<i32>);
303 /// impl fmt::Debug for Foo {
304 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
305 /// fmt.debug_set().entries(self.0.iter()).finish()
309 /// // prints "{10, 11}"
310 /// println!("{:?}", Foo(vec![10, 11]));
312 #[must_use = "must eventually call `finish()` on Debug builders"]
313 #[allow(missing_debug_implementations)]
314 #[stable(feature = "debug_builders", since = "1.2.0")]
315 pub struct DebugSet<'a, 'b: 'a> {
316 inner: DebugInner<'a, 'b>,
319 pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
320 let result = write!(fmt, "{{");
330 impl<'a, 'b: 'a> DebugSet<'a, 'b> {
331 /// Adds a new entry to the set output.
332 #[stable(feature = "debug_builders", since = "1.2.0")]
333 pub fn entry(&mut self, entry: &dyn fmt::Debug) -> &mut DebugSet<'a, 'b> {
334 self.inner.entry(entry);
338 /// Adds the contents of an iterator of entries to the set output.
339 #[stable(feature = "debug_builders", since = "1.2.0")]
340 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
342 I: IntoIterator<Item = D>
344 for entry in entries {
350 /// Finishes output and returns any error encountered.
351 #[stable(feature = "debug_builders", since = "1.2.0")]
352 pub fn finish(&mut self) -> fmt::Result {
354 self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
358 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
360 /// This is useful when you wish to output a formatted list of items as a part
361 /// of your [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
363 /// This can be constructed by the
364 /// [`Formatter::debug_list`](struct.Formatter.html#method.debug_list)
372 /// struct Foo(Vec<i32>);
374 /// impl fmt::Debug for Foo {
375 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
376 /// fmt.debug_list().entries(self.0.iter()).finish()
380 /// // prints "[10, 11]"
381 /// println!("{:?}", Foo(vec![10, 11]));
383 #[must_use = "must eventually call `finish()` on Debug builders"]
384 #[allow(missing_debug_implementations)]
385 #[stable(feature = "debug_builders", since = "1.2.0")]
386 pub struct DebugList<'a, 'b: 'a> {
387 inner: DebugInner<'a, 'b>,
390 pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
391 let result = write!(fmt, "[");
401 impl<'a, 'b: 'a> DebugList<'a, 'b> {
402 /// Adds a new entry to the list output.
403 #[stable(feature = "debug_builders", since = "1.2.0")]
404 pub fn entry(&mut self, entry: &dyn fmt::Debug) -> &mut DebugList<'a, 'b> {
405 self.inner.entry(entry);
409 /// Adds the contents of an iterator of entries to the list output.
410 #[stable(feature = "debug_builders", since = "1.2.0")]
411 pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
413 I: IntoIterator<Item = D>
415 for entry in entries {
421 /// Finishes output and returns any error encountered.
422 #[stable(feature = "debug_builders", since = "1.2.0")]
423 pub fn finish(&mut self) -> fmt::Result {
425 self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
429 /// A struct to help with [`fmt::Debug`](trait.Debug.html) implementations.
431 /// This is useful when you wish to output a formatted map as a part of your
432 /// [`Debug::fmt`](trait.Debug.html#tymethod.fmt) implementation.
434 /// This can be constructed by the
435 /// [`Formatter::debug_map`](struct.Formatter.html#method.debug_map)
443 /// struct Foo(Vec<(String, i32)>);
445 /// impl fmt::Debug for Foo {
446 /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
447 /// fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish()
451 /// // prints "{"A": 10, "B": 11}"
452 /// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
454 #[must_use = "must eventually call `finish()` on Debug builders"]
455 #[allow(missing_debug_implementations)]
456 #[stable(feature = "debug_builders", since = "1.2.0")]
457 pub struct DebugMap<'a, 'b: 'a> {
458 fmt: &'a mut fmt::Formatter<'b>,
463 pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
464 let result = write!(fmt, "{{");
472 impl<'a, 'b: 'a> DebugMap<'a, 'b> {
473 /// Adds a new entry to the map output.
474 #[stable(feature = "debug_builders", since = "1.2.0")]
475 pub fn entry(&mut self, key: &dyn fmt::Debug, value: &dyn fmt::Debug) -> &mut DebugMap<'a, 'b> {
476 self.result = self.result.and_then(|_| {
477 if self.is_pretty() {
479 let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot);
480 writer.write_str(if self.has_fields {
485 key.fmt(&mut writer)?;
486 writer.write_str(": ")?;
487 value.fmt(&mut writer)
490 self.fmt.write_str(", ")?
493 self.fmt.write_str(": ")?;
498 self.has_fields = true;
502 /// Adds the contents of an iterator of entries to the map output.
503 #[stable(feature = "debug_builders", since = "1.2.0")]
504 pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
507 I: IntoIterator<Item = (K, V)>
509 for (k, v) in entries {
515 /// Finishes output and returns any error encountered.
516 #[stable(feature = "debug_builders", since = "1.2.0")]
517 pub fn finish(&mut self) -> fmt::Result {
518 let prefix = if self.is_pretty() && self.has_fields {
523 self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
526 fn is_pretty(&self) -> bool {