bitfields 2.0.1

Macro for for generating bitfields from structs and enum bitflags for defining packed binary schemas in low-level systems (e.g. embedded development or emulators)
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
#[cfg(test)]
mod feature_flag_tests {

    #[cfg(feature = "order_lsb")]
    #[test]
    fn feature_order_lsb_default_packing() {
        use bitfields::bitfield;

        #[bitfield(u16)]
        pub struct Bitfield {
            #[bits(default = 0xAB)]
            low: u8,
            #[bits(default = 0xCD)]
            high: u8,
        }

        let bf = Bitfield::new();
        assert_eq!(bf.low(), 0xAB, "low byte should be in bits [7..=0]");
        assert_eq!(bf.high(), 0xCD, "high byte should be in bits [15..=8]");
        assert_eq!(
            bf.into_bits(),
            0xCDAB,
            "LSB order: low field in lower byte, high field in upper byte"
        );
    }

    #[cfg(all(feature = "order_msb", not(feature = "order_lsb")))]
    #[test]
    fn feature_order_msb_default_packing() {
        use bitfields::bitfield;

        #[bitfield(u16)]
        pub struct Bitfield {
            #[bits(default = 0xAB)]
            high: u8,
            #[bits(default = 0xCD)]
            low: u8,
        }

        let bf = Bitfield::new();
        assert_eq!(bf.high(), 0xAB, "first field should be in most-significant byte");
        assert_eq!(bf.low(), 0xCD, "second field should be in least-significant byte");
        assert_eq!(
            bf.into_bits(),
            0xABCD,
            "MSB order: first field in upper byte, second field in lower byte"
        );
    }

