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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! Lets you derive [`fmt`](::core::fmt) traits on types wrapping types that already implement them.
//!
//! [](https://github.com/Alorel/delegate-display-rs/actions/workflows/test.yml?query=branch%3Amaster)
//! [](https://crates.io/crates/delegate-display)
//! [](https://coveralls.io/github/Alorel/delegate-display-rs?branch=master)
//! [](https://libraries.io/cargo/delegate-display)
//!
//! # Examples
//! <details><summary>Newtype structs</summary>
//!
//! ```
//! # use delegate_display::*;
//! struct SomeType;
//! impl core::fmt::Display for SomeType {
//! fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
//! f.write_str(">foo<")
//! }
//! }
//!
//! #[derive(DelegateDisplay)]
//! struct Foo(SomeType);
//!
//! assert_eq!(format!("{}", Foo(SomeType)), ">foo<");
//! ```
//!
//! </details>
//! <details><summary>Structs with 0..=1 fields</summary>
//!
//! ```
//! # use delegate_display::*;
//! struct SomeType;
//! impl core::fmt::Debug for SomeType {
//! fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
//! f.write_str(">foo<")
//! }
//! }
//!
//! #[derive(DelegateDebug)]
//! struct Foo { some_field: SomeType }
//!
//! assert_eq!(format!("{:?}", Foo { some_field: SomeType }), ">foo<");
//! ```
//!
//! </details>
//! <details><summary>Enums with 0..=1 variants each</summary>
//!
//! ```
//! # use delegate_display::*;
//! struct SomeType;
//! struct AnotherType;
//!
//! impl core::fmt::Display for SomeType {
//! fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
//! f.write_str(">foo<")
//! }
//! }
//! impl core::fmt::Display for AnotherType {
//! fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
//! f.write_str(">bar<")
//! }
//! }
//!
//! #[derive(DelegateDisplay)]
//! enum MyEnum {
//! Foo,
//! Bar(SomeType),
//! Qux { baz: AnotherType }
//! }
//!
//! assert_eq!(format!("{}", MyEnum::Bar(SomeType)), ">foo<");
//! assert_eq!(format!("{}", MyEnum::Qux { baz: AnotherType }), ">bar<");
//! ```
//!
//! </details>
//! <details><summary>Generics</summary>
//!
//! Generics are handled automatically for you.
//!
//! ```
//! # use delegate_display::*;
//! #
//! #[derive(DelegateDisplay)]
//! struct MyStruct<T>(T);
//!
//! #[derive(DelegateDisplay)]
//! enum MyEnum<A, B> {
//! A(A),
//! B { value: B },
//! }
//!
//! assert_eq!(format!("{}", MyStruct(50)), "50");
//! assert_eq!(format!("{}", MyEnum::<u8, i8>::A(75)), "75");
//! assert_eq!(format!("{}", MyEnum::<u8, i8>::B { value: -1 }), "-1");
//! ```
//!
//! </details>
//! <details><summary>Structs & enums with 2+ fields</summary>
//!
//! The field being delegated to must be marked with the appropriate attribute.
//!
//! ```
//! # use delegate_display::*;
//!
//! #[derive(DelegateDisplay)]
//! struct MyStruct<T> {
//! label: String,
//! #[ddisplay]
//! value: T,
//! }
//!
//! #[derive(DelegateDebug)]
//! enum MyEnum {
//! Foo(#[ddebug] String, u8),
//! Bar { baz: u8, #[ddebug] qux: u8 }
//! }
//!
//! let my_struct = MyStruct { label: "foo".into(), value: 42 };
//! assert_eq!(format!("{}", my_struct), "42");
//!
//! let my_enum = MyEnum::Foo(".".into(), 1);
//! assert_eq!(format!("{:?}", my_enum), "\".\"");
//!
//! let my_enum = MyEnum::Bar { baz: 2, qux: 3 };
//! assert_eq!(format!("{:?}", my_enum), "3");
//! ```
//!
//! </details>
//! <details><summary>Empty structs</summary>
//!
//! ```
//! # use delegate_display::*;
//! #
//! #[derive(DelegateDebug, DelegateDisplay)]
//! struct Foo;
//!
//! #[derive(DelegateDebug, DelegateDisplay)]
//! struct Bar{}
//!
//! #[derive(DelegateDebug, DelegateDisplay)]
//! struct Qux();
//!
//! assert_eq!(format!("{}-{:?}", Foo, Foo), "-");
//! assert_eq!(format!("{}-{:?}", Bar{}, Bar{}), "-");
//! assert_eq!(format!("{}-{:?}", Qux(), Qux()), "-");
//! ```
//!
//! </details>
//! <details><summary>Typed delegations</summary>
//!
//! Can be useful for further prettifying the output.
//!
//! ```
//! # use delegate_display::*;
//! #
//! /// Some type that `Deref`s to the type we want to use in our formatting, in this case, `str`.
//! #[derive(Debug)]
//! struct Wrapper(&'static str);
//! impl std::ops::Deref for Wrapper {
//! type Target = str;
//! fn deref(&self) -> &Self::Target {
//! self.0
//! }
//! }
//!
//! #[derive(DelegateDebug)]
//! #[ddebug(delegate_to(str))] // ignore `Wrapper` and debug the `str` it `Deref`s instead
//! struct Typed(Wrapper);
//!
//! #[derive(DelegateDebug)] // Included for comparison
//! struct Base(Wrapper);
//!
//! assert_eq!(format!("{:?}", Typed(Wrapper("foo"))), "\"foo\"");
//! assert_eq!(format!("{:?}", Base(Wrapper("bar"))), "Wrapper(\"bar\")");
//! ```
//!
//! </details>
//! <details><summary>Custom generic bounds</summary>
//!
//! ```
//! # use delegate_display::*;
//! # use core::fmt::{Display, Formatter, self};
//! # use std::ops::Deref;
//! #
//! struct CopyDisplayable<T>(T); // Implements Deref
//! # impl<T> Deref for CopyDisplayable<T> {
//! # type Target = T;
//! # fn deref(&self) -> &Self::Target {
//! # &self.0
//! # }
//! # }
//!
//! impl<T: Copy> Display for CopyDisplayable<T> {
//! fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
//! unimplemented!("Nonsense generic bound - base bounds don't work.");
//! }
//! }
//!
//! // Without these options the implementation would have a predicate of `CopyDisplayable<T>: Debug` which would
//! // effectively mean `T: Copy`; we can transform it to `T: Display` because `CopyDisplayable` derefs to `T`.
//! #[derive(DelegateDisplay)]
//! #[ddisplay(bounds(T: Display), delegate_to(T))]
//! struct Displayable<T>(CopyDisplayable<T>);
//!
//! let dbg = Displayable::<String>(CopyDisplayable("cdbg".into()));
//! assert_eq!(format!("{}", dbg), "cdbg");
//! ```
//!
//! </details>
//! <details><summary>Multiple traits at once</summary>
//!
//! Instead of re-parsing your struct/enum multiple times, you can instead derive `DelegateFmt`.
//! It supports every individual macro's attribute along with `dany` as a catch-all default.
//!
//! ```
//! # use delegate_display::*;
//! #
//! struct Wrapper(u8); // implements Deref
//! # impl ::std::ops::Deref for Wrapper {
//! # type Target = u8;
//! # fn deref(&self) -> &Self::Target {
//! # &self.0
//! # }
//! # }
//!
//! #[derive(DelegateFmt)]
//! #[dfmt(dany(delegate_to(u8)), ddebug, ddisplay, dbinary)]
//! struct MyStruct(#[dany] Wrapper, #[dbinary] Wrapper);
//! # impl MyStruct {
//! # fn new(a: u8, b: u8) -> Self {
//! # Self(Wrapper(a), Wrapper(b))
//! # }
//! # }
//!
//! assert_eq!(format!("{:?}", MyStruct::new(1, 2)), "1");
//! assert_eq!(format!("{}", MyStruct::new(3, 4)), "3");
//! assert_eq!(format!("{:b}", MyStruct::new(5, 6)), "110");
//! ```
//!
//! </details>
//! <details><summary>Invalid inputs</summary>
//!
//! ```compile_fail
//! #[derive(delegate_display::DelegateDebug)]
//! struct TooManyFields1 {
//! foo: u8,
//! bar: u8, // No fields marked with `#[ddebug]` or `#[dany]`
//! }
//! ```
//!
//! ```compile_fail
//! #[derive(delegate_display::DelegateDebug)]
//! struct TooManyFields2(u8, u8); // No fields marked with `#[ddebug]` or `#[dany]`
//! ```
//!
//! ```compile_fail
//! #[derive(delegate_display::DelegateDebug)]
//! enum SomeEnum {
//! A, // this is ok
//! B(u8), // this is ok
//! C { foo: u8 }, // this is ok
//! D(u8, u8), // ERR: No fields marked with `#[ddebug]` or `#[dany]`
//! E { foo: u8, bar: u8 } // ERR: No fields marked with `#[ddebug]` or `#[dany]`
//! }
//! ```
//!
//! ```compile_fail
//! #[derive(delegate_display::DelegateDebug)]
//! union Foo { bar: u8 } // Unions are not supported
//! ```
//!
//! ```compile_fail
//! # use delegate_display::*;
//! #
//! struct NonDebug;
//!
//! #[derive(DelegateDebug)]
//! struct Foo<A, B>(A, B);
//!
//! format!("{:?}", Foo(NonDebug, 1)); // NonDebug does not implement Debug
//! ```
//!
//! </details>
const ATTR_ANY: &str = "dany";
const ATTR_FMT: &str = "dfmt";
$+
};
}
alias!