| 1 | # What `#[derive(Debug)]` generates |
| 2 | |
| 3 | This derive macro is a clever superset of `Debug` from standard library. Additional features include: |
| 4 | - not imposing redundant trait bounds; |
| 5 | - `#[debug(skip)]` (or `#[debug(ignore)]`) attribute to skip formatting struct field or enum variant; |
| 6 | - `#[debug("...", args...)]` to specify custom formatting either for the whole struct or enum variant, or its particular field; |
| 7 | - `#[debug(bounds(...))]` to impose additional custom trait bounds. |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | ## The format of the format |
| 13 | |
| 14 | You supply a format by placing an attribute on a struct or enum variant, or its particular field: |
| 15 | `#[debug("...", args...)]`. The format is exactly like in [`format!()`] or any other [`format_args!()`]-based macros. |
| 16 | |
| 17 | The variables available in the arguments is `self` and each member of the |
| 18 | struct or enum variant, with members of tuple structs being named with a |
| 19 | leading underscore and their index, i.e. `_0`, `_1`, `_2`, etc. Due to |
| 20 | ownership/lifetime limitations the member variables are all references to the |
| 21 | fields, except when used directly in the format string. For most purposes this |
| 22 | detail doesn't matter, but it is quite important when using `Pointer` |
| 23 | formatting. If you don't use the `{field:p}` syntax, you have to dereference |
| 24 | once to get the address of the field itself, instead of the address of the |
| 25 | reference to the field: |
| 26 | |
| 27 | ```rust |
| 28 | use derive_more::Debug; |
| 29 | |
| 30 | #[derive(Debug)] |
| 31 | #[debug("{field:p} {:p}", *field)] |
| 32 | struct RefInt<'a> { |
| 33 | field: &'a i32, |
| 34 | } |
| 35 | |
| 36 | let a = &123; |
| 37 | assert_eq!(format!("{:?}", RefInt{field: &a}), format!("{a:p} {:p}", a)); |
| 38 | ``` |
| 39 | |
| 40 | |
| 41 | ### Generic data types |
| 42 | |
| 43 | When deriving `Debug` for a generic struct/enum, all generic type arguments _used_ during formatting |
| 44 | are bound by respective formatting trait. |
| 45 | |
| 46 | E.g., for a structure `Foo` defined like this: |
| 47 | ```rust |
| 48 | use derive_more::Debug; |
| 49 | |
| 50 | #[derive(Debug)] |
| 51 | struct Foo<'a, T1, T2: Trait, T3, T4> { |
| 52 | #[debug("{a}")] |
| 53 | a: T1, |
| 54 | #[debug("{b}")] |
| 55 | b: <T2 as Trait>::Type, |
| 56 | #[debug("{c:?}")] |
| 57 | c: Vec<T3>, |
| 58 | #[debug("{d:p}")] |
| 59 | d: &'a T1, |
| 60 | #[debug(skip)] // or #[debug(ignore)] |
| 61 | e: T4, |
| 62 | } |
| 63 | |
| 64 | trait Trait { type Type; } |
| 65 | ``` |
| 66 | |
| 67 | The following where clauses would be generated: |
| 68 | - `T1: Display` |
| 69 | - `<T2 as Trait>::Type: Display` |
| 70 | - `Vec<T3>: Debug` |
| 71 | - `&'a T1: Pointer` |
| 72 | |
| 73 | |
| 74 | ### Custom trait bounds |
| 75 | |
| 76 | Sometimes you may want to specify additional trait bounds on your generic type parameters, so that they could be used |
| 77 | during formatting. This can be done with a `#[debug(bound(...))]` attribute. |
| 78 | |
| 79 | `#[debug(bound(...))]` accepts code tokens in a format similar to the format used in angle bracket list (or `where` |
| 80 | clause predicates): `T: MyTrait, U: Trait1 + Trait2`. |
| 81 | |
| 82 | Using `#[debug("...", ...)]` formatting we'll try our best to infer trait bounds, but in more advanced cases this isn't |
| 83 | possible. Our aim is to avoid imposing additional bounds, as they can be added with `#[debug(bound(...))]`. |
| 84 | In the example below, we can infer only that `V: Display`, other bounds have to be supplied by the user: |
| 85 | |
| 86 | ```rust |
| 87 | use std::fmt::Display; |
| 88 | use derive_more::Debug; |
| 89 | |
| 90 | #[derive(Debug)] |
| 91 | #[debug(bound(T: MyTrait, U: Display))] |
| 92 | struct MyStruct<T, U, V, F> { |
| 93 | #[debug("{}", a.my_function())] |
| 94 | a: T, |
| 95 | #[debug("{}", b.to_string().len())] |
| 96 | b: U, |
| 97 | #[debug("{c}")] |
| 98 | c: V, |
| 99 | #[debug(skip)] // or #[debug(ignore)] |
| 100 | d: F, |
| 101 | } |
| 102 | |
| 103 | trait MyTrait { fn my_function(&self) -> i32; } |
| 104 | ``` |
| 105 | |
| 106 | |
| 107 | ### Transparency |
| 108 | |
| 109 | If the top-level `#[debug("...", args...)]` attribute (the one for a whole struct or variant) is specified |
| 110 | and can be trivially substituted with a transparent delegation call to the inner type, then all the additional |
| 111 | [formatting parameters][1] do work as expected: |
| 112 | ```rust |
| 113 | use derive_more::Debug; |
| 114 | |
| 115 | #[derive(Debug)] |
| 116 | #[debug("{_0:o}")] // the same as calling `Octal::fmt()` |
| 117 | struct MyOctalInt(i32); |
| 118 | |
| 119 | // so, additional formatting parameters do work transparently |
| 120 | assert_eq!(format!("{:03?}", MyOctalInt(9)), "011"); |
| 121 | |
| 122 | #[derive(Debug)] |
| 123 | #[debug("{_0:02b}")] // cannot be trivially substituted with `Binary::fmt()`, |
| 124 | struct MyBinaryInt(i32); // because of specified formatting parameters |
| 125 | |
| 126 | // so, additional formatting parameters have no effect |
| 127 | assert_eq!(format!("{:07?}", MyBinaryInt(2)), "10"); |
| 128 | ``` |
| 129 | |
| 130 | If, for some reason, transparency in trivial cases is not desired, it may be suppressed explicitly |
| 131 | either with the [`format_args!()`] macro usage: |
| 132 | ```rust |
| 133 | use derive_more::Debug; |
| 134 | |
| 135 | #[derive(Debug)] |
| 136 | #[debug("{}", format_args!("{_0:o}"))] // `format_args!()` obscures the inner type |
| 137 | struct MyOctalInt(i32); |
| 138 | |
| 139 | // so, additional formatting parameters have no effect |
| 140 | assert_eq!(format!("{:07?}", MyOctalInt(9)), "11"); |
| 141 | ``` |
| 142 | Or by adding [formatting parameters][1] which cause no visual effects: |
| 143 | ```rust |
| 144 | use derive_more::Debug; |
| 145 | |
| 146 | #[derive(Debug)] |
| 147 | #[debug("{_0:^o}")] // `^` is centering, but in absence of additional width has no effect |
| 148 | struct MyOctalInt(i32); |
| 149 | |
| 150 | // and so, additional formatting parameters have no effect |
| 151 | assert_eq!(format!("{:07?}", MyOctalInt(9)), "11"); |
| 152 | ``` |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | ## Example usage |
| 158 | |
| 159 | ```rust |
| 160 | use std::path::PathBuf; |
| 161 | use derive_more::Debug; |
| 162 | |
| 163 | #[derive(Debug)] |
| 164 | struct MyInt(i32); |
| 165 | |
| 166 | #[derive(Debug)] |
| 167 | struct MyIntHex(#[debug("{_0:x}")] i32); |
| 168 | |
| 169 | #[derive(Debug)] |
| 170 | #[debug("{_0} = {_1}")] |
| 171 | struct StructFormat(&'static str, u8); |
| 172 | |
| 173 | #[derive(Debug)] |
| 174 | enum E { |
| 175 | Skipped { |
| 176 | x: u32, |
| 177 | #[debug(skip)] // or #[debug(ignore)] |
| 178 | y: u32, |
| 179 | }, |
| 180 | Binary { |
| 181 | #[debug("{i:b}")] |
| 182 | i: i8, |
| 183 | }, |
| 184 | Path(#[debug("{}", _0.display())] PathBuf), |
| 185 | #[debug("{_0}")] |
| 186 | EnumFormat(bool) |
| 187 | } |
| 188 | |
| 189 | assert_eq!(format!("{:?}", MyInt(-2)), "MyInt(-2)"); |
| 190 | assert_eq!(format!("{:?}", MyIntHex(-255)), "MyIntHex(ffffff01)"); |
| 191 | assert_eq!(format!("{:?}", StructFormat("answer", 42)), "answer = 42"); |
| 192 | assert_eq!(format!("{:?}", E::Skipped { x: 10, y: 20 }), "Skipped { x: 10, .. }"); |
| 193 | assert_eq!(format!("{:?}", E::Binary { i: -2 }), "Binary { i: 11111110 }"); |
| 194 | assert_eq!(format!("{:?}", E::Path("abc".into())), "Path(abc)"); |
| 195 | assert_eq!(format!("{:?}", E::EnumFormat(true)), "true"); |
| 196 | ``` |
| 197 | |
| 198 | [`format!()`]: https://doc.rust-lang.org/stable/std/macro.format.html |
| 199 | [`format_args!()`]: https://doc.rust-lang.org/stable/std/macro.format_args.html |
| 200 | |
| 201 | [1]: https://doc.rust-lang.org/stable/std/fmt/index.html#formatting-parameters |