Debian Codegraph @

debian/missing-sources/derive_more/tests/debug.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![cfg_attr(nightly, feature(never_type))]
3 #![allow(dead_code)] // some code is tested for type checking only
4
5 #[cfg(not(feature = "std"))]
6 extern crate alloc;
7
8 mod structs {
9 mod unit {
10 #[cfg(not(feature = "std"))]
11 use alloc::format;
12
13 use derive_more::Debug;
14
15 #[derive(Debug)]
16 struct Unit;
17
18 #[derive(Debug)]
19 struct Tuple();
20
21 #[derive(Debug)]
22 struct Struct {}
23
24 #[test]
25 fn assert() {
26 assert_eq!(format!("{Unit:?}"), "Unit");
27 assert_eq!(format!("{Unit:#?}",), "Unit");
28 assert_eq!(format!("{:?}", Tuple()), "Tuple");
29 assert_eq!(format!("{:#?}", Tuple()), "Tuple");
30 assert_eq!(format!("{:?}", Struct {}), "Struct");
31 assert_eq!(format!("{:#?}", Struct {}), "Struct");
32 }
33
34 mod interpolated_struct {
35 #[cfg(not(feature = "std"))]
36 use alloc::format;
37
38 use derive_more::Debug;
39
40 #[derive(Debug)]
41 #[debug("Format String")]
42 struct Unit;
43
44 #[test]
45 fn assert() {
46 assert_eq!(format!("{Unit:?}"), "Format String");
47 }
48 }
49
50 mod transparency {
51 #[cfg(not(feature = "std"))]
52 use alloc::format;
53
54 use derive_more::Debug;
55
56 const I32: i32 = 11;
57 const F64: f64 = 3.15;
58 const POINTER: &f64 = &3.15;
59
60 #[derive(Debug)]
61 #[debug("{I32}")]
62 struct Display;
63
64 #[derive(Debug)]
65 #[debug("{I32:?}")]
66 struct StructDebug;
67
68 #[derive(Debug)]
69 #[debug("{:b}", I32)]
70 struct Binary;
71
72 #[derive(Debug)]
73 #[debug("{0:o}", I32)]
74 struct Octal;
75
76 #[derive(Debug)]
77 #[debug("{I32:x}")]
78 struct LowerHex;
79
80 #[derive(Debug)]
81 #[debug("{:X}", I32)]
82 struct UpperHex;
83
84 #[derive(Debug)]
85 #[debug("{F64:e}")]
86 struct LowerExp;
87
88 #[derive(Debug)]
89 #[debug("{named:E}", named = F64)]
90 struct UpperExp;
91
92 #[derive(Debug)]
93 #[debug("{POINTER:p}")]
94 struct Pointer;
95
96 #[test]
97 fn assert() {
98 assert_eq!(format!("{Display:03?}"), "011");
99 assert_eq!(format!("{StructDebug:03?}"), "011");
100 assert_eq!(format!("{Binary:07?}"), "0001011");
101 assert_eq!(format!("{Octal:07?}"), "0000013");
102 assert_eq!(format!("{LowerHex:03?}"), "00b");
103 assert_eq!(format!("{UpperHex:03?}"), "00B");
104 assert_eq!(format!("{LowerExp:07?}"), "03.15e0");
105 assert_eq!(format!("{UpperExp:07?}"), "03.15E0");
106 assert_eq!(format!("{Pointer:018?}"), format!("{POINTER:018p}"));
107 }
108
109 mod omitted {
110 mod on_modifiers {
111 #[cfg(not(feature = "std"))]
112 use alloc::format;
113
114 use derive_more::Debug;
115
116 const I32: i32 = 11;
117 const F64: f64 = 3.15;
118
119 #[derive(Debug)]
120 #[debug("{I32:x?}")]
121 struct LowerDebug;
122
123 #[derive(Debug)]
124 #[debug("{I32:X?}")]
125 struct UpperDebug;
126
127 #[derive(Debug)]
128 #[debug("{:^}", I32)]
129 struct Align;
130
131 #[derive(Debug)]
132 #[debug("{:+}", I32)]
133 struct Sign;
134
135 #[derive(Debug)]
136 #[debug("{:#b}", I32)]
137 struct Alternate;
138
139 #[derive(Debug)]
140 #[debug("{:0}", I32)]
141 struct ZeroPadded;
142
143 #[derive(Debug)]
144 #[debug("{:07}", I32)]
145 struct Width;
146
147 #[derive(Debug)]
148 #[debug("{:.1}", F64)]
149 struct Precision;
150
151 #[test]
152 fn assert() {
153 assert_eq!(format!("{LowerDebug:03?}"), "b");
154 assert_eq!(format!("{UpperDebug:03?}"), "B");
155 assert_eq!(format!("{Align:03?}"), "11");
156 assert_eq!(format!("{Sign:04?}"), "+11");
157 assert_eq!(format!("{Alternate:07?}"), "0b1011");
158 assert_eq!(format!("{ZeroPadded:07?}"), "11");
159 assert_eq!(format!("{Width:03?}"), "0000011");
160 assert_eq!(format!("{Precision:.3?}"), "3.1");
161 }
162 }
163 }
164 }
165 }
166
167 mod single_field {
168 #[cfg(not(feature = "std"))]
169 use alloc::format;
170
171 use derive_more::Debug;
172
173 #[derive(Debug)]
174 struct Tuple(i32);
175
176 #[derive(Debug)]
177 struct Struct {
178 field: i32,
179 }
180
181 #[test]
182 fn assert() {
183 assert_eq!(format!("{:?}", Tuple(0)), "Tuple(0)");
184 assert_eq!(format!("{:#?}", Tuple(0)), "Tuple(\n 0,\n)");
185 assert_eq!(format!("{:?}", Struct { field: 0 }), "Struct { field: 0 }");
186 assert_eq!(
187 format!("{:#?}", Struct { field: 0 }),
188 "Struct {\n field: 0,\n}",
189 );
190 }
191
192 mod str_field {
193 #[cfg(not(feature = "std"))]
194 use alloc::format;
195
196 use derive_more::Debug;
197
198 #[derive(Debug)]
199 struct Tuple(#[debug("i32")] i32);
200
201 #[derive(Debug)]
202 struct Struct {
203 #[debug("i32")]
204 field: i32,
205 }
206
207 #[test]
208 fn assert() {
209 assert_eq!(format!("{:?}", Tuple(0)), "Tuple(i32)");
210 assert_eq!(format!("{:#?}", Tuple(0)), "Tuple(\n i32,\n)");
211 assert_eq!(
212 format!("{:?}", Struct { field: 0 }),
213 "Struct { field: i32 }",
214 );
215 assert_eq!(
216 format!("{:#?}", Struct { field: 0 }),
217 "Struct {\n field: i32,\n}",
218 );
219 }
220 }
221
222 mod interpolated_field {
223 #[cfg(not(feature = "std"))]
224 use alloc::format;
225
226 use derive_more::Debug;
227
228 #[derive(Debug)]
229 struct Tuple(#[debug("{_0}.{}", _0)] i32);
230
231 #[derive(Debug)]
232 struct Struct {
233 #[debug("{field}.{}", field)]
234 field: i32,
235 }
236
237 #[test]
238 fn assert() {
239 assert_eq!(format!("{:?}", Tuple(0)), "Tuple(0.0)");
240 assert_eq!(format!("{:#?}", Tuple(0)), "Tuple(\n 0.0,\n)");
241 assert_eq!(
242 format!("{:?}", Struct { field: 0 }),
243 "Struct { field: 0.0 }",
244 );
245 assert_eq!(
246 format!("{:#?}", Struct { field: 0 }),
247 "Struct {\n field: 0.0,\n}",
248 );
249 }
250
251 mod pointer {
252 #[cfg(not(feature = "std"))]
253 use alloc::format;
254
255 use derive_more::Debug;
256
257 #[derive(Debug)]
258 struct Tuple<'a>(#[debug("{_0:p}.{:p}", self.0)] &'a i32);
259
260 #[derive(Debug)]
261 struct Struct<'a> {
262 #[debug("{field:p}.{:p}", self.field)]
263 field: &'a i32,
264 }
265
266 #[derive(Debug)]
267 #[debug("{_0:p}")]
268 struct TupleTransparent<'a>(&'a i32);
269
270 #[derive(Debug)]
271 #[debug("{field:p}")]
272 struct StructTransparent<'a> {
273 field: &'a i32,
274 }
275
276 #[test]
277 fn assert() {
278 let a = 42;
279 assert_eq!(
280 format!("{:?}", Tuple(&a)),
281 format!("Tuple({0:p}.{0:p})", &a),
282 );
283 assert_eq!(
284 format!("{:?}", Struct { field: &a }),
285 format!("Struct {{ field: {0:p}.{0:p} }}", &a),
286 );
287 assert_eq!(
288 format!("{:?}", TupleTransparent(&a)),
289 format!("{0:p}", &a),
290 );
291 assert_eq!(
292 format!("{:?}", StructTransparent { field: &a }),
293 format!("{0:p}", &a),
294 );
295 }
296 }
297 }
298
299 mod ignore {
300 #[cfg(not(feature = "std"))]
301 use alloc::format;
302
303 use derive_more::Debug;
304
305 #[derive(Debug)]
306 struct Tuple(#[debug(ignore)] i32);
307
308 #[derive(Debug)]
309 struct Struct {
310 #[debug(skip)]
311 field: i32,
312 }
313
314 #[test]
315 fn assert() {
316 assert_eq!(format!("{:?}", Tuple(0)), "Tuple(..)");
317 assert_eq!(format!("{:#?}", Tuple(0)), "Tuple(..)");
318 assert_eq!(format!("{:?}", Struct { field: 0 }), "Struct { .. }");
319 assert_eq!(format!("{:#?}", Struct { field: 0 }), "Struct { .. }");
320 }
321 }
322
323 mod transparency {
324 #[cfg(not(feature = "std"))]
325 use alloc::format;
326
327 use derive_more::Debug;
328
329 #[derive(Debug)]
330 #[debug("{_0:?}")]
331 struct TupleDebug(i32);
332
333 #[derive(Debug)]
334 #[debug("{}", field)]
335 struct StructDisplay {
336 field: i32,
337 }
338
339 #[test]
340 fn assert() {
341 assert_eq!(format!("{:03?}", TupleDebug(7)), "007");
342 assert_eq!(format!("{:03?}", StructDisplay { field: 7 }), "007");
343 }
344
345 mod suppressed {
346 #[cfg(not(feature = "std"))]
347 use alloc::format;
348
349 use derive_more::Debug;
350
351 #[derive(Debug)]
352 #[debug("{}", format_args!("{_0:?}"))]
353 struct TupleDebug(i32);
354
355 #[derive(Debug)]
356 #[debug("{}", format_args!("{}", field))]
357 struct StructDisplay {
358 field: i32,
359 }
360
361 #[test]
362 fn assert() {
363 assert_eq!(format!("{:03?}", TupleDebug(7)), "7");
364 assert_eq!(format!("{:03?}", StructDisplay { field: 7 }), "7");
365 }
366 }
367
368 mod omitted {
369 mod on_modifiers {
370 #[cfg(not(feature = "std"))]
371 use alloc::format;
372
373 use derive_more::Debug;
374
375 #[derive(Debug)]
376 #[debug("{_0:x?}")]
377 struct LowerDebug(i32);
378
379 #[derive(Debug)]
380 #[debug("{_0:X?}")]
381 struct UpperDebug(i32);
382
383 #[derive(Debug)]
384 #[debug("{:^}", _0)]
385 struct Align(i32);
386
387 #[derive(Debug)]
388 #[debug("{:+}", _0)]
389 struct Sign(i32);
390
391 #[derive(Debug)]
392 #[debug("{:#b}", _0)]
393 struct Alternate(i32);
394
395 #[derive(Debug)]
396 #[debug("{:0}", _0)]
397 struct ZeroPadded(i32);
398
399 #[derive(Debug)]
400 #[debug("{:07}", _0)]
401 struct Width(i32);
402
403 #[derive(Debug)]
404 #[debug("{:.5}", _0)]
405 struct Precision(f64);
406
407 #[test]
408 fn assert() {
409 assert_eq!(format!("{:03?}", LowerDebug(7)), "7");
410 assert_eq!(format!("{:03?}", UpperDebug(8)), "8");
411 assert_eq!(format!("{:03?}", Align(5)), "5");
412 assert_eq!(format!("{:03?}", Sign(5)), "+5");
413 assert_eq!(format!("{:07?}", Alternate(5)), "0b101");
414 assert_eq!(format!("{:07?}", ZeroPadded(-5)), "-5");
415 assert_eq!(format!("{:03?}", Width(5)), "0000005");
416 assert_eq!(format!("{:.3?}", Precision(1.23456789)), "1.23457");
417 }
418 }
419 }
420 }
421
422 mod r#unsized {
423 #[cfg(not(feature = "std"))]
424 use alloc::format;
425 use core::ptr;
426
427 use derive_more::Debug;
428
429 #[derive(Debug)]
430 struct Tuple(str);
431
432 #[derive(Debug)]
433 struct Struct {
434 tail: str,
435 }
436
437 #[test]
438 fn assert() {
439 let dat = "14";
440
441 let t =
442 unsafe { &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple) };
443 assert_eq!(format!("{t:?}"), r#"Tuple("14")"#);
444 let s =
445 unsafe { &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct) };
446 assert_eq!(format!("{s:?}"), r#"Struct { tail: "14" }"#);
447 }
448
449 mod interpolated {
450 #[cfg(not(feature = "std"))]
451 use alloc::format;
452 use core::ptr;
453
454 use derive_more::Debug;
455
456 #[derive(Debug)]
457 #[debug("{}.", _0)]
458 struct Tuple1(str);
459
460 #[derive(Debug)]
461 #[debug("{_0}.")]
462 struct Tuple2(str);
463
464 #[derive(Debug)]
465 #[debug("{}.", tail)]
466 struct Struct1 {
467 tail: str,
468 }
469
470 #[derive(Debug)]
471 #[debug("{tail}.")]
472 struct Struct2 {
473 tail: str,
474 }
475
476 #[test]
477 fn assert() {
478 let dat = "14";
479
480 let t1 = unsafe {
481 &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple1)
482 };
483 assert_eq!(format!("{t1:?}"), "14.");
484 let t2 = unsafe {
485 &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple2)
486 };
487 assert_eq!(format!("{t2:?}"), "14.");
488 let s1 = unsafe {
489 &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct1)
490 };
491 assert_eq!(format!("{s1:?}"), "14.");
492 let s2 = unsafe {
493 &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct2)
494 };
495 assert_eq!(format!("{s2:?}"), "14.");
496 }
497 }
498 }
499
500 #[cfg(nightly)]
501 mod never {
502 use derive_more::Debug;
503
504 #[derive(Debug)]
505 struct Tuple(!);
506
507 #[derive(Debug)]
508 struct Struct {
509 field: !,
510 }
511 }
512
513 mod deprecated {
514 use derive_more::Debug;
515
516 #[derive(Debug)]
517 #[deprecated(note = "struct")]
518 struct Tuple(#[deprecated(note = "field")] i32);
519
520 #[derive(Debug)]
521 #[deprecated(note = "struct")]
522 struct Struct {
523 #[deprecated(note = "field")]
524 field: i32,
525 }
526 }
527 }
528
529 mod multi_field {
530 #[cfg(not(feature = "std"))]
531 use alloc::format;
532
533 use derive_more::Debug;
534
535 #[derive(Debug)]
536 struct Tuple(i32, i32);
537
538 #[derive(Debug)]
539 struct Struct {
540 field1: i32,
541 field2: i32,
542 }
543
544 #[test]
545 fn assert() {
546 assert_eq!(format!("{:?}", Tuple(1, 2)), "Tuple(1, 2)");
547 assert_eq!(format!("{:#?}", Tuple(1, 2)), "Tuple(\n 1,\n 2,\n)");
548 assert_eq!(
549 format!(
550 "{:?}",
551 Struct {
552 field1: 1,
553 field2: 2,
554 }
555 ),
556 "Struct { field1: 1, field2: 2 }",
557 );
558 assert_eq!(
559 format!(
560 "{:#?}",
561 Struct {
562 field1: 1,
563 field2: 2,
564 }
565 ),
566 "Struct {\n field1: 1,\n field2: 2,\n}",
567 );
568 }
569
570 mod str_field {
571 #[cfg(not(feature = "std"))]
572 use alloc::format;
573
574 use derive_more::Debug;
575
576 #[derive(Debug)]
577 struct Tuple(i32, #[debug("i32")] i32);
578
579 #[derive(Debug)]
580 struct Struct {
581 #[debug("i32")]
582 field1: i32,
583 field2: i32,
584 }
585
586 #[test]
587 fn assert() {
588 assert_eq!(format!("{:?}", Tuple(1, 2)), "Tuple(1, i32)");
589 assert_eq!(
590 format!("{:#?}", Tuple(1, 2)),
591 "Tuple(\n 1,\n i32,\n)",
592 );
593 assert_eq!(
594 format!(
595 "{:?}",
596 Struct {
597 field1: 1,
598 field2: 2,
599 }
600 ),
601 "Struct { field1: i32, field2: 2 }",
602 );
603 assert_eq!(
604 format!(
605 "{:#?}",
606 Struct {
607 field1: 1,
608 field2: 2,
609 }
610 ),
611 "Struct {\n field1: i32,\n field2: 2,\n}",
612 );
613 }
614 }
615
616 mod interpolated_field {
617 #[cfg(not(feature = "std"))]
618 use alloc::format;
619
620 use derive_more::Debug;
621
622 #[derive(Debug)]
623 struct Tuple(i32, #[debug("{_0}.{}", _1)] i32);
624
625 #[derive(Debug)]
626 struct Struct {
627 #[debug("{field1}.{}", field2)]
628 field1: i32,
629 field2: i32,
630 }
631
632 #[test]
633 fn assert() {
634 assert_eq!(format!("{:?}", Tuple(1, 2)), "Tuple(1, 1.2)");
635 assert_eq!(
636 format!("{:#?}", Tuple(1, 2)),
637 "Tuple(\n 1,\n 1.2,\n)",
638 );
639 assert_eq!(
640 format!(
641 "{:?}",
642 Struct {
643 field1: 1,
644 field2: 2,
645 }
646 ),
647 "Struct { field1: 1.2, field2: 2 }",
648 );
649 assert_eq!(
650 format!(
651 "{:#?}",
652 Struct {
653 field1: 1,
654 field2: 2,
655 }
656 ),
657 "Struct {\n field1: 1.2,\n field2: 2,\n}",
658 );
659 }
660 }
661
662 mod interpolated_struct {
663 #[cfg(not(feature = "std"))]
664 use alloc::format;
665
666 use derive_more::Debug;
667
668 #[derive(Debug)]
669 #[debug("{_0} * {_1}")]
670 struct Tuple(u8, bool);
671
672 #[derive(Debug)]
673 #[debug("{a} * {b}")]
674 struct Struct {
675 a: u8,
676 b: bool,
677 }
678
679 #[test]
680 fn assert() {
681 assert_eq!(format!("{:?}", Tuple(10, true)), "10 * true");
682 assert_eq!(format!("{:?}", Struct { a: 10, b: true }), "10 * true");
683 }
684
685 mod pointer {
686 #[cfg(not(feature = "std"))]
687 use alloc::format;
688
689 use derive_more::Debug;
690
691 #[derive(Debug)]
692 #[debug("{_0:p} * {_1:p}")]
693 struct Tuple<'a, 'b>(&'a u8, &'b bool);
694
695 #[derive(Debug)]
696 #[debug("{a:p} * {b:p}")]
697 struct Struct<'a, 'b> {
698 a: &'a u8,
699 b: &'b bool,
700 }
701
702 #[test]
703 fn assert() {
704 let (a, b) = (10, true);
705 assert_eq!(
706 format!("{:?}", Tuple(&a, &b)),
707 format!("{:p} * {:p}", &a, &b),
708 );
709 assert_eq!(
710 format!("{:?}", Struct { a: &a, b: &b }),
711 format!("{:p} * {:p}", &a, &b),
712 );
713 }
714 }
715 }
716
717 mod ignore {
718 #[cfg(not(feature = "std"))]
719 use alloc::format;
720
721 use derive_more::Debug;
722
723 #[derive(Debug)]
724 struct Tuple(#[debug(ignore)] i32, i32);
725
726 #[derive(Debug)]
727 struct Struct {
728 field1: i32,
729 #[debug(skip)]
730 field2: i32,
731 }
732
733 #[test]
734 fn assert() {
735 assert_eq!(format!("{:?}", Tuple(1, 2)), "Tuple(2, ..)");
736 assert_eq!(format!("{:#?}", Tuple(1, 2)), "Tuple(\n 2,\n ..\n)",);
737 assert_eq!(
738 format!(
739 "{:?}",
740 Struct {
741 field1: 1,
742 field2: 2
743 }
744 ),
745 "Struct { field1: 1, .. }",
746 );
747 assert_eq!(
748 format!(
749 "{:#?}",
750 Struct {
751 field1: 1,
752 field2: 2
753 }
754 ),
755 "Struct {\n field1: 1,\n ..\n}",
756 );
757 }
758 }
759
760 mod transparency {
761 #[cfg(not(feature = "std"))]
762 use alloc::format;
763
764 use derive_more::Debug;
765
766 #[derive(Debug)]
767 #[debug("{0:o}", _0)]
768 struct TupleOctal(i32, i64);
769
770 #[derive(Debug)]
771 #[debug("{named:e}", named = b)]
772 struct StructLowerExp {
773 a: i32,
774 b: f64,
775 }
776
777 #[test]
778 fn assert() {
779 assert_eq!(format!("{:03?}", TupleOctal(9, 4)), "011");
780 assert_eq!(
781 format!("{:.1?}", StructLowerExp { a: 7, b: 3.15 }),
782 "3.1e0",
783 );
784 }
785 }
786
787 #[cfg(target_endian = "little")]
788 mod r#unsized {
789 #[cfg(not(feature = "std"))]
790 use alloc::format;
791 use core::ptr;
792
793 use derive_more::Debug;
794
795 #[derive(Debug)]
796 struct Tuple(char, str);
797
798 #[derive(Debug)]
799 struct Struct {
800 head: char,
801 tail: str,
802 }
803
804 #[test]
805 fn assert() {
806 let dat = [51i32, 3028017];
807
808 let t =
809 unsafe { &*(ptr::addr_of!(dat) as *const [i32] as *const Tuple) };
810 assert_eq!(format!("{t:?}"), r#"Tuple('3', "14")"#);
811 let s =
812 unsafe { &*(ptr::addr_of!(dat) as *const [i32] as *const Struct) };
813 assert_eq!(format!("{s:?}"), r#"Struct { head: '3', tail: "14" }"#);
814 }
815
816 mod interpolated {
817 #[cfg(not(feature = "std"))]
818 use alloc::format;
819 use core::ptr;
820
821 use derive_more::Debug;
822
823 #[derive(Debug)]
824 #[debug("{}.{}", _0, _1)]
825 struct Tuple1(char, str);
826
827 #[derive(Debug)]
828 #[debug("{_0}.{_1}")]
829 struct Tuple2(char, str);
830
831 #[derive(Debug)]
832 #[debug("{}.{}", head, tail)]
833 struct Struct1 {
834 head: char,
835 tail: str,
836 }
837
838 #[derive(Debug)]
839 #[debug("{head}.{tail}")]
840 struct Struct2 {
841 head: char,
842 tail: str,
843 }
844
845 #[test]
846 fn assert() {
847 let dat = [51i32, 3028017];
848
849 let t1 = unsafe {
850 &*(ptr::addr_of!(dat) as *const [i32] as *const Tuple1)
851 };
852 assert_eq!(format!("{t1:?}"), "3.14");
853 let t2 = unsafe {
854 &*(ptr::addr_of!(dat) as *const [i32] as *const Tuple2)
855 };
856 assert_eq!(format!("{t2:?}"), "3.14");
857 let s1 = unsafe {
858 &*(ptr::addr_of!(dat) as *const [i32] as *const Struct1)
859 };
860 assert_eq!(format!("{s1:?}"), "3.14");
861 let s2 = unsafe {
862 &*(ptr::addr_of!(dat) as *const [i32] as *const Struct2)
863 };
864 assert_eq!(format!("{s2:?}"), "3.14");
865 }
866 }
867 }
868
869 #[cfg(nightly)]
870 mod never {
871 use derive_more::Debug;
872
873 #[derive(Debug)]
874 struct Tuple(i32, !);
875
876 #[derive(Debug)]
877 struct Struct {
878 field: !,
879 other: i32,
880 }
881 }
882 }
883 }
884
885 mod enums {
886 mod no_variants {
887 use derive_more::Debug;
888
889 #[derive(Debug)]
890 enum Void {}
891
892 const fn assert<T: core::fmt::Debug>() {}
893 const _: () = assert::<Void>();
894 }
895
896 mod unit_variant {
897 #[cfg(not(feature = "std"))]
898 use alloc::format;
899
900 use derive_more::Debug;
901
902 #[derive(Debug)]
903 enum Enum {
904 Unit,
905 Unnamed(),
906 Named {},
907 }
908
909 #[test]
910 fn assert() {
911 assert_eq!(format!("{:?}", Enum::Unit), "Unit");
912 assert_eq!(format!("{:#?}", Enum::Unit), "Unit");
913 assert_eq!(format!("{:?}", Enum::Unnamed()), "Unnamed");
914 assert_eq!(format!("{:#?}", Enum::Unnamed()), "Unnamed");
915 assert_eq!(format!("{:?}", Enum::Named {}), "Named");
916 assert_eq!(format!("{:#?}", Enum::Named {}), "Named");
917 }
918
919 mod transparency {
920 #[cfg(not(feature = "std"))]
921 use alloc::format;
922
923 use derive_more::Debug;
924
925 const I32: i32 = 11;
926 const F64: f64 = 3.15;
927 const POINTER: &f64 = &3.15;
928
929 #[derive(Debug)]
930 enum Unit {
931 #[debug("{I32}")]
932 Display,
933 #[debug("{I32:?}")]
934 Debug,
935 #[debug("{:b}", I32)]
936 Binary,
937 #[debug("{0:o}", I32)]
938 Octal,
939 #[debug("{I32:x}")]
940 LowerHex,
941 #[debug("{:X}", I32)]
942 UpperHex,
943 #[debug("{F64:e}")]
944 LowerExp,
945 #[debug("{named:E}", named = F64)]
946 UpperExp,
947 #[debug("{POINTER:p}")]
948 Pointer,
949 }
950
951 #[test]
952 fn assert() {
953 assert_eq!(format!("{:03?}", Unit::Display), "011");
954 assert_eq!(format!("{:03?}", Unit::Debug), "011");
955 assert_eq!(format!("{:07?}", Unit::Binary), "0001011");
956 assert_eq!(format!("{:07?}", Unit::Octal), "0000013");
957 assert_eq!(format!("{:03?}", Unit::LowerHex), "00b");
958 assert_eq!(format!("{:03?}", Unit::UpperHex), "00B");
959 assert_eq!(format!("{:07?}", Unit::LowerExp), "03.15e0");
960 assert_eq!(format!("{:07?}", Unit::UpperExp), "03.15E0");
961 assert_eq!(
962 format!("{:018?}", Unit::Pointer),
963 format!("{POINTER:018p}"),
964 );
965 }
966
967 mod omitted {
968 mod on_modifiers {
969 #[cfg(not(feature = "std"))]
970 use alloc::format;
971
972 use derive_more::Debug;
973
974 const I32: i32 = 11;
975 const F64: f64 = 3.15;
976
977 #[derive(Debug)]
978 enum Unit {
979 #[debug("{I32:x?}")]
980 LowerDebug,
981 #[debug("{I32:X?}")]
982 UpperDebug,
983 #[debug("{:^}", I32)]
984 Align,
985 #[debug("{:+}", I32)]
986 Sign,
987 #[debug("{:#b}", I32)]
988 Alternate,
989 #[debug("{:0}", I32)]
990 ZeroPadded,
991 #[debug("{:07}", I32)]
992 Width,
993 #[debug("{:.1}", F64)]
994 Precision,
995 }
996
997 #[test]
998 fn assert() {
999 assert_eq!(format!("{:03?}", Unit::LowerDebug), "b");
1000 assert_eq!(format!("{:03?}", Unit::UpperDebug), "B");
1001 assert_eq!(format!("{:03?}", Unit::Align), "11");
1002 assert_eq!(format!("{:04?}", Unit::Sign), "+11");
1003 assert_eq!(format!("{:07?}", Unit::Alternate), "0b1011");
1004 assert_eq!(format!("{:07?}", Unit::ZeroPadded), "11");
1005 assert_eq!(format!("{:03?}", Unit::Width), "0000011");
1006 assert_eq!(format!("{:.3?}", Unit::Precision), "3.1");
1007 }
1008 }
1009 }
1010 }
1011 }
1012
1013 mod single_field_variant {
1014 #[cfg(not(feature = "std"))]
1015 use alloc::format;
1016
1017 use derive_more::Debug;
1018
1019 #[derive(Debug)]
1020 enum Enum {
1021 Unnamed(i32),
1022 Named {
1023 field: i32,
1024 },
1025 StrUnnamed(#[debug("i32")] i32),
1026 StrNamed {
1027 #[debug("i32")]
1028 field: i32,
1029 },
1030 InterpolatedUnnamed(#[debug("{_0}.{}", _0)] i32),
1031 InterpolatedNamed {
1032 #[debug("{field}.{}", field)]
1033 field: i32,
1034 },
1035 SkippedUnnamed(#[debug(skip)] i32),
1036 SkippedNamed {
1037 #[debug(skip)]
1038 field: i32,
1039 },
1040 }
1041
1042 #[test]
1043 fn assert() {
1044 assert_eq!(format!("{:?}", Enum::Unnamed(1)), "Unnamed(1)");
1045 assert_eq!(format!("{:#?}", Enum::Unnamed(1)), "Unnamed(\n 1,\n)");
1046 assert_eq!(
1047 format!("{:?}", Enum::Named { field: 1 }),
1048 "Named { field: 1 }",
1049 );
1050 assert_eq!(
1051 format!("{:#?}", Enum::Named { field: 1 }),
1052 "Named {\n field: 1,\n}",
1053 );
1054 assert_eq!(format!("{:?}", Enum::StrUnnamed(1)), "StrUnnamed(i32)");
1055 assert_eq!(
1056 format!("{:#?}", Enum::StrUnnamed(1)),
1057 "StrUnnamed(\n i32,\n)",
1058 );
1059 assert_eq!(
1060 format!("{:?}", Enum::StrNamed { field: 1 }),
1061 "StrNamed { field: i32 }",
1062 );
1063 assert_eq!(
1064 format!("{:#?}", Enum::StrNamed { field: 1 }),
1065 "StrNamed {\n field: i32,\n}",
1066 );
1067 assert_eq!(
1068 format!("{:?}", Enum::InterpolatedUnnamed(1)),
1069 "InterpolatedUnnamed(1.1)",
1070 );
1071 assert_eq!(
1072 format!("{:#?}", Enum::InterpolatedUnnamed(1)),
1073 "InterpolatedUnnamed(\n 1.1,\n)",
1074 );
1075 assert_eq!(
1076 format!("{:?}", Enum::InterpolatedNamed { field: 1 }),
1077 "InterpolatedNamed { field: 1.1 }",
1078 );
1079 assert_eq!(
1080 format!("{:#?}", Enum::InterpolatedNamed { field: 1 }),
1081 "InterpolatedNamed {\n field: 1.1,\n}",
1082 );
1083 assert_eq!(
1084 format!("{:?}", Enum::SkippedUnnamed(1)),
1085 "SkippedUnnamed(..)",
1086 );
1087 assert_eq!(
1088 format!("{:#?}", Enum::SkippedUnnamed(1)),
1089 "SkippedUnnamed(..)",
1090 );
1091 assert_eq!(
1092 format!("{:?}", Enum::SkippedNamed { field: 1 }),
1093 "SkippedNamed { .. }",
1094 );
1095 assert_eq!(
1096 format!("{:#?}", Enum::SkippedNamed { field: 1 }),
1097 "SkippedNamed { .. }",
1098 );
1099 }
1100
1101 mod transparency {
1102 #[cfg(not(feature = "std"))]
1103 use alloc::format;
1104
1105 use derive_more::Debug;
1106
1107 #[derive(Debug)]
1108 enum Enum {
1109 #[debug("{_0:?}")]
1110 Debug(i32),
1111 #[debug("{}", field)]
1112 Display { field: i32 },
1113 }
1114
1115 #[test]
1116 fn assert() {
1117 assert_eq!(format!("{:03?}", Enum::Debug(7)), "007");
1118 assert_eq!(format!("{:03?}", Enum::Display { field: 7 }), "007");
1119 }
1120
1121 mod suppressed {
1122 #[cfg(not(feature = "std"))]
1123 use alloc::format;
1124
1125 use derive_more::Debug;
1126
1127 #[derive(Debug)]
1128 enum Enum {
1129 #[debug("{}", format_args!("{_0:?}"))]
1130 Debug(i32),
1131 #[debug("{}", format_args!("{}", field))]
1132 Display { field: i32 },
1133 }
1134
1135 #[test]
1136 fn assert() {
1137 assert_eq!(format!("{:03?}", Enum::Debug(7)), "7");
1138 assert_eq!(format!("{:03?}", Enum::Display { field: 7 }), "7");
1139 }
1140 }
1141
1142 mod omitted {
1143 mod on_modifiers {
1144 #[cfg(not(feature = "std"))]
1145 use alloc::format;
1146
1147 use derive_more::Debug;
1148
1149 #[derive(Debug)]
1150 enum Enum {
1151 #[debug("{_0:x?}")]
1152 LowerDebug(i32),
1153 #[debug("{_0:X?}")]
1154 UpperDebug(i32),
1155 #[debug("{:^}", _0)]
1156 Align(i32),
1157 #[debug("{:+}", _0)]
1158 Sign(i32),
1159 #[debug("{:#b}", _0)]
1160 Alternate(i32),
1161 #[debug("{:0}", _0)]
1162 ZeroPadded(i32),
1163 #[debug("{:07}", _0)]
1164 Width(i32),
1165 #[debug("{:.5}", _0)]
1166 Precision(f64),
1167 }
1168
1169 #[test]
1170 fn assert() {
1171 assert_eq!(format!("{:03?}", Enum::LowerDebug(7)), "7");
1172 assert_eq!(format!("{:03?}", Enum::UpperDebug(8)), "8");
1173 assert_eq!(format!("{:03?}", Enum::Align(5)), "5");
1174 assert_eq!(format!("{:03?}", Enum::Sign(5)), "+5");
1175 assert_eq!(format!("{:07?}", Enum::Alternate(5)), "0b101");
1176 assert_eq!(format!("{:07?}", Enum::ZeroPadded(-5)), "-5");
1177 assert_eq!(format!("{:03?}", Enum::Width(5)), "0000005");
1178 assert_eq!(
1179 format!("{:.3?}", Enum::Precision(1.23456789)),
1180 "1.23457",
1181 );
1182 }
1183 }
1184 }
1185 }
1186
1187 #[cfg(nightly)]
1188 mod never {
1189 use derive_more::Debug;
1190
1191 #[derive(Debug)]
1192 enum Enum {
1193 Unnamed(!),
1194 Named { field: ! },
1195 }
1196 }
1197
1198 mod deprecated {
1199 use derive_more::Debug;
1200
1201 #[derive(Debug)]
1202 #[deprecated(note = "enum")]
1203 enum Enum {
1204 #[deprecated(note = "variant")]
1205 Variant {
1206 #[deprecated(note = "field")]
1207 field: i32,
1208 },
1209 }
1210 }
1211 }
1212
1213 mod multi_field_variant {
1214 #[cfg(not(feature = "std"))]
1215 use alloc::format;
1216
1217 use derive_more::Debug;
1218
1219 #[derive(Debug)]
1220 enum Enum {
1221 Unnamed(i32, i32),
1222 Named {
1223 field1: i32,
1224 field2: i32,
1225 },
1226 StrUnnamed(#[debug("i32")] i32, i32),
1227 StrNamed {
1228 field1: i32,
1229 #[debug("i32")]
1230 field2: i32,
1231 },
1232 InterpolatedUnnamed(i32, #[debug("{_0}.{}", _1)] i32),
1233 InterpolatedNamed {
1234 #[debug("{field1}.{}", field2)]
1235 field1: i32,
1236 field2: i32,
1237 },
1238 SkippedUnnamed(i32, #[debug(skip)] i32),
1239 SkippedNamed {
1240 #[debug(skip)]
1241 field1: i32,
1242 field2: i32,
1243 },
1244 }
1245
1246 #[test]
1247 fn assert() {
1248 assert_eq!(format!("{:?}", Enum::Unnamed(1, 2)), "Unnamed(1, 2)");
1249 assert_eq!(
1250 format!("{:#?}", Enum::Unnamed(1, 2)),
1251 "Unnamed(\n 1,\n 2,\n)",
1252 );
1253 assert_eq!(
1254 format!("{:?}", Enum::StrUnnamed(1, 2)),
1255 "StrUnnamed(i32, 2)",
1256 );
1257 assert_eq!(
1258 format!("{:#?}", Enum::StrUnnamed(1, 2)),
1259 "StrUnnamed(\n i32,\n 2,\n)",
1260 );
1261 assert_eq!(
1262 format!(
1263 "{:?}",
1264 Enum::StrNamed {
1265 field1: 1,
1266 field2: 2,
1267 }
1268 ),
1269 "StrNamed { field1: 1, field2: i32 }",
1270 );
1271 assert_eq!(
1272 format!(
1273 "{:#?}",
1274 Enum::StrNamed {
1275 field1: 1,
1276 field2: 2,
1277 }
1278 ),
1279 "StrNamed {\n field1: 1,\n field2: i32,\n}",
1280 );
1281 assert_eq!(
1282 format!("{:?}", Enum::InterpolatedUnnamed(1, 2)),
1283 "InterpolatedUnnamed(1, 1.2)",
1284 );
1285 assert_eq!(
1286 format!("{:#?}", Enum::InterpolatedUnnamed(1, 2)),
1287 "InterpolatedUnnamed(\n 1,\n 1.2,\n)",
1288 );
1289 assert_eq!(
1290 format!(
1291 "{:?}",
1292 Enum::InterpolatedNamed {
1293 field1: 1,
1294 field2: 2,
1295 }
1296 ),
1297 "InterpolatedNamed { field1: 1.2, field2: 2 }",
1298 );
1299 assert_eq!(
1300 format!(
1301 "{:#?}",
1302 Enum::InterpolatedNamed {
1303 field1: 1,
1304 field2: 2,
1305 }
1306 ),
1307 "InterpolatedNamed {\n field1: 1.2,\n field2: 2,\n}",
1308 );
1309 assert_eq!(
1310 format!("{:?}", Enum::SkippedUnnamed(1, 2)),
1311 "SkippedUnnamed(1, ..)",
1312 );
1313 assert_eq!(
1314 format!("{:#?}", Enum::SkippedUnnamed(1, 2)),
1315 "SkippedUnnamed(\n 1,\n ..\n)",
1316 );
1317 assert_eq!(
1318 format!(
1319 "{:?}",
1320 Enum::SkippedNamed {
1321 field1: 1,
1322 field2: 2,
1323 }
1324 ),
1325 "SkippedNamed { field2: 2, .. }",
1326 );
1327 assert_eq!(
1328 format!(
1329 "{:#?}",
1330 Enum::SkippedNamed {
1331 field1: 1,
1332 field2: 2,
1333 }
1334 ),
1335 "SkippedNamed {\n field2: 2,\n ..\n}",
1336 );
1337 }
1338
1339 mod interpolated_variant {
1340 #[cfg(not(feature = "std"))]
1341 use alloc::format;
1342
1343 use derive_more::Debug;
1344
1345 #[derive(Debug)]
1346 enum Enum {
1347 #[debug("Format String")]
1348 Unit,
1349 #[debug("Format {a} String {b}")]
1350 Fields { a: usize, b: u8 },
1351 }
1352
1353 #[test]
1354 fn assert() {
1355 assert_eq!(format!("{:?}", Enum::Unit), "Format String");
1356 assert_eq!(
1357 format!("{:?}", Enum::Fields { a: 1, b: 2 }),
1358 "Format 1 String 2",
1359 );
1360 }
1361 }
1362
1363 mod transparency {
1364 #[cfg(not(feature = "std"))]
1365 use alloc::format;
1366
1367 use derive_more::Debug;
1368
1369 #[derive(Debug)]
1370 enum Enum {
1371 #[debug("{0:o}", _0)]
1372 TupleOctal(i32, i64),
1373 #[debug("{named:e}", named = b)]
1374 StructLowerExp { a: i32, b: f64 },
1375 }
1376
1377 #[test]
1378 fn assert() {
1379 assert_eq!(format!("{:03?}", Enum::TupleOctal(9, 4)), "011");
1380 assert_eq!(
1381 format!("{:.1?}", Enum::StructLowerExp { a: 7, b: 3.15 }),
1382 "3.1e0",
1383 );
1384 }
1385 }
1386
1387 #[cfg(nightly)]
1388 mod never {
1389 use derive_more::Debug;
1390
1391 #[derive(Debug)]
1392 enum Enum {
1393 Unnamed(i32, !),
1394 Named { field: !, other: i32 },
1395 }
1396 }
1397 }
1398 }
1399
1400 mod generic {
1401 #[cfg(not(feature = "std"))]
1402 use alloc::{boxed::Box, format};
1403 use core::fmt;
1404
1405 use derive_more::Debug;
1406
1407 struct NotDebug;
1408
1409 impl fmt::Display for NotDebug {
1410 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1411 f.debug_tuple("NotDebug").finish()
1412 }
1413 }
1414
1415 #[derive(Debug)]
1416 struct NamedGenericStruct<T> {
1417 field: T,
1418 }
1419 #[test]
1420 fn named_generic_struct() {
1421 assert_eq!(
1422 format!("{:?}", NamedGenericStruct { field: 1 }),
1423 "NamedGenericStruct { field: 1 }",
1424 );
1425 assert_eq!(
1426 format!("{:#?}", NamedGenericStruct { field: 1 }),
1427 "NamedGenericStruct {\n field: 1,\n}",
1428 );
1429 }
1430
1431 #[derive(Debug)]
1432 struct NamedGenericStructUnsized<T: ?Sized> {
1433 field: T,
1434 }
1435 #[test]
1436 fn named_generic_struct_unsized() {
1437 assert_eq!(
1438 format!("{:?}", NamedGenericStructUnsized { field: 1 }),
1439 "NamedGenericStructUnsized { field: 1 }",
1440 );
1441 assert_eq!(
1442 format!("{:#?}", NamedGenericStructUnsized { field: 1 }),
1443 "NamedGenericStructUnsized {\n field: 1,\n}",
1444 );
1445 }
1446
1447 #[derive(Debug)]
1448 struct NamedGenericStructIgnored<T> {
1449 #[debug(ignore)]
1450 field: T,
1451 }
1452 #[test]
1453 fn named_generic_struct_ignored() {
1454 assert_eq!(
1455 format!("{:?}", NamedGenericStructIgnored { field: NotDebug }),
1456 "NamedGenericStructIgnored { .. }",
1457 );
1458 assert_eq!(
1459 format!("{:#?}", NamedGenericStructIgnored { field: NotDebug }),
1460 "NamedGenericStructIgnored { .. }",
1461 );
1462 }
1463
1464 #[derive(Debug)]
1465 struct InterpolatedNamedGenericStruct<T> {
1466 #[debug("{field}.{}", field)]
1467 field: T,
1468 }
1469 #[test]
1470 fn interpolated_named_generic_struct() {
1471 assert_eq!(
1472 format!("{:?}", InterpolatedNamedGenericStruct { field: 1 }),
1473 "InterpolatedNamedGenericStruct { field: 1.1 }",
1474 );
1475 assert_eq!(
1476 format!("{:#?}", InterpolatedNamedGenericStruct { field: 1 }),
1477 "InterpolatedNamedGenericStruct {\n field: 1.1,\n}",
1478 );
1479 assert_eq!(
1480 format!("{:?}", InterpolatedNamedGenericStruct { field: NotDebug }),
1481 "InterpolatedNamedGenericStruct { field: NotDebug.NotDebug }",
1482 );
1483 assert_eq!(
1484 format!("{:#?}", InterpolatedNamedGenericStruct { field: NotDebug }),
1485 "InterpolatedNamedGenericStruct {\n field: NotDebug.NotDebug,\n}",
1486 );
1487 }
1488
1489 #[derive(Debug)]
1490 struct InterpolatedNamedGenericStructWidthPrecision<T> {
1491 #[debug("{field:<>width$.prec$}.{field}")]
1492 field: T,
1493 width: usize,
1494 prec: usize,
1495 }
1496 #[test]
1497 fn interpolated_named_generic_struct_width_precision() {
1498 assert_eq!(
1499 format!(
1500 "{:?}",
1501 InterpolatedNamedGenericStructWidthPrecision {
1502 field: 1.2345,
1503 width: 9,
1504 prec: 2,
1505 }
1506 ),
1507 "InterpolatedNamedGenericStructWidthPrecision { \
1508 field: <<<<<1.23.1.2345, \
1509 width: 9, \
1510 prec: 2 \
1511 }",
1512 );
1513 assert_eq!(
1514 format!(
1515 "{:#?}",
1516 InterpolatedNamedGenericStructWidthPrecision {
1517 field: 1.2345,
1518 width: 9,
1519 prec: 2,
1520 }
1521 ),
1522 "InterpolatedNamedGenericStructWidthPrecision {\n \
1523 field: <<<<<1.23.1.2345,\n \
1524 width: 9,\n \
1525 prec: 2,\n\
1526 }",
1527 );
1528 }
1529
1530 #[derive(Debug)]
1531 #[debug("test_named")]
1532 struct InterpolatedNamedGenericStructIgnored<T> {
1533 field: T,
1534 }
1535 #[test]
1536 fn interpolated_named_generic_struct_ignored() {
1537 assert_eq!(
1538 format!(
1539 "{:?}",
1540 InterpolatedNamedGenericStructIgnored { field: NotDebug },
1541 ),
1542 "test_named",
1543 );
1544 }
1545
1546 #[derive(Debug)]
1547 struct AliasedNamedGenericStruct<T> {
1548 #[debug("{alias}", alias = field)]
1549 field: T,
1550 }
1551 #[test]
1552 fn aliased_named_generic_struct() {
1553 assert_eq!(
1554 format!("{:?}", AliasedNamedGenericStruct { field: 1 }),
1555 "AliasedNamedGenericStruct { field: 1 }",
1556 );
1557 assert_eq!(
1558 format!("{:#?}", AliasedNamedGenericStruct { field: 1 }),
1559 "AliasedNamedGenericStruct {\n field: 1,\n}",
1560 );
1561 }
1562
1563 #[derive(Debug)]
1564 struct AliasedFieldNamedGenericStruct<T> {
1565 #[debug("{field1}", field1 = field2)]
1566 field1: T,
1567 field2: i32,
1568 }
1569 #[test]
1570 fn aliased_field_named_generic_struct() {
1571 assert_eq!(
1572 format!(
1573 "{:?}",
1574 AliasedFieldNamedGenericStruct {
1575 field1: NotDebug,
1576 field2: 1,
1577 },
1578 ),
1579 "AliasedFieldNamedGenericStruct { field1: 1, field2: 1 }",
1580 );
1581 assert_eq!(
1582 format!(
1583 "{:#?}",
1584 AliasedFieldNamedGenericStruct {
1585 field1: NotDebug,
1586 field2: 1,
1587 },
1588 ),
1589 "AliasedFieldNamedGenericStruct {\n field1: 1,\n field2: 1,\n}",
1590 );
1591 }
1592
1593 #[derive(Debug)]
1594 struct UnnamedGenericStruct<T>(T);
1595 #[test]
1596 fn unnamed_generic_struct() {
1597 assert_eq!(
1598 format!("{:?}", UnnamedGenericStruct(2)),
1599 "UnnamedGenericStruct(2)",
1600 );
1601 assert_eq!(
1602 format!("{:#?}", UnnamedGenericStruct(2)),
1603 "UnnamedGenericStruct(\n 2,\n)",
1604 );
1605 }
1606
1607 #[derive(Debug)]
1608 struct UnnamedGenericStructUnsized<T: ?Sized>(T);
1609 #[test]
1610 fn unnamed_generic_struct_unsized() {
1611 assert_eq!(
1612 format!("{:?}", UnnamedGenericStructUnsized(2)),
1613 "UnnamedGenericStructUnsized(2)",
1614 );
1615 assert_eq!(
1616 format!("{:#?}", UnnamedGenericStructUnsized(2)),
1617 "UnnamedGenericStructUnsized(\n 2,\n)",
1618 );
1619 }
1620
1621 #[derive(Debug)]
1622 struct UnnamedGenericStructIgnored<T>(#[debug(skip)] T);
1623 #[test]
1624 fn unnamed_generic_struct_ignored() {
1625 assert_eq!(
1626 format!("{:?}", UnnamedGenericStructIgnored(NotDebug)),
1627 "UnnamedGenericStructIgnored(..)",
1628 );
1629 assert_eq!(
1630 format!("{:#?}", UnnamedGenericStructIgnored(NotDebug)),
1631 "UnnamedGenericStructIgnored(..)",
1632 );
1633 }
1634
1635 #[derive(Debug)]
1636 struct InterpolatedUnnamedGenericStruct<T>(#[debug("{}.{_0}", _0)] T);
1637 #[test]
1638 fn interpolated_unnamed_generic_struct() {
1639 assert_eq!(
1640 format!("{:?}", InterpolatedUnnamedGenericStruct(2)),
1641 "InterpolatedUnnamedGenericStruct(2.2)",
1642 );
1643 assert_eq!(
1644 format!("{:#?}", InterpolatedUnnamedGenericStruct(2)),
1645 "InterpolatedUnnamedGenericStruct(\n 2.2,\n)",
1646 );
1647 assert_eq!(
1648 format!("{:?}", InterpolatedUnnamedGenericStruct(NotDebug)),
1649 "InterpolatedUnnamedGenericStruct(NotDebug.NotDebug)",
1650 );
1651 assert_eq!(
1652 format!("{:#?}", InterpolatedUnnamedGenericStruct(NotDebug)),
1653 "InterpolatedUnnamedGenericStruct(\n NotDebug.NotDebug,\n)",
1654 );
1655 }
1656
1657 #[derive(Debug)]
1658 #[debug("test_unnamed")]
1659 struct InterpolatedUnnamedGenericStructIgnored<T>(T);
1660 #[test]
1661 fn interpolated_unnamed_generic_struct_ignored() {
1662 assert_eq!(
1663 format!("{:?}", InterpolatedUnnamedGenericStructIgnored(NotDebug)),
1664 "test_unnamed",
1665 );
1666 }
1667
1668 #[derive(Debug)]
1669 struct AliasedUnnamedGenericStruct<T>(#[debug("{alias}", alias = _0)] T);
1670 #[test]
1671 fn aliased_unnamed_generic_struct() {
1672 assert_eq!(
1673 format!("{:?}", AliasedUnnamedGenericStruct(2)),
1674 "AliasedUnnamedGenericStruct(2)",
1675 );
1676 assert_eq!(
1677 format!("{:#?}", AliasedUnnamedGenericStruct(2)),
1678 "AliasedUnnamedGenericStruct(\n 2,\n)",
1679 );
1680 }
1681
1682 #[derive(Debug)]
1683 struct AliasedFieldUnnamedGenericStruct<T>(#[debug("{_0}", _0 = _1)] T, i32);
1684 #[test]
1685 fn aliased_field_unnamed_generic_struct() {
1686 assert_eq!(
1687 format!("{:?}", AliasedFieldUnnamedGenericStruct(NotDebug, 2)),
1688 "AliasedFieldUnnamedGenericStruct(2, 2)",
1689 );
1690 assert_eq!(
1691 format!("{:#?}", AliasedFieldUnnamedGenericStruct(NotDebug, 2)),
1692 "AliasedFieldUnnamedGenericStruct(\n 2,\n 2,\n)",
1693 );
1694 }
1695
1696 #[derive(Debug)]
1697 enum GenericEnum<A, B> {
1698 A { field: A },
1699 B(B),
1700 }
1701 #[test]
1702 fn generic_enum() {
1703 assert_eq!(
1704 format!("{:?}", GenericEnum::A::<_, u8> { field: 1 }),
1705 "A { field: 1 }",
1706 );
1707 assert_eq!(
1708 format!("{:#?}", GenericEnum::A::<_, u8> { field: 1 }),
1709 "A {\n field: 1,\n}",
1710 );
1711 assert_eq!(format!("{:?}", GenericEnum::B::<u8, _>(2)), "B(2)");
1712 assert_eq!(
1713 format!("{:#?}", GenericEnum::B::<u8, _>(2)),
1714 "B(\n 2,\n)",
1715 );
1716 }
1717
1718 #[derive(derive_more::Debug)]
1719 enum GenericEnumUnsized<A: ?Sized, B: ?Sized + 'static> {
1720 A { field: Box<A> },
1721 B(&'static B),
1722 }
1723 #[test]
1724 fn generic_enum_unsized() {
1725 assert_eq!(
1726 format!("{:?}", GenericEnumUnsized::A::<i32, u8> { field: 1.into() }),
1727 "A { field: 1 }",
1728 );
1729 assert_eq!(
1730 format!(
1731 "{:#?}",
1732 GenericEnumUnsized::A::<i32, u8> { field: 1.into() },
1733 ),
1734 "A {\n field: 1,\n}",
1735 );
1736 assert_eq!(
1737 format!("{:?}", GenericEnumUnsized::B::<u8, i32>(&2)),
1738 "B(2)",
1739 );
1740 assert_eq!(
1741 format!("{:#?}", GenericEnumUnsized::B::<u8, i32>(&2)),
1742 "B(\n 2,\n)",
1743 );
1744 }
1745
1746 #[derive(Debug)]
1747 enum InterpolatedGenericEnum<A, B> {
1748 A {
1749 #[debug("{}.{field}", field)]
1750 field: A,
1751 },
1752 B(#[debug("{}.{_0}", _0)] B),
1753 }
1754 #[test]
1755 fn interpolated_generic_enum() {
1756 assert_eq!(
1757 format!("{:?}", InterpolatedGenericEnum::A::<_, u8> { field: 1 }),
1758 "A { field: 1.1 }",
1759 );
1760 assert_eq!(
1761 format!("{:#?}", InterpolatedGenericEnum::A::<_, u8> { field: 1 }),
1762 "A {\n field: 1.1,\n}",
1763 );
1764 assert_eq!(
1765 format!("{:?}", InterpolatedGenericEnum::B::<u8, _>(2)),
1766 "B(2.2)",
1767 );
1768 assert_eq!(
1769 format!("{:#?}", InterpolatedGenericEnum::B::<u8, _>(2)),
1770 "B(\n 2.2,\n)",
1771 );
1772 }
1773
1774 #[derive(Debug)]
1775 enum InterpolatedGenericEnumIgnored<A, B> {
1776 #[debug("A {field}")]
1777 A { field: A },
1778 #[debug("B")]
1779 B(B),
1780 }
1781 #[test]
1782 fn interpolated_generic_enum_ignored() {
1783 assert_eq!(
1784 format!(
1785 "{:?}",
1786 InterpolatedGenericEnumIgnored::A::<_, u8> { field: NotDebug },
1787 ),
1788 "A NotDebug",
1789 );
1790 assert_eq!(
1791 format!("{:?}", InterpolatedGenericEnumIgnored::B::<u8, _>(NotDebug)),
1792 "B",
1793 );
1794 }
1795
1796 #[derive(Debug)]
1797 struct MultiTraitNamedGenericStruct<A, B> {
1798 #[debug("{}.{}<->{0:o}.{1:#x}<->{0:?}.{1:X?}", a, b)]
1799 a: A,
1800 b: B,
1801 }
1802 #[test]
1803 fn multi_trait_named_generic_struct() {
1804 let s = MultiTraitNamedGenericStruct { a: 8u8, b: 255 };
1805 assert_eq!(
1806 format!("{s:?}"),
1807 "MultiTraitNamedGenericStruct { a: 8.255<->10.0xff<->8.FF, b: 255 }",
1808 );
1809 assert_eq!(
1810 format!("{s:#?}"),
1811 "MultiTraitNamedGenericStruct {\n a: 8.255<->10.0xff<->8.FF,\n b: 255,\n}",
1812 );
1813 }
1814
1815 #[derive(Debug)]
1816 struct MultiTraitUnnamedGenericStruct<A, B>(
1817 #[debug("{}.{}.{{}}.{0:o}.{1:#x}-{0:>4?}.{1:^4X?}", _0, _1)] A,
1818 B,
1819 );
1820 #[test]
1821 fn multi_trait_unnamed_generic_struct() {
1822 let s = MultiTraitUnnamedGenericStruct(8u8, 255);
1823 assert_eq!(
1824 format!("{s:?}"),
1825 "MultiTraitUnnamedGenericStruct(8.255.{}.10.0xff- 8. FF , 255)",
1826 );
1827 assert_eq!(
1828 format!("{s:#?}"),
1829 "MultiTraitUnnamedGenericStruct(\n 8.255.{}.10.0xff- 8. FF ,\n 255,\n)",
1830 );
1831 }
1832
1833 #[derive(Debug)]
1834 struct UnusedGenericStruct<T>(#[debug("{}", 3 * 4)] T);
1835 #[test]
1836 fn unused_generic_struct() {
1837 let s = UnusedGenericStruct(NotDebug);
1838 assert_eq!(format!("{s:?}"), "UnusedGenericStruct(12)");
1839 assert_eq!(format!("{s:#?}"), "UnusedGenericStruct(\n 12,\n)");
1840 }
1841
1842 mod associated_type_field_enumerator {
1843 #[cfg(not(feature = "std"))]
1844 use alloc::format;
1845
1846 use derive_more::Debug;
1847
1848 trait Trait {
1849 type Type;
1850 }
1851
1852 struct Struct;
1853
1854 impl Trait for Struct {
1855 type Type = i32;
1856 }
1857
1858 #[test]
1859 fn auto_generic_named_struct_associated() {
1860 #[derive(Debug)]
1861 struct AutoGenericNamedStructAssociated<T: Trait> {
1862 field: <T as Trait>::Type,
1863 }
1864
1865 let s = AutoGenericNamedStructAssociated::<Struct> { field: 10 };
1866 assert_eq!(
1867 format!("{s:?}"),
1868 "AutoGenericNamedStructAssociated { field: 10 }",
1869 );
1870 assert_eq!(
1871 format!("{s:#?}"),
1872 "AutoGenericNamedStructAssociated {\n field: 10,\n}",
1873 );
1874 }
1875
1876 #[test]
1877 fn auto_generic_unnamed_struct_associated() {
1878 #[derive(Debug)]
1879 struct AutoGenericUnnamedStructAssociated<T: Trait>(<T as Trait>::Type);
1880
1881 let s = AutoGenericUnnamedStructAssociated::<Struct>(10);
1882 assert_eq!(format!("{s:?}"), "AutoGenericUnnamedStructAssociated(10)",);
1883 assert_eq!(
1884 format!("{s:#?}"),
1885 "AutoGenericUnnamedStructAssociated(\n 10,\n)",
1886 );
1887 }
1888
1889 #[test]
1890 fn auto_generic_enum_associated() {
1891 #[derive(Debug)]
1892 enum AutoGenericEnumAssociated<T: Trait> {
1893 Enumerator(<T as Trait>::Type),
1894 }
1895
1896 let e = AutoGenericEnumAssociated::<Struct>::Enumerator(10);
1897 assert_eq!(format!("{e:?}"), "Enumerator(10)");
1898 assert_eq!(format!("{e:#?}"), "Enumerator(\n 10,\n)");
1899 }
1900 }
1901
1902 mod complex_type_field_enumerator {
1903 #[cfg(not(feature = "std"))]
1904 use alloc::format;
1905
1906 use derive_more::Debug;
1907
1908 #[derive(Debug)]
1909 struct Struct<T>(T);
1910
1911 #[test]
1912 fn auto_generic_named_struct_complex() {
1913 #[derive(Debug)]
1914 struct AutoGenericNamedStructComplex<T> {
1915 field: Struct<T>,
1916 }
1917
1918 let s = AutoGenericNamedStructComplex { field: Struct(10) };
1919 assert_eq!(
1920 format!("{s:?}"),
1921 "AutoGenericNamedStructComplex { field: Struct(10) }",
1922 );
1923 assert_eq!(
1924 format!("{s:#?}"),
1925 "AutoGenericNamedStructComplex {\n field: Struct(\n 10,\n ),\n}",
1926 );
1927 }
1928
1929 #[test]
1930 fn auto_generic_unnamed_struct_complex() {
1931 #[derive(Debug)]
1932 struct AutoGenericUnnamedStructComplex<T>(Struct<T>);
1933
1934 let s = AutoGenericUnnamedStructComplex(Struct(10));
1935 assert_eq!(
1936 format!("{s:?}"),
1937 "AutoGenericUnnamedStructComplex(Struct(10))",
1938 );
1939 assert_eq!(
1940 format!("{s:#?}"),
1941 "AutoGenericUnnamedStructComplex(\n Struct(\n 10,\n ),\n)",
1942 );
1943 }
1944
1945 #[test]
1946 fn auto_generic_enum_complex() {
1947 #[derive(Debug)]
1948 enum AutoGenericEnumComplex<T> {
1949 Enumerator(Struct<T>),
1950 }
1951
1952 let e = AutoGenericEnumComplex::Enumerator(Struct(10));
1953 assert_eq!(format!("{e:?}"), "Enumerator(Struct(10))");
1954 assert_eq!(
1955 format!("{e:#?}"),
1956 "Enumerator(\n Struct(\n 10,\n ),\n)",
1957 )
1958 }
1959 }
1960
1961 mod reference {
1962 #[cfg(not(feature = "std"))]
1963 use alloc::format;
1964
1965 use derive_more::Debug;
1966
1967 #[test]
1968 fn auto_generic_reference() {
1969 #[derive(Debug)]
1970 struct AutoGenericReference<'a, T>(&'a T);
1971
1972 let s = AutoGenericReference(&10);
1973 assert_eq!(format!("{s:?}"), "AutoGenericReference(10)");
1974 assert_eq!(format!("{s:#?}"), "AutoGenericReference(\n 10,\n)");
1975 }
1976
1977 #[test]
1978 fn auto_generic_static_reference() {
1979 #[derive(Debug)]
1980 struct AutoGenericStaticReference<T: 'static>(&'static T);
1981
1982 let s = AutoGenericStaticReference(&10);
1983 assert_eq!(format!("{s:?}"), "AutoGenericStaticReference(10)");
1984 assert_eq!(format!("{s:#?}"), "AutoGenericStaticReference(\n 10,\n)",);
1985 }
1986 }
1987
1988 mod indirect {
1989 #[cfg(not(feature = "std"))]
1990 use alloc::format;
1991
1992 use derive_more::Debug;
1993
1994 #[derive(Debug)]
1995 struct Struct<T>(T);
1996
1997 #[test]
1998 fn auto_generic_indirect() {
1999 #[derive(Debug)]
2000 struct AutoGenericIndirect<T: 'static>(Struct<&'static T>);
2001
2002 const V: i32 = 10;
2003 let s = AutoGenericIndirect(Struct(&V));
2004 assert_eq!(format!("{s:?}"), "AutoGenericIndirect(Struct(10))");
2005 assert_eq!(
2006 format!("{s:#?}"),
2007 "AutoGenericIndirect(\n Struct(\n 10,\n ),\n)",
2008 );
2009 }
2010 }
2011
2012 mod raw {
2013 #[cfg(not(feature = "std"))]
2014 use alloc::format;
2015
2016 use derive_more::Debug;
2017
2018 #[derive(Debug)]
2019 struct StructOne<T> {
2020 r#thing: T,
2021 }
2022
2023 #[derive(Debug)]
2024 struct StructOneKeyword<T> {
2025 r#struct: T,
2026 }
2027
2028 #[derive(Debug)]
2029 enum Enum<T> {
2030 One { r#thing: T },
2031 }
2032
2033 #[derive(Debug)]
2034 enum EnumKeyword<T> {
2035 One { r#struct: T },
2036 }
2037
2038 #[test]
2039 fn assert() {
2040 assert_eq!(
2041 format!("{:?}", StructOne::<u8> { r#thing: 8 }),
2042 "StructOne { thing: 8 }",
2043 );
2044 assert_eq!(
2045 format!("{:?}", StructOneKeyword::<u8> { r#struct: 8 }),
2046 "StructOneKeyword { struct: 8 }",
2047 );
2048 assert_eq!(
2049 format!("{:?}", Enum::<u8>::One { r#thing: 8 }),
2050 "One { thing: 8 }",
2051 );
2052 assert_eq!(
2053 format!("{:?}", EnumKeyword::<u8>::One { r#struct: 8 }),
2054 "One { struct: 8 }",
2055 );
2056 }
2057
2058 mod interpolated {
2059 #[cfg(not(feature = "std"))]
2060 use alloc::format;
2061
2062 use derive_more::Debug;
2063
2064 #[derive(Debug)]
2065 #[debug("{thing}")]
2066 struct StructOne<T> {
2067 r#thing: T,
2068 }
2069
2070 #[derive(Debug)]
2071 #[debug("{struct}")]
2072 struct StructOneKeyword<T> {
2073 r#struct: T,
2074 }
2075
2076 #[derive(Debug)]
2077 enum Enum1<T> {
2078 #[debug("{thing}")]
2079 One { r#thing: T },
2080 }
2081
2082 #[derive(Debug)]
2083 enum Enum1Keyword<T> {
2084 #[debug("{struct}")]
2085 One { r#struct: T },
2086 }
2087
2088 #[derive(Debug)]
2089 #[debug("{a}:{b}")]
2090 struct StructTwo<A, B> {
2091 r#a: A,
2092 b: B,
2093 }
2094
2095 #[derive(Debug)]
2096 #[debug("{pub}:{b}")]
2097 struct StructTwoKeyword<A, B> {
2098 r#pub: A,
2099 b: B,
2100 }
2101
2102 #[derive(Debug)]
2103 enum Enum2<A, B> {
2104 #[debug("{a}:{b}")]
2105 Two { r#a: A, b: B },
2106 }
2107
2108 #[derive(Debug)]
2109 enum Enum2Keyword<A, B> {
2110 #[debug("{pub}:{b}")]
2111 Two { r#pub: A, b: B },
2112 }
2113
2114 #[test]
2115 fn assert() {
2116 assert_eq!(format!("{:?}", StructOne::<u8> { r#thing: 8 }), "8");
2117 assert_eq!(
2118 format!("{:?}", StructOneKeyword::<u8> { r#struct: 8 }),
2119 "8",
2120 );
2121 assert_eq!(format!("{:?}", Enum1::<u8>::One { r#thing: 8 }), "8");
2122 assert_eq!(
2123 format!("{:?}", Enum1Keyword::<u8>::One { r#struct: 8 }),
2124 "8",
2125 );
2126 assert_eq!(
2127 format!("{:?}", StructTwo::<u8, u16> { r#a: 8, b: 16 }),
2128 "8:16",
2129 );
2130 assert_eq!(
2131 format!("{:?}", StructTwoKeyword::<u8, u16> { r#pub: 8, b: 16 }),
2132 "8:16",
2133 );
2134 assert_eq!(
2135 format!("{:?}", Enum2::<u8, u16>::Two { r#a: 8, b: 16 }),
2136 "8:16",
2137 );
2138 assert_eq!(
2139 format!("{:?}", Enum2Keyword::<u8, u16>::Two { r#pub: 8, b: 16 }),
2140 "8:16",
2141 );
2142 }
2143 }
2144 }
2145
2146 mod bound {
2147 #[cfg(not(feature = "std"))]
2148 use alloc::format;
2149
2150 use derive_more::Debug;
2151
2152 #[test]
2153 fn simple() {
2154 #[derive(Debug)]
2155 struct Struct<T1, T2>(#[debug("{}.{}", _0, _1)] T1, #[debug(skip)] T2);
2156
2157 let s = Struct(10, 20);
2158 assert_eq!(format!("{s:?}"), "Struct(10.20, ..)");
2159 assert_eq!(format!("{s:#?}"), "Struct(\n 10.20,\n ..\n)");
2160 }
2161
2162 #[test]
2163 fn underscored_simple() {
2164 #[derive(Debug)]
2165 struct Struct<T1, T2>(#[debug("{_0}.{_1}")] T1, #[debug(skip)] T2);
2166
2167 let s = Struct(10, 20);
2168 assert_eq!(format!("{s:?}"), "Struct(10.20, ..)");
2169 assert_eq!(format!("{s:#?}"), "Struct(\n 10.20,\n ..\n)");
2170 }
2171
2172 #[test]
2173 fn redundant() {
2174 #[derive(Debug)]
2175 #[debug(bound(T1: ::core::fmt::Display, T2: ::core::fmt::Display))]
2176 struct Struct<T1, T2>(#[debug("{}.{}", _0, _1)] T1, #[debug(skip)] T2);
2177
2178 let s = Struct(10, 20);
2179 assert_eq!(format!("{s:?}"), "Struct(10.20, ..)");
2180 assert_eq!(format!("{s:#?}"), "Struct(\n 10.20,\n ..\n)");
2181 }
2182
2183 #[test]
2184 fn underscored_redundant() {
2185 #[derive(Debug)]
2186 #[debug(bound(T1: ::core::fmt::Display, T2: ::core::fmt::Display))]
2187 struct Struct<T1, T2>(#[debug("{_0}.{_1}")] T1, #[debug(ignore)] T2);
2188
2189 let s = Struct(10, 20);
2190 assert_eq!(format!("{s:?}"), "Struct(10.20, ..)");
2191 assert_eq!(format!("{s:#?}"), "Struct(\n 10.20,\n ..\n)");
2192 }
2193
2194 #[test]
2195 fn complex() {
2196 trait Trait1 {
2197 fn function1(&self) -> &'static str;
2198 }
2199
2200 trait Trait2 {
2201 fn function2(&self) -> &'static str;
2202 }
2203
2204 impl Trait1 for i32 {
2205 fn function1(&self) -> &'static str {
2206 "WHAT"
2207 }
2208 }
2209
2210 impl Trait2 for i32 {
2211 fn function2(&self) -> &'static str {
2212 "EVER"
2213 }
2214 }
2215
2216 #[derive(Debug)]
2217 #[debug(bound(T1: Trait1 + Trait2, T2: Trait1 + Trait2))]
2218 struct Struct<T1, T2>(
2219 #[debug("{}_{}_{}_{}", _0.function1(), _0, _1.function2(), _1)] T1,
2220 T2,
2221 );
2222
2223 let s = Struct(10, 20);
2224 assert_eq!(format!("{s:?}"), "Struct(WHAT_10_EVER_20, 20)");
2225 assert_eq!(
2226 format!("{s:#?}"),
2227 "Struct(\n WHAT_10_EVER_20,\n 20,\n)",
2228 );
2229 }
2230
2231 #[test]
2232 fn underscored_complex() {
2233 trait Trait1 {
2234 fn function1(&self) -> &'static str;
2235 }
2236
2237 trait Trait2 {
2238 fn function2(&self) -> &'static str;
2239 }
2240
2241 impl Trait1 for i32 {
2242 fn function1(&self) -> &'static str {
2243 "WHAT"
2244 }
2245 }
2246
2247 impl Trait2 for i32 {
2248 fn function2(&self) -> &'static str {
2249 "EVER"
2250 }
2251 }
2252
2253 #[derive(Debug)]
2254 #[debug(bound(T1: Trait1 + Trait2, T2: Trait1 + Trait2))]
2255 struct Struct<T1, T2>(
2256 #[debug("{}_{_0}_{}_{_1}", _0.function1(), _1.function2())] T1,
2257 T2,
2258 );
2259
2260 let s = Struct(10, 20);
2261 assert_eq!(format!("{s:?}"), "Struct(WHAT_10_EVER_20, 20)");
2262 assert_eq!(
2263 format!("{s:#?}"),
2264 "Struct(\n WHAT_10_EVER_20,\n 20,\n)",
2265 );
2266 }
2267 }
2268
2269 mod transparency {
2270 #[cfg(not(feature = "std"))]
2271 use alloc::format;
2272
2273 use derive_more::Debug;
2274
2275 #[derive(Debug)]
2276 #[debug("{0:o}", _0)]
2277 struct Tuple<T>(T);
2278
2279 #[derive(Debug)]
2280 #[debug("{named:e}", named = b)]
2281 struct Struct<A, B> {
2282 a: A,
2283 b: B,
2284 }
2285
2286 #[derive(Debug)]
2287 enum Enum<A, B, C> {
2288 #[debug("{_0:?}")]
2289 Debug(A),
2290 #[debug("{}", c)]
2291 Display { b: B, c: C },
2292 }
2293
2294 #[test]
2295 fn assert() {
2296 assert_eq!(format!("{:03?}", Tuple(9)), "011");
2297 assert_eq!(format!("{:.1?}", Struct { a: 9, b: 3.15 }), "3.1e0");
2298 assert_eq!(format!("{:03?}", Enum::<_, u8, u8>::Debug(7)), "007");
2299 assert_eq!(
2300 format!("{:03?}", Enum::<u8, _, _>::Display { b: 7, c: 8 }),
2301 "008",
2302 );
2303 }
2304
2305 mod omitted {
2306 mod on_modifiers {
2307 #[cfg(not(feature = "std"))]
2308 use alloc::format;
2309
2310 use derive_more::Debug;
2311
2312 #[derive(Debug)]
2313 enum Enum<A, B, C, D> {
2314 #[debug("{_0:x?}")]
2315 LowerDebug(A),
2316 #[debug("{_0:X?}")]
2317 UpperDebug(B),
2318 #[debug("{:^}", _0)]
2319 Align(C),
2320 #[debug("{:+}", _0)]
2321 Sign(C),
2322 #[debug("{:#b}", _0)]
2323 Alternate(C),
2324 #[debug("{:0}", _0)]
2325 ZeroPadded(C),
2326 #[debug("{:07}", _0)]
2327 Width(C),
2328 #[debug("{:.5}", _0)]
2329 Precision(D),
2330 }
2331
2332 #[test]
2333 fn assert() {
2334 assert_eq!(
2335 format!("{:03?}", Enum::<_, u8, u8, f64>::LowerDebug(7)),
2336 "7",
2337 );
2338 assert_eq!(
2339 format!("{:03?}", Enum::<u8, _, u8, f64>::UpperDebug(8)),
2340 "8",
2341 );
2342 assert_eq!(
2343 format!("{:03?}", Enum::<u8, u8, _, f64>::Align(5)),
2344 "5",
2345 );
2346 assert_eq!(
2347 format!("{:03?}", Enum::<u8, u8, _, f64>::Sign(5)),
2348 "+5",
2349 );
2350 assert_eq!(
2351 format!("{:07?}", Enum::<u8, u8, _, f64>::Alternate(5)),
2352 "0b101",
2353 );
2354 assert_eq!(
2355 format!("{:07?}", Enum::<u8, u8, _, f64>::ZeroPadded(-5)),
2356 "-5",
2357 );
2358 assert_eq!(
2359 format!("{:03?}", Enum::<u8, u8, _, f64>::Width(5)),
2360 "0000005",
2361 );
2362 assert_eq!(
2363 format!("{:.3?}", Enum::<u8, u8, u8, _>::Precision(1.23456789)),
2364 "1.23457",
2365 );
2366 }
2367 }
2368 }
2369 }
2370
2371 mod r#unsized {
2372 #[cfg(not(feature = "std"))]
2373 use alloc::format;
2374 use core::ptr;
2375
2376 use derive_more::Debug;
2377
2378 #[derive(Debug)]
2379 struct Tuple<T: ?Sized>(T);
2380
2381 #[derive(Debug)]
2382 struct Struct<T: ?Sized> {
2383 tail: T,
2384 }
2385
2386 #[test]
2387 fn assert() {
2388 let dat = "14";
2389
2390 let t =
2391 unsafe { &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple<str>) };
2392 assert_eq!(format!("{t:?}"), r#"Tuple("14")"#);
2393 let s = unsafe {
2394 &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct<str>)
2395 };
2396 assert_eq!(format!("{s:?}"), r#"Struct { tail: "14" }"#);
2397 }
2398
2399 mod interpolated {
2400 #[cfg(not(feature = "std"))]
2401 use alloc::format;
2402 use core::ptr;
2403
2404 use derive_more::Debug;
2405
2406 #[derive(Debug)]
2407 #[debug("{}.", _0)]
2408 struct Tuple1<T: ?Sized>(T);
2409
2410 #[derive(Debug)]
2411 #[debug("{_0}.")]
2412 struct Tuple2<T: ?Sized>(T);
2413
2414 #[derive(Debug)]
2415 #[debug("{}.", tail)]
2416 struct Struct1<T: ?Sized> {
2417 tail: T,
2418 }
2419
2420 #[derive(Debug)]
2421 #[debug("{tail}.")]
2422 struct Struct2<T: ?Sized> {
2423 tail: T,
2424 }
2425
2426 #[test]
2427 fn assert() {
2428 let dat = "14";
2429
2430 let t1 = unsafe {
2431 &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple1<str>)
2432 };
2433 assert_eq!(format!("{t1:?}"), "14.");
2434 let t2 = unsafe {
2435 &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple2<str>)
2436 };
2437 assert_eq!(format!("{t2:?}"), "14.");
2438 let s1 = unsafe {
2439 &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct1<str>)
2440 };
2441 assert_eq!(format!("{s1:?}"), "14.");
2442 let s2 = unsafe {
2443 &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct2<str>)
2444 };
2445 assert_eq!(format!("{s2:?}"), "14.");
2446 }
2447 }
2448 }
2449 }
2450
2451 // See: https://github.com/JelteF/derive_more/issues/301
2452 mod complex_enum_syntax {
2453 #[cfg(not(feature = "std"))]
2454 use alloc::format;
2455
2456 use derive_more::Debug;
2457
2458 #[derive(Debug)]
2459 enum Enum {
2460 A = if cfg!(unix) { 2 } else { 3 },
2461 }
2462
2463 #[test]
2464 fn assert() {
2465 assert_eq!(format!("{:?}", Enum::A), "A");
2466 }
2467 }
2468
2469 // See: https://github.com/JelteF/derive_more/issues/363
2470 mod type_variables {
2471 mod our_alloc {
2472 #[cfg(not(feature = "std"))]
2473 pub use alloc::{boxed::Box, format, vec, vec::Vec};
2474 #[cfg(not(feature = "std"))]
2475 pub use core::iter;
2476 #[cfg(feature = "std")]
2477 pub use std::{boxed::Box, format, iter, vec, vec::Vec};
2478 }
2479
2480 use our_alloc::{format, iter, vec, Box, Vec};
2481
2482 use derive_more::Debug;
2483
2484 #[derive(Debug)]
2485 struct ItemStruct {
2486 next: Option<Box<ItemStruct>>,
2487 }
2488
2489 #[derive(Debug)]
2490 struct ItemTuple(Option<Box<ItemTuple>>);
2491
2492 #[derive(Debug)]
2493 #[debug("Item({_0:?})")]
2494 struct ItemTupleContainerFmt(Option<Box<ItemTupleContainerFmt>>);
2495
2496 #[derive(Debug)]
2497 enum ItemEnum {
2498 Node { children: Vec<ItemEnum>, inner: i32 },
2499 Leaf { inner: i32 },
2500 }
2501
2502 #[derive(Debug)]
2503 struct VecMeansDifferent<Vec> {
2504 next: our_alloc::Vec<i32>,
2505 real: Vec,
2506 }
2507
2508 #[derive(Debug)]
2509 struct Array<T> {
2510 #[debug("{t}")]
2511 t: [T; 10],
2512 }
2513
2514 mod parens {
2515 #![allow(unused_parens)] // test that type is found even in parentheses
2516
2517 use derive_more::Debug;
2518
2519 #[derive(Debug)]
2520 struct Paren<T> {
2521 t: (T),
2522 }
2523 }
2524
2525 #[derive(Debug)]
2526 struct ParenthesizedGenericArgumentsInput<T> {
2527 t: dyn Fn(T) -> i32,
2528 }
2529
2530 #[derive(Debug)]
2531 struct ParenthesizedGenericArgumentsOutput<T> {
2532 t: dyn Fn(i32) -> T,
2533 }
2534
2535 #[derive(Debug)]
2536 struct Ptr<T> {
2537 t: *const T,
2538 }
2539
2540 #[derive(Debug)]
2541 struct Reference<'a, T> {
2542 t: &'a T,
2543 }
2544
2545 #[derive(Debug)]
2546 struct Slice<'a, T> {
2547 t: &'a [T],
2548 }
2549
2550 #[derive(Debug)]
2551 struct BareFn<T> {
2552 t: Box<fn(T) -> T>,
2553 }
2554
2555 #[derive(Debug)]
2556 struct Tuple<T> {
2557 t: Box<(T, T)>,
2558 }
2559
2560 trait MyTrait<T> {}
2561
2562 #[derive(Debug)]
2563 struct TraitObject<T> {
2564 t: Box<dyn MyTrait<T>>,
2565 }
2566
2567 #[derive(Debug)]
2568 struct AssocType<I: Iterator> {
2569 iter: I,
2570 elem: Option<I::Item>,
2571 }
2572
2573 #[derive(derive_more::Debug)]
2574 struct CollidedPathName<Item> {
2575 item: Item,
2576 elem: Option<some_path::Item>,
2577 }
2578
2579 mod some_path {
2580 use super::Debug;
2581
2582 #[derive(Debug)]
2583 pub struct Item;
2584 }
2585
2586 #[test]
2587 fn assert() {
2588 assert_eq!(
2589 format!(
2590 "{:?}",
2591 ItemStruct {
2592 next: Some(Box::new(ItemStruct { next: None }))
2593 },
2594 ),
2595 "ItemStruct { next: Some(ItemStruct { next: None }) }",
2596 );
2597
2598 assert_eq!(
2599 format!("{:?}", ItemTuple(Some(Box::new(ItemTuple(None))))),
2600 "ItemTuple(Some(ItemTuple(None)))",
2601 );
2602
2603 assert_eq!(
2604 format!(
2605 "{:?}",
2606 ItemTupleContainerFmt(Some(Box::new(ItemTupleContainerFmt(None)))),
2607 ),
2608 "Item(Some(Item(None)))",
2609 );
2610
2611 let item = ItemEnum::Node {
2612 children: vec![
2613 ItemEnum::Node {
2614 children: vec![],
2615 inner: 0,
2616 },
2617 ItemEnum::Leaf { inner: 1 },
2618 ],
2619 inner: 2,
2620 };
2621 assert_eq!(
2622 format!("{item:?}"),
2623 "Node { children: [Node { children: [], inner: 0 }, Leaf { inner: 1 }], inner: 2 }",
2624 );
2625
2626 assert_eq!(
2627 format!(
2628 "{:?}",
2629 AssocType {
2630 iter: iter::empty::<bool>(),
2631 elem: None,
2632 },
2633 ),
2634 "AssocType { iter: Empty, elem: None }",
2635 );
2636
2637 assert_eq!(
2638 format!(
2639 "{:?}",
2640 CollidedPathName {
2641 item: true,
2642 elem: None,
2643 },
2644 ),
2645 "CollidedPathName { item: true, elem: None }",
2646 );
2647 }
2648 }