[go: up one dir, main page]

defmt 0.1.3

A highly efficient logging framework that targets resource-constrained devices, like microcontrollers
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
// NOTE these tests should live in `defmt-macros` but the expansion of the macros defined there
// depend on `defmt` and `defmt` depends on `defmt-macros` -- the circular dependency may get in
// the way of `cargo test`

// NOTE string interning is mocked on x86 to aid testing so it does not do real interning. Instead
// the "interner" always returns a **7-bit** `u8` value that's bumped on every interning operation.
//
// In practice, this means that the following operation:
// ```
// fn foo(f: &mut Formatter) {
//     write!(f, "Hello")
// }
// ```
// writes a *different* index on each call. With real interning this operation always writes the
// same index.
//
// `fetch_string_index` returns the current index of the mocked interner. Use this
// when writing the expected output of a unit test.
//
// ```
// let mut f = Formatter::new();
// let index = defmt::export::fetch_string_index();
// foo(&mut f); // NOTE increases the interner index
// assert_eq!(f.bytes(), &[index]);
//
// let mut f = Formatter::new();
// foo(&mut f);
// assert_eq!(f.bytes(), &[index.wrapping_add(1)]);
//                               ^^^^^^^^^^^^^^^ account for the previous `foo` call
// ```
//
// The mocked string index is thread local so you can run unit tests in parallel.
// `fetch_string_index` returns the thread-local interner index.
//
// Additional notes:
//
// - the mocked index is 7 bits so its LEB128 encoding is the input byte
// - the family of `info!` macros do nothing on x86; instead use `write!` which takes a formatter
//   argument

use defmt::{export::fetch_string_index, write, Format, Formatter};

// Increase the 7-bit mocked interned index
fn inc(index: u8, n: u8) -> u8 {
    // NOTE(&) keep the highest bit at 0
    index.wrapping_add(n) & 0x7F
}

fn check_format_implementation(val: &(impl Format + ?Sized), expected_encoding: &[u8]) {
    let mut f = Formatter::new();
    val.format(&mut f);
    f.finalize();
    assert_eq!(f.bytes(), expected_encoding);
}

#[test]
fn write() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(f, "The answer is {:u8}", 42);
    assert_eq!(
        f.bytes(),
        &[
            index, // "The answer is {:u8}",
            42,    // u8 value
        ]
    );

    let ref mut f = Formatter::new();
    write!(f, "The answer is {:?}", 42u8);
    assert_eq!(
        f.bytes(),
        &[
            inc(index, 1), // "The answer is {:?}"
            inc(index, 2), // "{:u8}" / impl Format for u8
            42,            // u8 value
        ]
    );
}

#[test]
fn booleans_max_num_bool_flags() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(
        f,
        "encode 8 bools {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool}",
        false, true, true, false, true, false, true, true
    );
    assert_eq!(
        f.bytes(),
        &[
            index,       // "encode 8 bools {:bool} {:bool} [...]",
            0b0110_1011, // compressed bools (dec value = 107)
        ]
    );
}

#[test]
fn booleans_less_than_max_num_bool_flags() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(
        f,
        "encode 3 bools {:bool} {:bool} {:bool}",
        false, true, true
    );
    assert_eq!(
        f.bytes(),
        &[
            index, // "encode 3 bools {:bool} {:bool} {:bool}",
            0b011, // compressed bools
        ]
    );
}

#[test]
fn booleans_more_than_max_num_bool_flags() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(f, "encode 9 bools {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool}",
           false, true, true, false, true, false, true, true, false, true);
    assert_eq!(
        f.bytes(),
        &[
            index,       // "encode 8 bools {:bool} {:bool} {:bool} [...]",
            0b0110_1011, // first 8 compressed bools
            0b01,        // final compressed bools
        ]
    );
}

#[test]
fn booleans_mixed() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(
        f,
        "encode mixed bools {:bool} {:bool} {:u8} {:bool}",
        true, false, 42, true
    );
    assert_eq!(
        f.bytes(),
        &[
            index, // "encode mixed bools {:bool} {:bool} {:u8} {:bool}",
            42u8,  // intermediate `42`
            0b101, // all compressed bools
        ]
    );
}

