1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! This crate exports the [`ToDebug`] trait, which is an alternative to
//! [`ToString`] that uses [`Debug`] instead of [`Display`].
//!
//! This can be useful for writing doctests, as it allows you to inspect
//! the values of private fields:
//!
//! ```
//! # use to_debug::ToDebug;
//! mod private {
//! #[derive(Debug)]
//! pub struct Person { name: String, age: u16 }
//! // constructor boilerplate...
//! # impl Person {
//! # pub fn new(name: impl Into<String>, age: u16) -> Self {
//! # Self { name: name.into(), age }
//! # }
//! # }
//! }
//! let p = private::Person::new("Joseph", 20);
//! // assert_eq!(p.name, "Joseph"); // This would fail since `name` is private.
//! assert_eq!(p.to_debug(), r#"Person { name: "Joseph", age: 20 }"#);
//! ```
//!
//! [`Debug`]: core::fmt::Debug
//! [`Display`]: core::fmt::Display
use fmt;
/// A trait for converting a value to a `String` using the [`Debug`] trait.
///
/// This trait is automatically implemented for any type which implements the
/// [`Debug`] trait. As such, `ToDebug` shouldn’t be implemented directly:
/// [`Debug`] should be implemented instead, and you get the `ToDebug`
/// implementation for free.
///
/// [`Debug`]: core::fmt::Debug