    #[cfg(feature = "generate_new")]
    #[test]
    fn feature_generate_new_creates_with_defaults() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x11)]
            a: u8,
            #[bits(default = 0x22)]
            b: u8,
            #[bits(default = 0x33)]
            c: u8,
            #[bits(default = 0x44)]
            d: u8,
        }

        let bf = Bitfield::new();
        assert_eq!(bf.a(), 0x11);
        assert_eq!(bf.b(), 0x22);
        assert_eq!(bf.c(), 0x33);
        assert_eq!(bf.d(), 0x44);
        assert_eq!(bf.into_bits(), 0x44332211);
    }

    #[cfg(feature = "generate_new")]
    #[test]
    fn feature_generate_new_without_defaults_zeroes_non_reserved() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x11)]
            a: u8,
            #[bits(default = 0x22)]
            b: u8,
            #[bits(default = 0x33)]
            c: u8,

            #[bits(default = 0x44)]
            _reserved: u8,
        }

        let bf = Bitfield::new_without_defaults();
        assert_eq!(bf.a(), 0x00, "non-reserved field a should be zeroed");
        assert_eq!(bf.b(), 0x00, "non-reserved field b should be zeroed");
        assert_eq!(bf.c(), 0x00, "non-reserved field c should be zeroed");
        assert_eq!(bf.into_bits(), 0x44_00_00_00, "reserved field must keep its default 0x44");
    }

    #[cfg(feature = "generate_from_into_bits")]
    #[test]
    fn feature_generate_from_bits_round_trips() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            a: u8,
            b: u8,
            c: u8,
            d: u8,
        }

        let raw: u32 = 0xDEAD_BEEF;
        let bf = Bitfield::from_bits(raw);
        assert_eq!(bf.into_bits(), raw, "from_bits → into_bits must be the identity");
    }

    #[cfg(feature = "generate_from_into_bits")]
    #[test]
    fn feature_generate_from_bits_with_defaults_applies_defaults() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0xFF)]
            a: u8,
            b: u8,
            c: u8,
            #[bits(default = 0xFF)]
            d: u8,
        }

        let bf = Bitfield::from_bits_with_defaults(0x11_22_33_44);
        assert_eq!(bf.a(), 0xFF, "a should be replaced by its default 0xFF");
        assert_eq!(bf.b(), 0x33, "b has no default – raw bits[15:8] kept");
        assert_eq!(bf.c(), 0x22, "c has no default – raw bits[23:16] kept");
        assert_eq!(bf.d(), 0xFF, "d should be replaced by its default 0xFF");
        assert_eq!(bf.into_bits(), 0xFF_22_33_FF);
    }

    #[cfg(feature = "from_endian_big")]
    #[test]
    fn feature_from_endian_big_no_byte_swap() {
        use bitfields::bitfield;
        #[bitfield(u32, from_endian = big)]
        pub struct Bitfield {
            a: u8,
            b: u8,
            c: u8,
            d: u8,
        }

        let bf = Bitfield::from_bits(0x11_22_33_44);
        assert_eq!(bf.a(), 0x44, "LSB-order: a = bits[7..=0] = 0x44");
        assert_eq!(bf.b(), 0x33);
        assert_eq!(bf.c(), 0x22);
        assert_eq!(bf.d(), 0x11);
    }

    #[cfg(feature = "from_endian_little")]
    #[test]
    fn feature_from_endian_little_byte_swap() {
        use bitfields::bitfield;
        #[bitfield(u32, from_endian = little)]
        pub struct Bitfield {
            a: u8,
            b: u8,
            c: u8,
            d: u8,
        }

        let bf = Bitfield::from_bits(0x11_22_33_44);
        assert_eq!(bf.a(), 0x11, "LE from_bits: a should be 0x11 after byte swap");
        assert_eq!(bf.b(), 0x22);
        assert_eq!(bf.c(), 0x33);
        assert_eq!(bf.d(), 0x44);
    }

    #[cfg(feature = "into_endian_big")]
    #[test]
    fn feature_into_endian_big_no_byte_swap() {
        use bitfields::bitfield;
        #[bitfield(u32, from_endian = big, into_endian = big)]
        pub struct Bitfield {
            #[bits(default = 0x11)]
            a: u8,
            #[bits(default = 0x22)]
            b: u8,
            #[bits(default = 0x33)]
            c: u8,
            #[bits(default = 0x44)]
            d: u8,
        }

        let bf = Bitfield::new();

        assert_eq!(bf.into_bits(), 0x44_33_22_11, "BE into_bits should not byte-swap");
    }

    #[cfg(feature = "into_endian_little")]
    #[test]
    fn feature_into_endian_little_byte_swap() {
        use bitfields::bitfield;
        #[bitfield(u32, from_endian = big, into_endian = little)]
        pub struct Bitfield {
            #[bits(default = 0x11)]
            a: u8,
            #[bits(default = 0x22)]
            b: u8,
            #[bits(default = 0x33)]
            c: u8,
            #[bits(default = 0x44)]
            d: u8,
        }

        let bf = Bitfield::new();

        assert_eq!(bf.into_bits(), 0x11_22_33_44, "LE into_bits should byte-swap the result");
    }

    #[cfg(feature = "generate_from_traits")]
    #[test]
    fn feature_generate_from_traits_round_trips() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0xAA)]
            a: u8,
            #[bits(default = 0xBB)]
            b: u8,
            #[bits(default = 0xCC)]
            c: u8,
            #[bits(default = 0xDD)]
            d: u8,
        }

        let raw: u32 = 0xDDCCBBAA;

        let bf = Bitfield::from(raw);
        assert_eq!(bf.a(), 0xAA);
        assert_eq!(bf.b(), 0xBB);
        assert_eq!(bf.c(), 0xCC);
        assert_eq!(bf.d(), 0xDD);

        let recovered: u32 = bf.into();
        assert_eq!(recovered, raw, "From<Bitfield> for u32 should recover the raw value");
    }

    #[cfg(feature = "generate_default")]
    #[test]
    fn feature_generate_default_matches_new() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x10)]
            a: u8,
            #[bits(default = 0x20)]
            b: u8,
            #[bits(default = 0x30)]
            c: u8,
            #[bits(default = 0x40)]
            d: u8,
        }

        let via_new = Bitfield::new();
        let via_default = Bitfield::default();
        assert_eq!(
            via_new.into_bits(),
            via_default.into_bits(),
            "Default::default() and new() must produce identical bitfields"
        );
        assert_eq!(via_default.a(), 0x10);
        assert_eq!(via_default.b(), 0x20);
        assert_eq!(via_default.c(), 0x30);
        assert_eq!(via_default.d(), 0x40);
    }

    #[cfg(feature = "generate_debug")]
    #[test]
    fn feature_generate_debug_format_succeeds() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0xAB)]
            a: u8,
            #[bits(default = 0xCD)]
            b: u8,
            #[bits(8)]
            c: u8,
            #[bits(8)]
            d: u8,
        }

        let bf = Bitfield::new();
        let s = format!("{:?}", bf);
        assert!(s.contains("Bitfield"), "Debug output should include the struct name");
        assert!(s.contains("a"), "Debug output should include field 'a'");
        assert!(s.contains("b"), "Debug output should include field 'b'");
    }

    #[cfg(feature = "derive_copy")]
    #[test]
    fn feature_derive_copy_bitfield_is_copy() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0xAA)]
            a: u8,
            #[bits(default = 0xBB)]
            b: u8,
            #[bits(default = 0xCC)]
            c: u8,
            #[bits(default = 0xDD)]
            d: u8,
        }

        let original = Bitfield::new();
        let copy = original;

        assert_eq!(original.into_bits(), copy.into_bits());
    }

    #[cfg(feature = "generate_builder")]
    #[test]
    fn feature_generate_builder_applies_defaults() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x12)]
            a: u8,
            #[bits(default = 0x34)]
            b: u8,
            #[bits(default = 0x56)]
            c: u8,
            #[bits(default = 0x78)]
            d: u8,
        }

        let bf = BitfieldBuilder::new().build();
        assert_eq!(bf.a(), 0x12);
        assert_eq!(bf.b(), 0x34);
        assert_eq!(bf.c(), 0x56);
        assert_eq!(bf.d(), 0x78);
        assert_eq!(bf.into_bits(), 0x78563412);
    }

    #[cfg(feature = "generate_builder")]
    #[test]
    fn feature_generate_builder_with_field_overrides_default() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x12)]
            a: u8,
            #[bits(default = 0x34)]
            b: u8,
            #[bits(default = 0x56)]
            c: u8,
            #[bits(default = 0x78)]
            d: u8,
        }

        let bf = BitfieldBuilder::new().with_a(0xFF).with_d(0x00).build();
        assert_eq!(bf.a(), 0xFF, "with_a(0xFF) should override default 0x12");
        assert_eq!(bf.b(), 0x34, "b should keep its default");
        assert_eq!(bf.c(), 0x56, "c should keep its default");
        assert_eq!(bf.d(), 0x00, "with_d(0x00) should override default 0x78");
    }

    #[cfg(feature = "generate_bit_ops")]
    #[test]
    fn feature_generate_bit_ops_get_and_set() {
        use bitfields::bitfield;
        #[bitfield(u8)]
        pub struct Bitfield {
            #[bits(default = 0b1010_1010)]
            val: u8,
        }

        let mut bf = Bitfield::new();

        assert!(!bf.get_bit(0), "bit 0 should be 0");
        assert!(bf.get_bit(1), "bit 1 should be 1");
        assert!(!bf.get_bit(2), "bit 2 should be 0");
        assert!(bf.get_bit(3), "bit 3 should be 1");

        bf.set_bit(0, true);
        assert!(bf.get_bit(0), "bit 0 should be 1 after set_bit");

        bf.set_bit(1, false);
        assert!(!bf.get_bit(1), "bit 1 should be 0 after set_bit");
    }

    #[cfg(feature = "generate_write_bit_ops")]
    #[test]
    fn feature_generate_write_bit_ops_write_bits() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x11)]
            a: u8,
            #[bits(default = 0x22)]
            b: u8,
            #[bits(default = 0x33)]
            c: u8,
            #[bits(default = 0x44)]
            d: u8,
        }

        let mut bf = Bitfield::new();

        bf.write_bits(0xAA_BB_CC_DD);
        assert_eq!(bf.a(), 0xDD);
        assert_eq!(bf.b(), 0xCC);
        assert_eq!(bf.c(), 0xBB);
        assert_eq!(bf.d(), 0xAA);
    }

    #[cfg(feature = "generate_write_bit_ops")]
    #[test]
    fn feature_generate_write_bit_ops_preserves_read_only_fields() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x12)]
            a: u8,
            #[bits(default = 0x34)]
            b: u8,
            #[bits(default = 0x56)]
            c: u8,

            #[bits(default = 0x78)]
            _reserved: u8,
        }

        let mut bf = Bitfield::new();
        bf.write_bits(0x11_22_33_44);
        assert_eq!(bf.a(), 0x44);
        assert_eq!(bf.b(), 0x33);
        assert_eq!(bf.c(), 0x22);
        assert_eq!(
            bf.into_bits(),
            0x78_22_33_44,
            "reserved field must be unchanged after write_bits"
        );
    }

    #[cfg(feature = "write_endian_big")]
    #[test]
    fn feature_write_endian_big_write_be_bits() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            a: u8,
            b: u8,
            c: u8,
            d: u8,
        }

        let mut bf = Bitfield::new();
        bf.write_be_bits(0x11_22_33_44);
        assert_eq!(bf.a(), 0x44);
        assert_eq!(bf.b(), 0x33);
        assert_eq!(bf.c(), 0x22);
        assert_eq!(bf.d(), 0x11);
    }

    #[cfg(feature = "write_endian_little")]
    #[test]
    fn feature_write_endian_little_write_le_bits() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            a: u8,
            b: u8,
            c: u8,
            d: u8,
        }

        let mut bf = Bitfield::new();
        bf.write_le_bits(0x11_22_33_44);
        assert_eq!(bf.a(), 0x11, "LE write: a should be 0x11");
        assert_eq!(bf.b(), 0x22);
        assert_eq!(bf.c(), 0x33);
        assert_eq!(bf.d(), 0x44);
    }

    #[cfg(feature = "generate_clear_bit_ops")]
    #[test]
    fn feature_generate_clear_bits_zeroes_all_fields() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x12)]
            a: u8,
            #[bits(default = 0x34)]
            b: u8,
            #[bits(default = 0x56)]
            c: u8,
            #[bits(default = 0x78)]
            d: u8,
        }

        let mut bf = Bitfield::new();
        bf.clear_bits();
        assert_eq!(bf.a(), 0, "a should be zeroed");
        assert_eq!(bf.b(), 0, "b should be zeroed");
        assert_eq!(bf.c(), 0, "c should be zeroed");
        assert_eq!(bf.d(), 0, "d should be zeroed");
        assert_eq!(bf.into_bits(), 0);
    }

    #[cfg(feature = "generate_clear_bit_ops")]
    #[test]
    fn feature_generate_clear_bits_with_defaults_restores_defaults() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x12)]
            a: u8,
            b: u8,
            c: u8,
            #[bits(default = 0x78)]
            d: u8,
        }

        let mut bf = Bitfield::new();
        bf.clear_bits_with_defaults();
        assert_eq!(bf.a(), 0x12, "a must be restored to its default 0x12");
        assert_eq!(bf.b(), 0x00, "b has no default → stays zero");
        assert_eq!(bf.c(), 0x00, "c has no default → stays zero");
        assert_eq!(bf.d(), 0x78, "d must be restored to its default 0x78");
        assert_eq!(bf.into_bits(), 0x78_00_00_12);
    }

    #[cfg(feature = "generate_clear_bit_ops")]
    #[test]
    fn feature_generate_clear_bits_preserves_reserved_field() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x12)]
            a: u8,
            #[bits(default = 0x34)]
            b: u8,
            #[bits(default = 0x56)]
            c: u8,
            #[bits(default = 0x78)]
            _reserved: u8,
        }

        let mut bf = Bitfield::new();
        bf.clear_bits();
        assert_eq!(bf.a(), 0, "a should be zero after clear_bits");
        assert_eq!(bf.b(), 0, "b should be zero after clear_bits");
        assert_eq!(bf.c(), 0, "c should be zero after clear_bits");
        assert_eq!(
            bf.into_bits(),
            0x78_00_00_00,
            "reserved field must retain its default 0x78 after clear_bits"
        );
    }

    #[cfg(feature = "generate_set_get_bit_ops")]
    #[test]
    fn feature_generate_get_bits_range_reads_correct_slice() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0xAB)]
            a: u8,
            #[bits(default = 0xCD)]
            b: u8,
            #[bits(default = 0xEF)]
            c: u8,
            #[bits(default = 0x12)]
            d: u8,
        }

        let bf = Bitfield::new();
        assert_eq!(bf.get_bits_range(0, 8), 0xAB, "bits[7..=0] should be 0xAB");
        assert_eq!(bf.get_bits_range(8, 8), 0xCD, "bits[15..=8] should be 0xCD");
        assert_eq!(bf.get_bits_range(16, 8), 0xEF, "bits[23..=16] should be 0xEF");
        assert_eq!(bf.get_bits_range(24, 8), 0x12, "bits[31..=24] should be 0x12");
    }

    #[cfg(feature = "generate_set_get_bit_ops")]
    #[test]
    fn feature_generate_set_bits_range_writes_correct_slice() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            a: u8,
            b: u8,
            c: u8,
            d: u8,
        }

        let mut bf = Bitfield::new();
        bf.set_bits_range(0, 8, 0x11);
        bf.set_bits_range(8, 8, 0x22);
        bf.set_bits_range(16, 8, 0x33);
        bf.set_bits_range(24, 8, 0x44);

        assert_eq!(bf.a(), 0x11);
        assert_eq!(bf.b(), 0x22);
        assert_eq!(bf.c(), 0x33);
        assert_eq!(bf.d(), 0x44);
        assert_eq!(bf.into_bits(), 0x44332211);
    }

    #[cfg(feature = "generate_invert_bit_ops")]
    #[test]
    fn feature_generate_invert_bit_ops_invert_field() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x0F)]
            a: u8,
            #[bits(default = 0xF0)]
            b: u8,
            #[bits(default = 0x00)]
            c: u8,
            #[bits(default = 0xFF)]
            d: u8,
        }

        let mut bf = Bitfield::new();
        bf.invert_a();
        assert_eq!(bf.a(), 0xF0, "invert_a: 0x0F → 0xF0");
        bf.invert_b();
        assert_eq!(bf.b(), 0x0F, "invert_b: 0xF0 → 0x0F");
        bf.invert_c();
        assert_eq!(bf.c(), 0xFF, "invert_c: 0x00 → 0xFF");
        bf.invert_d();
        assert_eq!(bf.d(), 0x00, "invert_d: 0xFF → 0x00");
    }

    #[cfg(feature = "generate_invert_bit_ops")]
    #[test]
    fn feature_generate_invert_bit_ops_invert_bits_whole() {
        use bitfields::bitfield;
        #[bitfield(u32)]
        pub struct Bitfield {
            #[bits(default = 0x00)]
            a: u8,
            #[bits(default = 0xFF)]
            b: u8,
            #[bits(default = 0x0F)]
            c: u8,
            #[bits(default = 0xF0)]
            d: u8,
        }

        let mut bf = Bitfield::new();
        bf.invert_bits();
        assert_eq!(bf.a(), 0xFF);
        assert_eq!(bf.b(), 0x00);
        assert_eq!(bf.c(), 0xF0);
        assert_eq!(bf.d(), 0x0F);
    }

    #[cfg(feature = "generate_invert_bit_ops")]
    #[test]
    fn feature_generate_invert_bit_ops_bool_field() {
        use bitfields::bitfield;
        #[bitfield(u8)]
        pub struct Bitfield {
            #[bits(default = true)]
            flag_a: bool,
            #[bits(default = false)]
            flag_b: bool,
            #[bits(6)]
            _padding: u8,
        }

        let mut bf = Bitfield::new();
        assert!(bf.flag_a(), "flag_a should start true");
        assert!(!bf.flag_b(), "flag_b should start false");

        bf.invert_flag_a();
        bf.invert_flag_b();
        assert!(!bf.flag_a(), "flag_a should be false after invert");
        assert!(bf.flag_b(), "flag_b should be true after invert");
    }
}