#[test]
fn booleans_mixed_no_trailing_bool() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(f, "encode mixed bools {:bool} {:u8}", false, 42);
    assert_eq!(
        f.bytes(),
        &[
            index, // "encode mixed bools {:bool} {:u8}",
            42u8, 0b0, // bool is put at the end of the args
        ]
    );
}

#[test]
fn bitfields_mixed() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(
        f,
        "bitfields {0:7..12}, {1:0..5}",
        0b1110_0101_1111_0000u16, 0b1111_0000u8
    );
    assert_eq!(
        f.bytes(),
        &[
            index, // bitfields {0:7..12}, {1:0..5}",
            0b1111_0000,
            0b1110_0101,   // u16
            0b1111_0000u8, // u8
        ]
    );
}

#[test]
fn bitfields_across_octets() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(f, "bitfields {0:0..7} {0:9..14}", 0b0110_0011_1101_0010u16);
    assert_eq!(
        f.bytes(),
        &[
            index, // bitfields {0:0..7} {0:9..14}",
            0b1101_0010,
            0b0110_0011, // u16
        ]
    );
}

#[test]
fn bitfields_truncate_lower() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(
        f,
        "bitfields {0:9..14}",
        0b0000_0000_0000_1111_0110_0011_1101_0010u32
    );
    assert_eq!(
        f.bytes(),
        &[
            index,       // bitfields {0:9..14}",
            0b0110_0011, // the first octet should have been truncated away
        ]
    );
}

#[test]
fn bitfields_assert_range_exclusive() {
    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(f, "bitfields {0:6..8}", 0b1010_0101u8,);
    assert_eq!(
        f.bytes(),
        &[
            index, // "bitfields {0:6..8}"
            0b1010_0101
        ]
    );
}

#[test]
fn boolean_struct() {
    #[derive(Format)]
    struct X {
        y: bool,
        z: bool,
    }

    let index = fetch_string_index();
    check_format_implementation(
        &X { y: false, z: true },
        &[
            index, // "X {{ x: {:bool}, y: {:bool} }}"
            0b01,  // y and z compressed together
        ],
    )
}

#[test]
fn boolean_struct_mixed() {
    #[derive(Format)]
    struct X {
        y: bool,
        z: bool,
    }

    let index = fetch_string_index();
    let ref mut f = Formatter::new();

    write!(
        f,
        "mixed formats {:bool} {:?}",
        true,
        X { y: false, z: true }
    );
    assert_eq!(
        f.bytes(),
        &[
            index,         // "mixed formats {:bool} {:?}",
            inc(index, 1), // "X {{ x: {:bool}, y: {:bool} }}"
            0b101,         // compressed struct bools
        ]
    );
}

#[test]
fn single_struct() {
    #[derive(Format)]
    struct X {
        y: u8,
        z: u16,
    }

    let index = fetch_string_index();
    check_format_implementation(
        &X { y: 1, z: 2 },
        &[
            index, // "X {{ x: {:u8}, y: {:u16} }}"
            1,     // x
            2,     // y.low
            0,     // y.high
        ],
    )
}

#[test]
fn single_struct_manual() {
    // Above `#[derive]`d impl should be equivalent to this:
    struct X {
        y: u8,
        z: u16,
    }

    impl Format for X {
        fn format(&self, f: &mut Formatter) {
            defmt::write!(f, "X {{ x: {:u8}, y: {:u16} }}", self.y, self.z)
        }
    }

    let index = fetch_string_index();
    check_format_implementation(
        &X { y: 1, z: 2 },
        &[
            index, // "X {{ x: {:u8}, y: {:u16} }}"
            1,     // x
            2,     // y.low
            0,     // y.high
        ],
    )
}

#[test]
fn nested_struct() {
    #[derive(Format)]
    struct X {
        y: Y,
    }

    #[derive(Format)]
    struct Y {
        z: u8,
    }

    let val = 42;
    let index = fetch_string_index();
    check_format_implementation(
        &X { y: Y { z: val } },
        &[
            index,         // "X {{ y: {:?} }}"
            inc(index, 1), // "Y {{ z: {:u8} }}"
            val,
        ],
    );
}

