devela/data/bit/field/
bitfield.rs

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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
// devela::data::bit::field::bit_field
//
//! the bitfield! macro.
//

#![allow(
    clippy::eq_op,
    clippy::identity_op,
    unused_comparisons,
    reason = "fixes lints for fields assertions"
)]

/// Creates a custom bit field struct.
///
/// The inner type must be an integer primitive.
///
/// The new struct already derives
/// `Clone`, `Copy`, `Debug`, `Default`, `PartialEq`, `Eq` and `Hash`.
///
/// # Features
/// This macro depends on enabling any of the `_bit_*` features. E.g. `_bit_u8`.
///
/// # Examples
/// See also the [bitfield][crate::_info::examples::bitfield] example.
///
/// ```
/// # use devela::bitfield;
/// bitfield! {
///     /// My custom bit field struct.
///     struct MyBf(u8) {
///         // single bit fields:
///         FIRST_BIT: 0;
///         SECOND_BIT: 1;
///         THIRD_BIT: 2;
///         // multi-bit fields:
///         MASK1: 0, 2;
///         MASK2: 3, 6;
///     }
/// }
/// let b = MyBf::new_zeroed();
/// assert![b.is_empty()];
/// ```
///
/// See also the [`enumset!`][crate::code::enumset] macro.
#[doc(hidden)]
#[macro_export]
macro_rules! _bitfield {
    {
        /* full syntax */

        ( // visibility qualifiers:
          custom:$vis_custom:vis,   // custom fields
          extra:$vis_extra:vis      // extra functionality
        )
        // $vis:  the visibility of the struct.
        // $name: the name of the new struct.
        // $T: the inner integer primitive type (u8, i32, …).
        $(#[$struct_attributes:meta])*
        $vis:vis struct $name:ident($T:ty) {
            // Custom fields (panics if $field_start > $field_end || field_end >= $T::BITS):
            // $field: the name of the field
            // $field_start: the starting bit index.
            // $field_end: the ending bit index (optional).
            $(
                $(#[$field_attrs:meta])*
                $field:ident: $field_start:expr, $field_end:expr; // NAME: from_bit, to_bit;
            )*
        }
    } => { $crate::paste! {

        $( #[$struct_attributes] )*
        #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
        $vis struct $name { bits: $T }

        /* core impls */

        impl Default for $name {
            fn default() -> Self { $name { bits: Default::default() } }
        }
        impl core::fmt::Display for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                core::fmt::Display::fmt(&self.bits, f)
            }}
        impl core::fmt::Binary for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                core::fmt::Binary::fmt(&self.bits, f)
            }}
        impl core::fmt::Octal for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                core::fmt::Octal::fmt(&self.bits, f)
            }}
        impl core::fmt::LowerHex for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                core::fmt::LowerHex::fmt(&self.bits, f)
            }}
        impl core::fmt::UpperHex for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                core::fmt::UpperHex::fmt(&self.bits, f)
            }}
        impl core::fmt::UpperExp for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                core::fmt::UpperExp::fmt(&self.bits, f)
        }}
        impl core::fmt::LowerExp for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                core::fmt::LowerExp::fmt(&self.bits, f)
        }}

        // /// # Custom fields constants
        #[allow(dead_code)]
        impl $name {
            $(
                // The field bit mask
                $( #[$field_attrs] )*
                $vis_custom const $field: Self = {
                    assert![$field_start <= $field_end,
                        "The field_start bit is bigger than the field_end bit."];
                    assert![$field_end < <$T>::BITS,
                        "There are more fields than available bits in the inner primitive type."];
                    Self {
                        bits: $crate::Bitwise::<$T>::mask_range($field_start, $field_end).0
                    }
                };
            )*
        }

        /// # Custom fields operations
        #[allow(dead_code)]
        impl $name {
            #[doc = "Returns a new `" $name "` with none of the fields set."]
            #[must_use]
            $vis_custom const fn without_fields() -> Self {
                Self { bits: 0 }
            }

            #[doc = "Returns a new `" $name "` with all the fields set."]
            #[must_use]
            $vis_custom const fn with_all_fields() -> Self {
                Self {
                    bits: 0 $(| $crate::Bitwise::<$T>::mask_range($field_start, $field_end).0)*
                }
            }

            /// Returns `true` if it all the fields are set.
            #[must_use]
            $vis_custom const fn are_all_fields(self) -> bool {
                self.bits == Self::with_all_fields().bits
            }
        }

        $(
            #[doc = "# `" $field "` single field operations"]
            #[allow(dead_code)]
            impl $name {
                /* constructor */

                #[doc = "Returns a new `" $name "` with [`" $field "`][Self::" $field
                    "] field set."]
                #[must_use]
                $vis_custom const fn [<with_field_ $field:lower>]() -> Self {
                    Self::$field
                }

                /* query */

                #[doc = "Returns `true` if the [`" $field "`][Self::" $field "] field is set."]
                #[must_use]
                $vis_custom const fn [<is_field_ $field:lower>](self) -> bool {
                    Self::contains_mask(self, Self::$field.bits)
                }

                /// Returns the size of the field in bits.
                #[must_use]
                $vis_custom const fn [<bits_field_ $field:lower>](self) -> u32 {
                    $field_end - $field_start + 1
                }

                /* get */

                #[doc = "Gets a copy of `self` with only the bits of [`" $field "`][Self::" $field
                    "] field."]
                #[must_use]
                $vis_custom const fn [<get_field_ $field:lower>](self) -> Self {
                    Self::intersect_mask(self, Self::$field.bits)
                }

                /* get value */

                #[doc = "Gets the value of the bits of [`" $field "`][Self::" $field "] field."]
                #[must_use]
                $vis_custom const fn [<get_field_value_ $field:lower>](self) -> Self {
                    Self::get_value_range(self, $field_start, $field_end)
                }

                /* set */

                #[doc = "Returns a copy of `self` with the [`"
                    $field "`][Self::" $field "] field set."]
                #[must_use]
                $vis_custom const fn [<set_field_ $field:lower>](self) -> Self {
                    Self::set_mask(self, Self::$field.bits)
                }
                #[doc = "Sets the [`" $field "`][Self::" $field "] field."]
                $vis_custom fn [<mut_set_field_ $field:lower>](&mut self) {
                    Self::mut_set_mask(self, Self::$field.bits);
                }

                /* set value */

                #[doc = "Sets the given `value` into the bits of [`" $field "`][Self::" $field
                    "] field."]
                $vis_custom const fn [<set_field_value_ $field:lower>](self, value: $T) -> Self {
                    Self::set_value_range(self, value, $field_start, $field_end)
                }

                /* unset */

                #[doc = "Returns a copy of `self` with the [`" $field "`][Self::" $field
                    "] field set."]
                #[must_use]
                $vis_custom const fn [<unset_field_ $field:lower>](self) -> Self {
                    Self::unset_mask(self, Self::$field.bits)
                }
                #[doc = "Unsets the [`" $field "`][Self::" $field "] field."]
                $vis_custom fn [<mut_unset_field_ $field:lower>](&mut self) {
                    Self::mut_unset_mask(self, Self::$field.bits);
                }

                /* flip */

                #[doc = "Returns a copy of `self` with the [`" $field "`][Self::" $field
                    "] field flipped."]
                #[must_use]
                $vis_custom const fn [<flip_field_ $field:lower>](self) -> Self {
                    Self::flip_mask(self, Self::$field.bits)
                }
                #[doc = "Flips the [`" $field "`][Self::" $field "] field."]
                $vis_custom fn [<mut_flip_field_ $field:lower>](&mut self) {
                    Self::mut_flip_mask(self, Self::$field.bits);
                }
            }
        )*

        /// # General bits manipulation functionality
        #[allow(dead_code)]
        impl $name {
            /// the maximum valid bit index.
            $vis_extra const MAX_BIT: u32 = $T::BITS - 1;

            /* constructors & deconstructors */

            #[doc = "Returns a new `" $name "` with the given inner `bits`."]
            #[must_use]
            $vis_extra const fn with_bits(bits: $T) -> Self { Self { bits } }

            #[doc = "Returns a new `" $name "` with all bits set to 0."]
            #[must_use]
            $vis_extra const fn new_zeroed() -> Self { $name { bits: 0 } }

            #[doc = "Returns a new `" $name "` with all bits set to 1."]
            #[must_use]
            $vis_extra const fn new_oned() -> Self { Self::new_zeroed().flip() }

            #[doc = "Returns the inner `" $T "` bits."]
            #[must_use]
            $vis_extra const fn bits(self) -> $T { self.bits }

            /* queries */

            /// Returns the number of bits set (the number of ones).
            #[must_use]
            $vis_extra const fn count_ones(&self) -> u32 { self.bits.count_ones() }

            /// Returns the number of bits unset (the number of zeros).
            #[must_use]
            $vis_extra const fn count_zeros(&self) -> u32 { self.bits.count_zeros() }

            /// Returns `true` if all the bits are set to 0.
            #[must_use]
            $vis_extra const fn is_empty(self) -> bool { self.bits == 0 }

            /// Returns `true` if all the bits are set to 1.
            #[must_use]
            $vis_extra const fn is_full(self) -> bool { self.bits == Self::new_oned().bits }

            /* get */

            /// Returns `true` if the bit at `index` is set.
            ///
            /// # Panics
            /// Panics in debug if `index > MAX_BIT`.
            #[must_use]
            $vis_extra const fn is_bit_set(self, index: u32) -> bool {
                (self.bits & (1 << index)) != 0
            }

            /// Returns `true` if the bit at `index` is set, checked.
            ///
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `index > MAX_BIT`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            $vis_extra const fn is_checked_bit_set(self, index: u32)
                -> $crate::Result<bool, $crate::MismatchedBounds> {
                if let Some(shl) = [<1 $T>].checked_shl(index) {
                    Ok((self.bits & shl) != 0)
                } else {
                    Err($crate::MismatchedBounds::IndexOutOfBounds(Some(index as usize)))
                }
            }

            /// Returns a copy of `self` with only the value of the bit at `index`.
            /// # Panics
            /// Panics in debug if `index > MAX_BIT`.
            #[must_use]
            $vis_extra const fn get_bit(self, index: u32) -> Self {
                Self { bits: self.bits & (1 << index) }
            }

            /// Returns a copy of `self` with only the value of the bit at `index`, checked.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `index > MAX_BIT`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            $vis_extra const fn get_checked_bit(self, index: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                if let Some(shl) = [<1 $T>].checked_shl(index) {
                    Ok(Self { bits: self.bits & shl })
                } else {
                    Err($crate::MismatchedBounds::IndexOutOfBounds(Some(index as usize)))
                }
            }

            /// Returns a copy of `self` with only the value of the bit at `index` shifted.
            /// # Panics
            /// Panics in debug if `index > MAX_BIT`.
            #[must_use]
            $vis_extra const fn get_shifted_bit(self, index: u32) -> Self {
                Self { bits: (self.bits >> index) & 1 }
            }
            /// Returns a copy of `self` with only the value of the bit at `index` shifted, checked.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `index > MAX_BIT`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            $vis_extra const fn get_shifted_checked_bit(self, index: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                if let Some(shl) = self.bits.checked_shr(index) {
                    Ok(Self { bits: shl & 1 })
                } else {
                    Err($crate::MismatchedBounds::IndexOutOfBounds(Some(index as usize)))
                }
            }

            /* set*/

            /// Returns a copy of `self` setting the bit at `index`.
            ///
            /// # Panics
            /// Panics in debug if `index > MAX_BIT`.
            #[must_use]
            $vis_extra fn set_bit(self, index: u32) -> Self {
                Self { bits: self.bits | 1 << index }
            }
            /// Returns a copy of `self` setting the bit at `index`, checked.
            ///
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `index > MAX_BIT`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            $vis_extra const fn set_checked_bit(self, index: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                if let Some(shl) = [<1 $T>].checked_shl(index) {
                    Ok(Self { bits: self.bits | shl })
                } else {
                    Err($crate::MismatchedBounds::IndexOutOfBounds(Some(index as usize)))
                }
            }

            /// Sets the bit at `index`.
            ///
            /// # Panics
            /// Panics in debug if `index > MAX_BIT`.
            #[allow(arithmetic_overflow)]
            $vis_extra fn mut_set_bit(&mut self, index: u32) {
                self.bits |= 1 << index;
            }
            /// Sets the bit at `index`, checked.
            ///
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `index > MAX_BIT`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            $vis_extra fn mut_set_checked_bit(&mut self, index: u32)
                -> $crate::Result<(), $crate::MismatchedBounds> {
                if let Some(shl) = [<1 $T>].checked_shl(index) {
                    self.bits |= shl;
                    Ok(())
                } else {
                    Err($crate::MismatchedBounds::IndexOutOfBounds(Some(index as usize)))
                }
            }

            /* unset */

            /// Returns a copy of `self` setting the bit at `index`.
            ///
            /// # Panics
            /// Panics in debug if `index > MAX_BIT`.
            #[must_use]
            $vis_extra fn unset_bit(self, index: u32) -> Self {
                Self { bits: self.bits & !(1 << index) }
            }
            /// Returns a copy of `self` unsetting the bit at `index`, checked.
            ///
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `index > MAX_BIT`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            $vis_extra const fn unset_checked_bit(self, index: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                if let Some(shl) = [<1 $T>].checked_shl(index) {
                    Ok(Self { bits: self.bits & !shl })
                } else {
                    Err($crate::MismatchedBounds::IndexOutOfBounds(Some(index as usize)))
                }
            }

            /// Unsets the bit at `index`.
            ///
            /// # Panics
            /// Panics in debug if `index > MAX_BIT`.
            $vis_extra fn mut_unset_bit(&mut self, index: u32) {
                self.bits &= !(1 << index);
            }
            /// Unsets the bit at `index`, checked.
            ///
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `index > MAX_BIT`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            $vis_extra fn mut_unset_checked_bit(&mut self, index: u32)
                -> $crate::Result<(), $crate::MismatchedBounds> {
                if let Some(shl) = [<1 $T>].checked_shl(index) {
                    self.bits &= !shl;
                    Ok(())
                } else {
                    Err($crate::MismatchedBounds::IndexOutOfBounds(Some(index as usize)))
                }
            }

            /* flip */

            /// Returns a copy of `self` with all its bits flipped.
            #[must_use]
            $vis_extra const fn flip(self) -> Self {
                Self { bits: !self.bits }
            }
            /// Flips all the bits of `self`.
            $vis_extra fn mut_flip(&mut self) {
                self.bits = !self.bits;
            }

            /// Returns a copy of `self` flipping the bit at `index`.
            ///
            /// # Panics
            /// Panics in debug if `index > MAX_BIT`.
            #[must_use]
            $vis_extra fn flip_bit(self, index: u32) -> Self {
                Self { bits: self.bits ^ 1 << index }
            }
            /// Returns a copy of `self` flipping the bit at `index`, checked.
            ///
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `index > MAX_BIT`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            $vis_extra const fn flip_checked_bit(self, index: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                if let Some(shl) = [<1 $T>].checked_shl(index) {
                    Ok(Self { bits: self.bits ^ shl })
                } else {
                    Err($crate::MismatchedBounds::IndexOutOfBounds(Some(index as usize)))
                }
            }

            /// Flips the bit at `index`, unchecked version.
            ///
            /// # Panics
            /// Panics in debug if `index > MAX_BIT`.
            $vis_extra fn mut_flip_bit(&mut self, index: u32) {
                self.bits ^= 1 << index;
            }
            /// Flips the bit at `index`, checked.
            $vis_extra fn mut_flip_checked_bit(&mut self, index: u32)
                -> $crate::Result<(), $crate::MismatchedBounds> {
                if let Some(shl) = [<1 $T>].checked_shl(index) {
                    self.bits ^= shl;
                    Ok(())
                } else {
                    Err($crate::MismatchedBounds::IndexOutOfBounds(Some(index as usize)))
                }
            }
        }

        /// # Bit ranges
        #[allow(dead_code)]
        impl $name {
            /* new mask */

            /// Returns a new bitmask of 1s from the `[start..=end]` range.
            ///
            /// Sets the rest of the bits to 0.
            ///
            /// # Panics
            /// Panics if `start >= BITS || end >= BITS || start > end`.
            #[must_use]
            $vis_extra const fn mask_range(start: u32, end: u32) -> Self {
                Self { bits: $crate::Bitwise::<$T>::mask_range(start, end).0 }
            }
            /// Returns a new bitmask of ones from the `[start..=end]` checked range.
            ///
            /// Sets the rest of the bits to 0.
            ///
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT`,
            /// or [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra const fn mask_checked_range(start: u32, end: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                match $crate::Bitwise::<$T>::mask_checked_range(start, end) {
                    Ok(bits) => Ok(Self { bits: bits.0 } ),
                    Err(e) => Err(e),
                }
            }

            /* get */

            /// Gets a copy of `self` with only the bits from the `[start..=end]` range.
            /// # Panics
            /// Panics in debug if `start > MAX_BIT || end > MAX_BIT` or if `start > end`.
            #[must_use]
            $vis_extra const fn get_range(self, start: u32, end: u32) -> Self {
                Self { bits: $crate::Bitwise(self.bits).get_range(start, end).0 }
            }
            /// Gets a copy of `self` with only the bits from the `[start..=end]` checked range.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT`,
            /// or [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra const fn get_checked_range(self, start: u32, end: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).get_checked_range(start, end) {
                    Ok(bits) => Ok(Self { bits: bits.0 } ),
                    Err(e) => Err(e),
                }
            }

            /* get value */

            /// Gets the value of the bits in from the `[start..=end]` range.
            ///
            /// # Panics
            /// Panics if `start >= BITS || end >= BITS || start > end`.
            #[must_use]
            $vis_extra const fn get_value_range(self, start: u32, end: u32) -> Self {
                Self { bits: $crate::Bitwise(self.bits).get_value_range(start, end).0 }
            }

            /// Gets the value of the bits from the `[start..=end]` checked range.
            ///
            /// Sets the rest of the bits to 0.
            ///
            /// The bits in the specified range are shifted rightwards so that the least
            /// significant bit (LSB) aligns with the units place, forming the integer value.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start >= BITS || end >= BITS`,
            /// [`MismatchedIndices`] if `start > end`, and
            /// [`DataOverflow`] if `value` does not fit within the specified bit range.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            /// [`DataOverflow`]: crate:MismatchedBounds::DataOverflow
            $vis_extra const fn get_value_checked_range(self, start: u32, end: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).get_value_checked_range(start, end) {
                    Ok(bits) => Ok(Self { bits: bits.0 } ),
                    Err(e) => Err(e),
                }
            }

            /* set */

            /// Get a copy of `self` with bits set to 1 from the `[start..=end]` range.
            ///
            /// # Panics
            /// Panics in debug if `start > MAX_BIT || end > MAX_BIT` or if `start > end`.
            #[must_use]
            $vis_extra const fn set_range(self, start: u32, end: u32) -> Self {
                Self { bits: $crate::Bitwise(self.bits).set_range(start, end).0 }
            }
            /// Get a copy of `self` with bits set to 1 from the `[start..=end]` checked range.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT`,
            /// or [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra const fn set_checked_range(self, start: u32, end: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).set_checked_range(start, end) {
                    Ok(bits) => Ok(Self { bits: bits.0 } ),
                    Err(e) => Err(e),
                }
            }

            /// Sets the bits from the `[start..=end]` range.
            /// # Panics
            /// Panics in debug if `start > MAX_BIT || end > MAX_BIT` or if `start > end`.
            $vis_extra fn mut_set_range(&mut self, start: u32, end: u32) {
                self.bits = $crate::Bitwise(self.bits).set_range(start, end).0;
            }
            /// Sets the bits from the `[start..=end]` checked range.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT`,
            /// or [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra fn mut_set_checked_range(&mut self, start: u32, end: u32)
                -> $crate::Result<(), $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).set_checked_range(start, end) {
                    Ok(bits) => { self.bits = bits.0; Ok(()) },
                    Err(e) => Err(e),
                }
            }

            /* set value */

            /// Gets a copy of `self` with the given `value` set into the `[start..=end]` range.
            ///
            /// Leaves the rest of the bits unchanged.
            ///
            /// The value is first masked to fit the size of the range, and then
            /// it is inserted into the specified bit range of `self`, replacing
            /// the existing bits in that range. The rest of the bits in `self` remain unchanged.
            /// # Panics
            /// Panics if `start >= BITS || end >= BITS || start > end`.
            #[must_use]
            $vis_extra const fn set_value_range(self, value: $T, start: u32, end: u32) -> Self {
                Self { bits: $crate::Bitwise(self.bits).set_value_range(value, start, end).0 }
            }

            /// Gets a copy of `self` with the given `value` set into the `[start..=end]`
            /// checked range.
            ///
            /// Leaves the rest of the bits unchanged.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start >= BITS || end >= BITS`
            /// and [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra const fn set_value_checked_range(self, value: $T, start: u32, end: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).set_value_checked_range(value, start, end) {
                    Ok(bits) => Ok(Self { bits: bits.0 } ),
                    Err(e) => Err(e),
                }
            }

            /// Gets a copy of `self` with the given checked `value` set into the `[start..=end]`
            /// checked range.
            ///
            /// Leaves the rest of the bits unchanged.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start >= BITS || end >= BITS`,
            /// [`MismatchedIndices`] if `start > end`, and
            /// [`DataOverflow`] if `value` does not fit within the specified bit range.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            /// [`DataOverflow`]: crate:MismatchedBounds::DataOverflow
            $vis_extra const fn set_checked_value_checked_range(self,
                value: $T, start: u32, end: u32) -> $crate::Result<Self, $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits)
                    .set_checked_value_checked_range(value, start, end) {
                    Ok(bits) => Ok(Self { bits: bits.0 } ),
                    Err(e) => Err(e),
                }
            }

            /// Sets the given `value` into the `[start..=end]` range.
            /// Sets the bits from the `[start..=end]` range.
            /// # Panics
            /// Panics in debug if `start > MAX_BIT || end > MAX_BIT` or if `start > end`.
            $vis_extra fn mut_set_value_range(&mut self, value: $T, start: u32, end: u32) {
                self.bits = $crate::Bitwise(self.bits).set_value_range(value, start, end).0;
            }
            /// Sets the given `value` into the `[start..=end]` checked range.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT` and
            /// [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra fn mut_set_value_checked_range(&mut self,
                value: $T, start: u32, end: u32) -> $crate::Result<(), $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).set_value_checked_range(value, start, end) {
                    Ok(bits) => { self.bits = bits.0; Ok(()) },
                    Err(e) => Err(e),
                }
            }
            /// Sets the given checked `value` into the `[start..=end]` checked range.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT`,
            /// [`MismatchedIndices`] if `start > end`, and
            /// [`DataOverflow`] if `value` does not fit within the specified bit range.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            /// [`DataOverflow`]: crate:MismatchedBounds::DataOverflow
            $vis_extra fn mut_set_checked_value_checked_range(&mut self,
                value: $T, start: u32, end: u32) -> $crate::Result<(), $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits)
                    .set_checked_value_checked_range(value, start, end) {
                    Ok(bits) => { self.bits = bits.0; Ok(()) },
                    Err(e) => Err(e),
                }
            }

            /* unset */

            /// Returns a copy of `self` with unset bits to 0 from the `[start..=end]` range.
            /// # Panics
            /// Panics in debug if `start > MAX_BIT || end > MAX_BIT` or if `start > end`.
            #[must_use]
            $vis_extra const fn unset_range(self, start: u32, end: u32) -> Self {
                Self { bits: $crate::Bitwise(self.bits).unset_range(start, end).0 }
            }
            /// Returns a copy of `self` with unset bits to 0 from the `[start..=end]`
            /// checked range.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT`,
            /// or [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra const fn unset_checked_range(self, start: u32, end: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).unset_checked_range(start, end) {
                    Ok(bits) => Ok(Self { bits: bits.0 } ),
                    Err(e) => Err(e),
                }
            }

            /// Unsets the bits from the `[start..=end]` range.
            /// # Panics
            /// Panics in debug if `start > MAX_BIT || end > MAX_BIT` or if `start > end`.
            $vis_extra fn mut_unset_range(&mut self, start: u32, end: u32) {
                self.bits = $crate::Bitwise(self.bits).unset_range(start, end).0;
            }
            /// Unsets the bits from the `[start..=end]` checked range.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT`,
            /// or [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra fn mut_unset_checked_range(&mut self, start: u32, end: u32)
                -> $crate::Result<(), $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).unset_checked_range(start, end) {
                    Ok(bits) => { self.bits = bits.0; Ok(()) },
                    Err(e) => Err(e),
                }
            }

            /* flip */

            /// Returns a copy of `self` with flipped bits from the `[start..=end]` range.
            /// # Panics
            /// Panics in debug if `start > MAX_BIT || end > MAX_BIT` or if `start > end`.
            #[must_use]
            $vis_extra const fn flip_range(self, start: u32, end: u32) -> Self {
                Self { bits: $crate::Bitwise(self.bits).flip_range(start, end).0 }
            }
            /// Returns a copy of `self` with flipped bits from the `[start..=end]` checked range.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT`,
            /// or [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra const fn flip_checked_range(self, start: u32, end: u32)
                -> $crate::Result<Self, $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).flip_checked_range(start, end) {
                    Ok(bits) => Ok(Self { bits: bits.0 } ),
                    Err(e) => Err(e),
                }
            }

            /// Flips the bits from the `[start..=end]` range.
            /// # Panics
            /// Panics in debug if `start > MAX_BIT || end > MAX_BIT` or if `start > end`.
            $vis_extra fn mut_flip_range(&mut self, start: u32, end: u32) {
                self.bits = $crate::Bitwise(self.bits).flip_range(start, end).0;
            }
            /// Flips the bits from the `[start..=end]` checked range.
            /// # Errors
            /// Returns [`IndexOutOfBounds`] if `start > MAX_BIT || end > MAX_BIT`,
            /// or [`MismatchedIndices`] if `start > end`.
            ///
            /// [`IndexOutOfBounds`]: crate::MismatchedBounds::IndexOutOfBounds
            /// [`MismatchedIndices`]: crate:MismatchedBounds::MismatchedIndices
            $vis_extra fn mut_flip_checked_range(&mut self, start: u32, end: u32)
                -> $crate::Result<(), $crate::MismatchedBounds> {
                match $crate::Bitwise(self.bits).flip_checked_range(start, end) {
                    Ok(bits) => { self.bits = bits.0; Ok(()) },
                    Err(e) => Err(e),
                }
            }
        }

        /// # Bit masks
        #[allow(dead_code)]
        impl $name {
            /* contains */

            /// Returns `true` if `self` contains all the same set bits that are set in `mask`.
            #[must_use]
            $vis_extra const fn contains_mask(self, mask: $T) -> bool {
                (self.bits & mask) == mask
            }
            /// Returns `true` if `self` contains all the same set bits that are set in `other`.
            #[must_use]
            $vis_extra const fn contains_other(self, other: Self) -> bool {
                (self.bits & other.bits) == other.bits
            }

            /* overlaps */

            /// Returns `true` if there's at least one set bit in common between `self` and `mask`.
            #[must_use]
            $vis_extra const fn overlaps_mask(&self, mask: $T) -> bool {
                (self.bits & mask) != 0
            }
            /// Returns `true` if there's at least one set bit in common between `self` and `other`.
            #[must_use]
            $vis_extra const fn overlaps_other(&self, other: Self) -> bool {
                (self.bits & other.bits) != 0
            }

            /* intersect */

            /// Returns a copy of `self` with only the bits both in `self` and the `mask`.
            #[must_use]
            $vis_extra const fn intersect_mask(self, mask: $T) -> Self {
                Self { bits: self.bits & mask }
            }
            /// Returns a copy of `self` with only the bits both in `self` and `other`.
            #[must_use]
            $vis_extra const fn intersect_other(self, other: Self) -> Self {
                Self { bits: self.bits & other.bits }
            }
            /// Only leaves the bits both in `self` and the `mask`.
            $vis_extra fn mut_intersect_mask(&mut self, mask: $T) {
                self.bits &= mask;
            }
            /// Only leaves the bits both in `self` and `other`.
            $vis_extra fn mut_intersect_other(&mut self, other: Self) {
                self.bits &= other.bits;
            }

            /* set */

            /// Returns a copy of `self` setting the bits that are set in the `mask`.
            #[must_use]
            $vis_extra const fn set_mask(self, mask: $T) -> Self {
                Self { bits: self.bits | mask }
            }
            /// Returns a copy of `self` setting the bits that are set in `other`.
            #[must_use]
            $vis_extra const fn set_other(self, other: Self) -> Self {
                Self { bits: self.bits | other.bits }
            }
            /// Sets the bits that are set in the `mask`.
            $vis_extra fn mut_set_mask(&mut self, mask: $T) {
                self.bits |= mask;
            }
            /// Sets the bits that are set in the `other`.
            $vis_extra fn mut_set_other(&mut self, other: Self) {
                self.bits |= other.bits;
            }

            /* unset */

            /// Returns a copy of `self` unsetting the bits that are set in the `mask`.
            #[must_use]
            $vis_extra const fn unset_mask(self, mask: $T) -> Self {
                Self { bits: self.bits & !mask }
            }
            /// Returns a copy of `self` unsetting the bits that are set in `other`.
            #[must_use]
            $vis_extra const fn unset_other(self, other: Self) -> Self {
                Self { bits: self.bits & !other.bits }
            }
            /// Unsets the bits that are set in the `mask`.
            $vis_extra fn mut_unset_mask(&mut self, mask: $T) {
                self.bits &= !mask;
            }
            /// Unsets the bits that are set in `other`.
            $vis_extra fn mut_unset_other(&mut self, other: Self) {
                self.bits &= !other.bits;
            }

            /* flip */

            /// Returns a copy of `self` flipping the bits that are set in the `mask`.
            #[must_use]
            $vis_extra const fn flip_mask(self, mask: $T) -> Self {
                Self { bits: self.bits ^ mask }
            }
            /// Returns a copy of `self` flipping the bits that are set in `other`.
            #[must_use]
            $vis_extra const fn flip_other(self, other: Self) -> Self {
                Self { bits: self.bits ^ other.bits }
            }
            /// Flips the bits that are set in the `mask`.
            $vis_extra fn mut_flip_mask(&mut self, mask: $T) {
                self.bits ^= mask;
            }
            /// Flips the bits that are set in `other`.
            $vis_extra fn mut_flip_other(&mut self, other: Self) {
                self.bits ^= other.bits;
            }
        }
    }};
    {
        /* optional syntax */

        (custom:$vis_custom:vis, extra:$vis_extra:vis $(,)?) // optional trailing comma
        $(#[$struct_attributes:meta])*
        $vis:vis struct $name:ident($T:ty) {
            $(
                $(#[$field_attrs:meta])*
                $field:ident: $field_start:expr $(,$field_end:expr)?; // NAME: bit;
            )*
        }
    } => {
        $crate::bitfield! {
            (custom: $vis_custom, extra: $vis_extra)
            $( #[$struct_attributes] )*
            $vis struct $name($T) {
                $(
                    $( #[$field_attrs] )*
                    $field: $field_start, $crate::coalesce![$($field_end)?, $field_start];
                )*
            }
        }
    };
    { (custom) // only public custom fields
        $($tt:tt)+ } => { $crate::bitfield![ (custom:pub, extra:pub(crate)) $($tt)+ ]; };
    { (extra) // only public extra methods
        $($tt:tt)+ } => { $crate::bitfield![ (custom:pub(crate), extra:pub) $($tt)+ ]; };
    { // everything public
        $($tt:tt)+ } => { $crate::bitfield![ (custom:pub, extra:pub) $($tt)+ ]; };
}
#[doc(inline)]
#[cfg_attr(feature = "nightly_doc", doc(cfg(_bit··)))]
pub use _bitfield as bitfield; // see crate::code::utils::mod.rs