#[test]
fn tuple_struct() {
    #[derive(Format)]
    struct Struct(u8, u16);

    let index = fetch_string_index();
    check_format_implementation(
        &Struct(0x1f, 0xaaaa),
        &[
            index, // "Struct({:u8}, {:u16})"
            0x1f,  // u8
            0xaa, 0xaa, // u16
        ],
    );
}

#[test]
fn c_like_enum() {
    #[derive(Format)]
    #[allow(dead_code)]
    enum Enum {
        A,
        B,
        C,
    }

    let index = fetch_string_index();
    check_format_implementation(
        &Enum::A,
        &[
            index, //
            0,     // `Enum::A`
        ],
    );
    let index = fetch_string_index();
    check_format_implementation(
        &Enum::B,
        &[
            index, //
            1,     // `Enum::B`
        ],
    );
}

#[test]
fn uninhabited_enum() {
    #[derive(Format)]
    enum Void {}
}

/// Tests that univariant enums do not encode the discriminant (since the variant in use is always
/// the same).
#[test]
fn univariant_enum() {
    #[derive(Format)]
    enum NoData {
        Variant,
    }

    let index = fetch_string_index();
    check_format_implementation(
        &NoData::Variant,
        &[
            index, //
        ],
    );

    #[derive(Format)]
    enum Data {
        Variant(u8, u16),
    }

    let index = fetch_string_index();
    check_format_implementation(
        &Data::Variant(0x1f, 0xaaaa),
        &[
            index, //
            0x1f,  // u8
            0xaa, 0xaa, // u16
        ],
    );
}

#[test]
fn nested_enum() {
    #[derive(Format)]
    #[allow(dead_code)]
    enum CLike {
        A,
        B,
        C,
    }

    #[derive(Format)]
    enum Inner {
        A(CLike, u8),
        _B,
    }

    #[derive(Format)]
    enum Outer {
        Variant1 { pre: u8, inner: Inner, post: u8 },
        Variant2,
        Variant3(Inner),
    }

    let index = fetch_string_index();
    check_format_implementation(
        &Outer::Variant1 {
            pre: 0xEE,
            inner: Inner::A(CLike::B, 0x07),
            post: 0xAB,
        },
        &[
            index,         //
            0,             // `Outer::Variant1`
            0xEE,          // u8 pre
            inc(index, 1), // `Inner`'s formatting string
            0,             // `Inner::A`
            inc(index, 2), // `CLike`'s formatting string
            1,             // `CLike::B`
            0x07,          // u8
            0xAB,          // u8 post
        ],
    );

    let index = fetch_string_index();
    check_format_implementation(
        &Outer::Variant2,
        &[
            index, //
            1,     // `Outer::Variant2`
        ],
    );

    let index = fetch_string_index();
    check_format_implementation(
        &Outer::Variant3(Inner::A(CLike::B, 0x07)),
        &[
            index,         //
            2,             // `Outer::Variant3`
            inc(index, 1), // `Inner`'s formatting string
            0,             // `Inner::A`
            inc(index, 2), // `CLike`'s formatting string
            1,             // `CLike::B`
            0x07,          // u8
        ],
    );
}

#[test]
fn slice() {
    let index = fetch_string_index();
    let val: &[u8] = &[23u8, 42u8];
    check_format_implementation(
        val,
        &[
            index,           // "{:[?]}"
            val.len() as u8, // length
            inc(index, 1),   // "{:u8}"
            23,              // val[0]
            42,              // val[1]
        ],
    )
}

#[test]
fn slice_of_usize() {
    let index = fetch_string_index();
    let val: &[usize] = &[23usize, 42];
    check_format_implementation(
        val,
        &[
            index,           // "{:[?]}"
            val.len() as u8, // length
            inc(index, 1),   // "{:usize}"
            23,              // val[0]
            42,              // val[1]
        ],
    )
}

#[test]
fn slice_of_bools() {
    let index = fetch_string_index();
    let val: &[bool] = &[true, true, false];
    check_format_implementation(
        val,
        &[
            index,           // "{:[?]}"
            val.len() as u8, // length
            inc(index, 1),   // "{:bool}"
            0b110,           // compressed bools: true, true, false
        ],
    )
}

#[test]
fn format_primitives() {
    let index = fetch_string_index();
    check_format_implementation(
        &42u8,
        &[
            index, // "{:u8}"
            42,
        ],
    );
    check_format_implementation(
        &42u16,
        &[
            inc(index, 1), // "{:u16}"
            42,
            0,
        ],
    );
    check_format_implementation(
        &513u16,
        &[
            inc(index, 2), // "{:u16}"
            1,
            2,
        ],
    );

    check_format_implementation(
        &42u32,
        &[
            inc(index, 3), // "{:u32}"
            42,
            0,
            0,
            0,
        ],
    );
    check_format_implementation(
        &513u32,
        &[
            inc(index, 4), // "{:u32}"
            1,
            2,
            0,
            0,
        ],
    );

    check_format_implementation(
        &5.13f32,
        &[
            inc(index, 5), // "{:f32}"
            246,
            40,
            164,
            64,
        ],
    );

    check_format_implementation(
        &42i8,
        &[
            inc(index, 6), // "{:i8}"
            42,
        ],
    );
    check_format_implementation(
        &-42i8,
        &[
            inc(index, 7), // "{:i8}"
            -42i8 as u8,
        ],
    );

    check_format_implementation(
        &None::<u8>,
        &[
            inc(index, 8), // "<option-format-string>"
            0,             // None discriminant
        ],
    );

    check_format_implementation(
        &Some(42u8),
        &[
            inc(index, 9),  // "<option-format-string>"
            1,              // Some discriminant
            inc(index, 10), // "{:u8}"
            42,             // Some.0 field
        ],
    );

    check_format_implementation(&-1isize, &[inc(index, 11), 0b0000_0001]);
    check_format_implementation(&-128isize, &[inc(index, 12), 0xff, 0b0000_0001]);

    check_format_implementation(
        &true,
        &[
            inc(index, 13), // "{:bool}"
            0b1,
        ],
    );

    check_format_implementation(
        &513u64,
        &[
            inc(index, 14), // "{:u64}"
            1,
            2,
            0,
            0,
            0,
            0,
            0,
            0,
        ],
    );

    check_format_implementation(
        &-2i64,
        &[
            inc(index, 15), // "{:i64}"
            0xFE,
            0xFF,
            0xFF,
            0xFF,
            0xFF,
            0xFF,
            0xFF,
            0xFF,
        ],
    );
}

#[test]
fn istr() {
    let index = fetch_string_index();
    let interned = defmt::intern!("interned string contents");
    check_format_implementation(
        &interned,
        &[
            inc(index, 1), // "{:istr}"
            index,
        ],
    );
}

#[test]
fn format_arrays() {
    let index = fetch_string_index();
    let array: [u16; 0] = [];
    check_format_implementation(&array, &[index]);

    let index = fetch_string_index();
    let array: [u16; 3] = [1, 256, 257];
    check_format_implementation(
        &array,
        &[
            index,         // "{:[?;3]}"
            inc(index, 1), // "{:u16}"
            1,             // [0].low
            0,             // [0].high
            0,             // [1].low
            1,             // [1].high
            1,             // [2].low
            1,             // [2].high
        ],
    );
}

#[test]
fn format_slice_of_primitives() {
    let index = fetch_string_index();
    let slice: &[u16] = &[1, 256, 257];
    check_format_implementation(
        slice,
        &[
            index,             // "{:[?]}"
            slice.len() as u8, //
            inc(index, 1),     // "{:u16}"
            1,                 // [0].low
            0,                 // [0].high
            0,                 // [1].low
            1,                 // [1].high
            1,                 // [2].low
            1,                 // [2].high
        ],
    );
}

#[test]
fn format_slice_of_structs() {
    #[derive(Format)]
    struct X {
        y: Y,
    }

    #[derive(Format)]
    struct Y {
        z: u8,
    }

    let index = fetch_string_index();
    let slice: &[_] = &[X { y: Y { z: 42 } }, X { y: Y { z: 24 } }];
    check_format_implementation(
        slice,
        &[
            index,             // "{:[?]}"
            slice.len() as u8, //
            // first element
            inc(index, 1), // "X {{ y: {:?} }}"
            inc(index, 2), // "Y {{ z: {:u8} }}"
            42,            // [0].y.z
            // second element: no tags
            24, // [1].y.z
        ],
    );
}

#[test]
fn format_slice_of_slices() {
    let index = fetch_string_index();
    let slice: &[&[u16]] = &[&[256, 257], &[258, 259, 260]];
    check_format_implementation(
        slice,
        &[
            index,             // "{:[?]}"
            slice.len() as u8, //
            // first slice
            inc(index, 1), // "{:[?]}"
            slice[0].len() as u8,
            // its first element
            inc(index, 2), // "{:u16}"
            0,             // [0][0].low
            1,             // [0][0].high
            // its second element: no tag
            1, // [0][1].low
            1, // [0][1].high
            // second slice: no tags
            slice[1].len() as u8,
            2, // [1][0].low
            1, // [1][0].high
            3, // [1][1].low
            1, // [1][1].high
            4, // [1][2].low
            1, // [1][2].high
        ],
    );
}

#[test]
fn format_slice_enum_slice() {
    let index = fetch_string_index();
    let slice: &[Option<&[u8]>] = &[None, Some(&[42, 43])];
    check_format_implementation(
        slice,
        &[
            index,             // "{:[?]}"
            slice.len() as u8, //
            // first optional slice
            inc(index, 1), // "None|Some({:?})"
            0,             // discriminant
            // second optional slice
            // omitted: "None|Some({:?})" index
            1,             // discriminant
            inc(index, 2), // "{:[?]}" (the ? behind "Some({:?})")
            2,             // length of second optional slice
            inc(index, 3), // "{:u8}" (the ? behind "{:[?]}")
            42,
            // omitted: "{:u8}" index
            43,
        ],
    );
}

#[test]
fn format_slice_enum_generic_struct() {
    #[derive(Format)]
    struct S<T> {
        x: u8,
        y: T,
    }

    let index = fetch_string_index();
    let slice: &[Option<S<u8>>] = &[None, Some(S { x: 42, y: 43 })];
    check_format_implementation(
        slice,
        &[
            index,             // "{:[?]}"
            slice.len() as u8, //
            // first optional element
            inc(index, 1), // "None|Some({:?})"
            0,             // discriminant
            // second optional element
            // omitted: "None|Some({:?})" index
            1,             // discriminant
            inc(index, 2), // "S {{ x: {:u8}, y: {:?} }}" (the ? behind "Some({:?})")
            42,            // S.x
            inc(index, 3), // "{:u8}" (the ? behind S.y)
            43,            // S. y
        ],
    );
}

#[test]
fn derive_with_bounds() {
    #[derive(Format)]
    struct S<T: Copy> {
        val: T,
    }

    #[derive(Format)]
    struct S2<'a: 'b, 'b> {
        a: &'a u8,
        b: &'b u8,
    }

    let index = fetch_string_index();
    check_format_implementation(
        &S { val: 0 },
        &[
            index,         // "S {{ val: {:?} }}"
            inc(index, 1), // "{:i32}"
            0,
            0,
            0,
            0,
        ],
    );

    let index = fetch_string_index();
    check_format_implementation(
        &S2 { a: &1, b: &2 },
        &[
            index,         // "S2 { a: {:?}, b: {:?} }}"
            inc(index, 1), // "{:u8}"
            1,
            inc(index, 2), // "{:u8}"
            2,
        ],
    );
}

#[test]
fn format_bools() {
    #[derive(Format)]
    struct A(bool);

    #[derive(Format)]
    struct B(bool);

    let index = fetch_string_index();
    check_format_implementation(
        &(A(true), B(true)),
        &[
            index,         // "({:?}, {:?})"
            inc(index, 1), // "A({:bool})"
            inc(index, 2), // "B({:bool})"
            0b11,          // compressed bools
        ],
    );
}

#[test]
fn issue_208() {
    #[derive(Format)]
    struct DhcpReprMin {
        pub broadcast: bool,
        pub a: [u8; 2],
    }

    let dhcp_repr = DhcpReprMin {
        broadcast: true,
        a: [10, 10],
    };

    let index = fetch_string_index();
    check_format_implementation(
        &dhcp_repr,
        &[
            index,         // "DhcpReprMin {{ broadcast: {:bool}, a: {:?} }}"
            inc(index, 1), // "{:[?;2]}"
            inc(index, 2), // "{:u8}"
            10,            // a[0]
            10,            // a[1]
            1,             // compressed bools
        ],
    );
}