tartan-bitfield 1.2.0

Define structures with accessors for particular bits or bit ranges
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
//! This crate can be used to define structures with accessors for particular bits or
//! bit ranges. Useful for dealing with registers and protocols.
//!
//! # Features
//!
//!   * **Performance**: Generated code is nearly identical to hand-rolled bit twiddling.
//!   * **Safety**: Absolutely no unsafe code in the implementation or usage.
//!   * **Portability**:
//!     * `#![no_std]`-compatible out of the box.
//!     * Unlike bit fields in C, the layout is predictable. See the section about
//!       endianness below.
//!     * Unlike Rust's `#[repr(packed, C)]`, each field has explicit bit ranges, rather
//!       than relying on the ordering and size of other fields within the struct. While
//!       specifying bit numbers may seem tedious, it can eliminate surprises, and it
//!       usually corresponds directly to the way registers and protocols are defined in
//!       datasheets.
//!   * **Convenience**:
//!     * Single-bit flags and multi-bit fields can be defined in the same structure.
//!     * Bit ranges can be accessed as non-primitive, non-integer types (including other
//!       bitfield structs) using appropriate [`Into`] and [`From`] implementations.
//!     * The structs implement all the traits you would expect. See the documentation
//!       for [`bitfield`]. A [`bitfield_without_debug`] macro is also available if you
//!       want to provide your own debugging output.
//!     * Accessors can be defined in a trait, which is useful for registers where where
//!       some fields are common, but others are only defined in certain states. See
//!       [`bitfield_accessors`].
//!
//! # Example
//!
//! ```
//! # use tartan_bitfield::bitfield;
//! bitfield! {
//!     // The structure will be a wrapper for a u32 value.
//!     pub struct Example(u32) {
//!         // Accessors for field `a` will refer to the first four least significant
//!         // bits of the wrapped value, bits 0, 1, 2, and 3.
//!         //
//!         // Note that like normal Rust ranges:
//!         //   * `[0..4]` does not include bit 4
//!         //   * `[0..=4]` includes bit 4
//!         //
//!         // The accessors will be public, and will take/return the four bits as a `u8`.
//!         [0..4] pub a: u8,
//!
//!         // No accessors cover bits `4..6`. This is legal and can be used for reserved
//!         // bits. However, these bits will still affect equality for the struct as a
//!         // whole.
//!
//!         // Accessors for field `b` will refer to the twelve bits starting at bit 6,
//!         // but they will not be public. They will take/return the 12 bits as a `u16`.
//!         [6..=17] b: u16,
//!
//!         // Note that this bit range overlaps with `b`. This is allowed.
//!         [16..20] pub c: u8,
//!
//!         // Accessors for field `d` will take/return a boolean and refer to a single
//!         // bit. Note that the `bool` is implied and not specified after the name.
//!         [25] pub d,
//!
//!         // This will cover the 6 most significant bits of the wrapped value, but
//!         // the getters will take/return a `SubFields` struct instead of `u8`. This is
//!         // useful for nested bitfields, but the `A as B` syntax works for any `B`
//!         // which implements `Into<A>` and `From<A>`.
//!         [26..32] pub e: u8 as SubFields,
//!     }
//! }
//!
//! bitfield! {
//!     // All accessors on this field use booleans and refer to single bits
//!     pub struct SubFields(u8) {
//!         [0] pub zero,
//!         [1] pub one,
//!         [2] pub two,
//!         [3] pub three,
//!         [4] pub four,
//!         [5] pub five,
//!     }
//! }
//!
//!
//! // The struct can be initialized with a u32 value
//! let x = Example(0xfa84_9e1b);
//! assert_eq!(x.a(), 0xb_u8);
//! assert_eq!(x.b(), 0x278_u16);  // Private, but still can be used within the module
//! assert_eq!(x.c(), 0x4_u8);
//! assert_eq!(x.d(), true);
//! assert_eq!(x.e(), SubFields(0x3e_u8));
//! assert_eq!(x.e().zero(), false);
//! assert_eq!(x.e().five(), true);
//!
//! // It can also be converted Into and From its underlying representation
//! let n: u32 = x.into();
//! let y: Example = n.into();
//! assert_eq!(n, 0xfa84_9e1b);
//! assert_eq!(x, y);
//!
//! // Setters are all prefixed with `set_`. They have the same visibility as the getters.
//! let mut z = Example::default();
//! z.set_a(0xb);
//! z.set_b(0x278);
//! z.set_c(0x4);
//! z.set_d(true);
//! z.set_e(SubFields(0x3e));
//! assert_eq!(z, Example(0xfa04_9e0b));
//!
//! // Reserved ranges influence equality, and they are all zero on `z`.
//! assert_ne!(z, x);
//!
//! // Alternatively, you can use the `with_` methods, which return a new value instead
//! // of mutating in place.
//! let mut w = x
//!     .with_a(0x6)
//!     .with_b(0x9f3)
//!     .with_c(0xd)
//!     .with_d(false)
//!     .with_e(SubFields(0x2b));
//! assert_eq!(w, Example(0xac8d_7cd6));
//! assert_eq!(x, Example(0xfa84_9e1b));
//! ```
//!
//! For lots more examples, see the [Tartan OS](https://github.com/cimbul/tartan-os)
//! project that this crate was spun off from.
//!
//! # Endiannness and Bit Numbering
//!
//! Each bitfield wraps an underlying integer type. In the example above, `Example(u32)`
//! wraps a `u32`. Bit numbers within the macro refer to the bits of the logical _value_,
//! starting from the least significant bit = 0. They are not dependent on the order of
//! the bytes of the `u32` representation in memory, a.k.a. endianness.
//!
//! The endianness of the underlying value is platform dependent. This is no different
//! than any other integer value, and the context determines whether you need to worry
//! about it.
//!   * If the underlying representation is a `u8`, then byte order is irrelevant.
//!   * If you are reading from a register, it's likely you want native byte order and
//!     don't need to do anything special.
//!   * If you are working with a network or bus protocol, it's likely you are serializing
//!     or deserializing from a byte array. To convert using a specific endianness
//!     regardless of platform, use the normal methods: for example, the builtins
//!     [`u32::from_be_bytes`] and [`u64::to_le_bytes`], or a crate like
//!     [byteorder](https://docs.rs/byteorder/latest/byteorder/).
//!
//! # Alternatives
//!
//! I have been using this in my personal OS project for a while, and it meets my needs
//! better than other options. But you may be interested in a few other crates:
//!   * [bitfield](https://github.com/dzamlo/rust-bitfield): Similar approach for
//!     accessors and bit ranges, but a less obvious syntax.
//!   * [bitflags](https://docs.rs/bitflags/latest/bitflags/): Works well for single-bit
//!     flags that can be viewed as a collection.
//!   * [bitvec](https://docs.rs/bitvec/latest/bitvec/): Another collection type to view
//!     memory as a sequence of bits. Less focused on defining domain-specific structs.
//!   * [modular-bitfield](https://docs.rs/modular-bitfield/latest/modular_bitfield/):
//!     Field ordering and widths determine bit ranges. Structs can only be converted to
//!     byte arrays, and only in little-endian order, regardless of platform endianness.
//!     This can be undesirable when working with registers.
//!   * [packed_struct](https://docs.rs/packed_struct/0.10.0/packed_struct/): Lots of
//!     options, including bit numbering and endianness conversions. Structs are held
//!     in unpacked form in memory, and only converted to packed form for serialization.
//!     Depending on your access patterns, this may be better or worse (or it may not
//!     matter at all).
//!       * For an analogue to `pack_struct`'s `PrimitiveEnum`, see the
//!         [`tartan-c-enum`](https://github.com/cimbul/tartan-c-enum) crate.

#![no_std]
#![warn(missing_docs)]
#![warn(clippy::pedantic)]
#![allow(clippy::doc_markdown)]
#![allow(clippy::inline_always)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::similar_names)]
#![allow(clippy::upper_case_acronyms)]

use core::convert::From;
use core::default::Default;
use core::ops;

// Must be re-exported so that crates that use these macros will be able to resolve it
#[doc(hidden)]
pub use paste::paste;

/// Marker trait implemented by types defined with the [`bitfield`] macro.
///
/// This mainly exists to allow type inference in the [`bitfield_accessors`] macro, but it
/// also aids documentation and may be useful in user code.
pub trait Bitfield<T>
where
    Self: core::fmt::Debug + Default + Copy + Eq + From<T> + Into<T>,
{
    /// Construct a new bitfield type from its underlying representation
    #[inline(always)]
    fn new(value: T) -> Self {
        value.into()
    }

    /// Unwrap the bitfield into its underlying representation
    #[inline(always)]
    fn value(self) -> T {
        self.into()
    }
}

/// Define a structure that wraps a number with accessors for certain bit ranges.
///
/// See the crate documentation for an example.
///
/// The structure will implement these traits, where `T` is the underlying type defined
/// in parentheses immediately after the struct name.
///   * [`Bitfield<T>`](Bitfield)
///   * [`Debug`]
///   * [`Default`]
///   * [`Copy`]
///   * [`Eq`]
///   * [`Into<T>`](Into)
///   * [`From<T>`](From)
#[macro_export]
macro_rules! bitfield {
    [
        $( #[$meta:meta] )*
        $vis:vis struct $struct:ident($underlying_type:ty) {
            $($body:tt)*
        }
    ] => {
        $crate::bitfield_without_debug! {
            $(#[$meta])*
            $vis struct $struct($underlying_type) {
                $($body)*
            }
        }

        impl ::core::fmt::Debug for $struct {
            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
                let mut struct_out = f.debug_struct(stringify!($struct));
                struct_out.field("<value>", &self.0);
                self.fmt_fields(&mut struct_out);
                struct_out.finish()
            }
        }
    }
}

/// Same as the [`bitfield`] macro without a [`Debug`] implementation provided.
///
/// Since Debug is required by the [`Bitfield`] trait, the caller must provide their own
/// implementation.
#[macro_export]
macro_rules! bitfield_without_debug {
    [
        $( #[$meta:meta] )*
        $vis:vis struct $struct:ident($underlying_type:ty) {
            $($body:tt)*
        }
    ] => {
        $( #[$meta] )*
        #[repr(transparent)]
        #[derive(Default, Clone, Copy, PartialEq, Eq)]
        $vis struct $struct($underlying_type);

        impl $struct {
            $crate::bitfield_accessors! { $($body)* }
        }

        impl $crate::Bitfield<$underlying_type> for $struct {}

        impl ::core::convert::From<$underlying_type> for $struct {
            #[inline(always)]
            fn from(val: $underlying_type) -> Self { Self(val) }
        }

        impl ::core::convert::From<$struct> for $underlying_type {
            #[inline(always)]
            fn from(val: $struct) -> Self { val.0 }
        }
    };
}

/// Define getters and setters for certain bit ranges. The containing type must
/// implement the [`Bitfield`] trait.
///
/// This is most commonly invoked by the [`bitfield`] macro, but it can be used on its own
/// to define common accessors as part of a trait, as in the example below:
///
/// ```
/// # use tartan_bitfield::{Bitfield, bitfield, bitfield_accessors};
/// #
/// trait CommonFields: Bitfield<u32> {
///     bitfield_accessors! {
///         // Since these are part of a trait, the `pub` keyword must be omitted.
///         [ 0.. 6] a: u8,
///         [14]     b,
///         [18..32] c: u16,
///     }
/// }
///
/// bitfield! {
///     struct SomeFields(u32) {
///         [ 7] pub x,
///         [16] pub y,
///     }
/// }
///
/// bitfield! {
///     struct OtherFields(u32) {
///         [10] pub z,
///         [12] pub q,
///     }
/// }
///
/// impl CommonFields for SomeFields {}
/// impl CommonFields for OtherFields {}
///
/// let f = SomeFields(0xabcd_1234);
/// assert_eq!(f.a(), 0x34); // has accessors from CommonFields
/// assert_eq!(f.y(), true); // has accessors from SomeFields
/// //assert_eq!(f.z(), false); // COMPILE ERROR: no accessors from OtherFields
///
/// let g = OtherFields(0xabcd_1234);
/// assert_eq!(g.a(), 0x34); // has accessors from CommonFields
/// //assert_eq!(g.y(), true); // COMPILE ERROR: no accessors from SomeFields
/// assert_eq!(g.z(), false); // has accessors from OtherFields
/// ```
#[macro_export]
macro_rules! bitfield_accessors {
    [
        $(
            $( #[$meta:meta] )*
            [ $( $range:tt )* ]
            $vis:vis $field:ident
            $( : $underlying_type:ty $( as $interface_type:ty )? )?
        ),*
        $(,)?
    ] => {
        $(
            $crate::bitfield_accessors! {
                @field getter
                $( #[$meta] )*
                [ $( $range )* ]
                $vis $field
                $( : $underlying_type $( as $interface_type )? )?
            }
        )*

        $(
            $crate::bitfield_accessors! {
                @field setter
                $( #[$meta] )*
                [ $( $range )* ]
                $vis $field
                $( : $underlying_type $( as $interface_type )? )?
            }
        )*

        /// Print this object's bitfield values. Helper method for `Debug`
        /// implementations.
        fn fmt_fields(&self, f: &mut ::core::fmt::DebugStruct) {
            $(
                $(#[$meta])*
                f.field(stringify!($field), &self.$field());
            )*
        }
    };

    // Special case for single-bit boolean fields
    [
        @field getter
        $( #[$meta:meta] )*
        [ $bit:literal ]
        $vis:vis $field:ident
    ] => {
        $crate::paste! {
            $( #[$meta] )*
            $vis fn $field(&self) -> bool {
                $crate::get_bit(<Self as $crate::Bitfield<_>>::value(*self), $bit)
            }
        }
    };

    // Special case for single-bit boolean fields
    [
        @field setter
        $( #[$meta:meta] )*
        [ $bit:literal ]
        $vis:vis $field:ident
    ] => {
        $crate::paste! {
            $( #[$meta] )*
            #[inline(always)]
            $vis fn [< set_ $field >](&mut self, value: bool) {
                *self = self.[< with_ $field >](value);
            }

            $( #[$meta] )*
            $vis fn [< with_ $field >](&mut self, value: bool) -> Self {
                let packed = <Self as $crate::Bitfield<_>>::value(*self);
                <Self as $crate::Bitfield<_>>::new(
                    $crate::set_bit(packed, $bit, value))
            }
        }
    };

    // A field type and both range bounds are required in all other cases.
    // When no explicit interface type is given, use the underlying type.
    [
        @field $accessor_type:tt
        $( #[$meta:meta] )*
        [ $lsb:literal .. $msb:literal ]
        $vis:vis $field:ident
        : $field_type:ty
    ] => {
        $crate::bitfield_accessors! {
            @field $accessor_type
            $( #[$meta] )*
            [$lsb..$msb] $vis $field: $field_type as $field_type
        }
    };

    [
        @field $accessor_type:tt
        $( #[$meta:meta] )*
        [ $lsb:literal ..= $msb:literal ]
        $vis:vis $field:ident
        : $field_type:ty
    ] => {
        $crate::bitfield_accessors! {
            @field $accessor_type
            $( #[$meta] )*
            [$lsb..=$msb] $vis $field: $field_type as $field_type
        }
    };

    [
        @field getter
        $( #[$meta:meta] )*
        [ $lsb:literal .. $msb:literal ]
        $vis:vis $field:ident
        : $underlying_type:ty as $interface_type:ty
    ] => {
        $crate::paste! {
            $( #[$meta] )*
            $vis fn $field(&self) -> $interface_type {
                use $crate::TruncateInto;
                let packed = <Self as $crate::Bitfield<_>>::value(*self);
                let underlying: $underlying_type =
                    $crate::get_bits(packed, $lsb, $msb).truncate_into();
                underlying.into()
            }
        }
    };

    [
        @field getter
        $( #[$meta:meta] )*
        [ $lsb:literal ..= $msb:literal ]
        $vis:vis $field:ident
        : $underlying_type:ty as $interface_type:ty
    ] => {
        $crate::paste! {
            $( #[$meta] )*
            $vis fn $field(&self) -> $interface_type {
                use $crate::TruncateInto;
                let packed = <Self as $crate::Bitfield<_>>::value(*self);
                let underlying: $underlying_type =
                    $crate::get_bits(packed, $lsb, $msb + 1).truncate_into();
                underlying.into()
            }
        }
    };

    [
        @field setter
        $( #[$meta:meta] )*
        [ $lsb:literal .. $msb:literal ]
        $vis:vis $field:ident
        : $underlying_type:ty as $interface_type:ty
    ] => {
        $crate::paste! {
            $( #[$meta] )*
            #[inline(always)]
            $vis fn [< set_ $field >](&mut self, value: $interface_type) {
                *self = self.[< with_ $field >](value);
            }

            $( #[$meta] )*
            $vis fn [< with_ $field >](&self, value: $interface_type) -> Self {
                let underlying: $underlying_type = value.into();
                let packed = <Self as $crate::Bitfield<_>>::value(*self);
                <Self as $crate::Bitfield<_>>::new(
                    $crate::set_bits(packed, $lsb, $msb, underlying.into()))
            }
        }
    };

    [
        @field setter
        $( #[$meta:meta] )*
        [ $lsb:literal ..= $msb:literal ]
        $vis:vis $field:ident
        : $underlying_type:ty as $interface_type:ty
    ] => {
        $crate::paste! {
            $( #[$meta] )*
            #[inline(always)]
            $vis fn [< set_ $field >](&mut self, value: $interface_type) {
                *self = self.[< with_ $field >](value);
            }

            $( #[$meta] )*
            $vis fn [< with_ $field >](&self, value: $interface_type) -> Self {
                let underlying: $underlying_type = value.into();
                let packed = <Self as $crate::Bitfield<_>>::value(*self);
                <Self as $crate::Bitfield<_>>::new(
                    $crate::set_bits(packed, $lsb, $msb + 1, underlying.into()))
            }
        }
    };
}

/// Get a boolean reflecting a single bit of the value.
///
/// `bit_num` starts as zero for the least significant bit.
///
/// ```
/// # use tartan_bitfield::get_bit;
/// assert_eq!(get_bit(0b0000_0100_u8, 2), true);
/// assert_eq!(get_bit(0b0000_0100_u8, 3), false);
/// ```
#[must_use]
pub fn get_bit<T>(val: T, bit_num: u8) -> bool
where
    T: Default
        + PartialEq
        + From<bool>
        + ops::BitAnd<T, Output = T>
        + ops::Shl<u8, Output = T>,
{
    let position_mask = T::from(true) << bit_num;
    (val & position_mask) != T::default()
}

/// Create a copy of the value with a single bit modified.
///
/// `bit_num` starts as zero for the least significant bit.
///
/// ```
/// # use tartan_bitfield::set_bit;
/// assert_eq!(set_bit(0b0000_0000_u8, 5, true), 0b0010_0000);
/// assert_eq!(set_bit(0b1111_1111_u8, 0, false), 0b1111_1110);
/// ```
#[must_use]
pub fn set_bit<T>(val: T, bit_num: u8, bit_val: bool) -> T
where
    T: From<bool>
        + ops::BitAnd<Output = T>
        + ops::BitOr<Output = T>
        + ops::Shl<u8, Output = T>
        + ops::Not<Output = T>,
{
    let value_mask = T::from(bit_val) << bit_num;
    let position_mask = T::from(true) << bit_num;
    val & position_mask.not() | value_mask
}

/// Extract a range of bits from the value, shifted so the first bit of the subset is the
/// least significant bit of the result.
///
/// Bits are numbered starting with zero for the least significant bit. The range of bits
/// in the result is `lsb..msb`, **exclusive** of `msb`.
///
/// ```
/// # use tartan_bitfield::get_bits;
/// assert_eq!(get_bits(0b1100_1110_u8, 3, 7), 0b1001);
/// assert_eq!(get_bits(0b1010_0101_u8, 6, 8), 0b10);
/// ```
#[must_use]
pub fn get_bits<T>(packed_val: T, lsb: u8, msb: u8) -> T
where
    T: Default
        + OverflowingShl
        + OverflowingShr
        + ops::Not<Output = T>
        + ops::BitAnd<T, Output = T>,
{
    let field_width = msb - lsb;
    // e.g., 0b0000_0111 for U with a width 3 bits from its MSB to LSB
    let field_width_mask = T::default().not().saturating_shl(field_width.into()).not();
    packed_val.saturating_shr(lsb.into()) & field_width_mask
}

/// Create a copy of the value with a subset of bits updated based on the passed value.
///
/// Bits are numbered starting with zero for the least significant bit. The range of
/// updated bits is `lsb..msb`, **exclusive** of `msb`. `field_val` is shifted left `lsb`
/// bits before being combined with `packed_val`.
///
/// ```
/// # use tartan_bitfield::set_bits;
/// assert_eq!(set_bits(0b0000_0000_u8, 6, 8, 0b11), 0b1100_0000);
/// assert_eq!(set_bits(0b1111_1111_u8, 1, 5, 0b0000), 0b1110_0001);
/// assert_eq!(set_bits(0b1010_0110_u8, 2, 6, 0b1110), 0b1011_1010);
/// ```
#[must_use]
pub fn set_bits<T>(packed_val: T, lsb: u8, msb: u8, field_val: T) -> T
where
    T: Default
        + Copy
        + OverflowingShl
        + ops::Shl<u8, Output = T>
        + ops::Not<Output = T>
        + ops::BitAnd<T, Output = T>
        + ops::BitOr<T, Output = T>,
{
    // e.g., 0b1110_0000 for MSB = 5 (exclusive)
    let msb_mask = T::default().not().saturating_shl(msb.into());
    // e.g., 0b0000_0011 for LSB = 2
    let lsb_mask = T::default().not().saturating_shl(lsb.into()).not();
    // e.g., 0b1110_0011 for MSB = 5, LSB = 2
    let position_mask = msb_mask | lsb_mask;
    let value_mask = field_val.saturating_shl(lsb.into()) & position_mask.not();
    packed_val & position_mask | value_mask
}

/// A type whose values can be truncated into another type. This is more explicit than
/// `x as T`.
pub trait TruncateInto<T> {
    /// Truncate the value to fit in the destination type
    fn truncate_into(self) -> T;
}

macro_rules! truncate_into_impl {
    ($source:ty, $dest:ty) => {
        impl TruncateInto<$dest> for $source {
            #[inline(always)]
            fn truncate_into(self) -> $dest {
                self as $dest
            }
        }
    };
}

truncate_into_impl!(u128, u128);
truncate_into_impl!(u128, u64);
truncate_into_impl!(u128, u32);
truncate_into_impl!(u128, u16);
truncate_into_impl!(u128, u8);

truncate_into_impl!(u64, u64);
truncate_into_impl!(u64, u32);
truncate_into_impl!(u64, u16);
truncate_into_impl!(u64, u8);

truncate_into_impl!(u32, u32);
truncate_into_impl!(u32, u16);
truncate_into_impl!(u32, u8);

truncate_into_impl!(u16, u16);
truncate_into_impl!(u16, u8);

truncate_into_impl!(u8, u8);

truncate_into_impl!(usize, usize);
#[cfg(target_pointer_width = "64")]
truncate_into_impl!(usize, u64);
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
truncate_into_impl!(usize, u32);
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
truncate_into_impl!(usize, u16);
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
truncate_into_impl!(usize, u8);

/// A type with an overflowing left shift operation. Also adds a saturating version.
///
/// All basic numeric types have this operation, but there is no corresponding trait in
/// [`core::ops`].
pub trait OverflowingShl
where
    Self: Sized + Default,
{
    /// Shift the value left by `n mod m` bits, where `m` is the number of bits in the
    /// type. Return the shifted value along with a boolean indicating whether the shift
    /// count was wrapped.
    ///
    /// Since this behavior is counterintuitive and practically useless, see
    /// [`saturating_shl`](Self::saturating_shl) for an alternative that behaves the way
    /// you probably expect.
    fn overflowing_shl(self, n: u32) -> (Self, bool);

    /// Shift the value left by `n` bits. If `n` is greater than or equal to the number
    /// of bits in this type, the result will be zero.
    #[inline(always)]
    #[must_use]
    fn saturating_shl(self, n: u32) -> Self {
        match self.overflowing_shl(n) {
            (_, true) => Self::default(),
            (x, _) => x,
        }
    }
}

macro_rules! overflowing_shl_impl {
    ($type:ty) => {
        impl OverflowingShl for $type {
            #[inline(always)]
            fn overflowing_shl(self, n: u32) -> (Self, bool) {
                self.overflowing_shl(n)
            }
        }
    };
}

overflowing_shl_impl!(u8);
overflowing_shl_impl!(u16);
overflowing_shl_impl!(u32);
overflowing_shl_impl!(u64);
overflowing_shl_impl!(u128);
overflowing_shl_impl!(usize);

/// A type with an overflowing right shift operation. Also adds a saturating version.
///
/// All basic numeric types have this operation, but there is no corresponding trait in
/// [`core::ops`].
pub trait OverflowingShr
where
    Self: Sized + Default,
{
    /// Shift the value right by `n mod m` bits, where `m` is the number of bits in the
    /// type. Return the shifted value along with a boolean indicating whether the shift
    /// count was wrapped.
    ///
    /// Since this behavior is counterintuitive and practically useless, see
    /// [`saturating_shr`](Self::saturating_shr) for an alternative that behaves the way
    /// you probably expect.
    fn overflowing_shr(self, n: u32) -> (Self, bool);

    /// Shift the value right by `n` bits. If `n` is greater than or equal to the number
    /// of bits in this type, the result will be zero.
    #[inline(always)]
    #[must_use]
    fn saturating_shr(self, n: u32) -> Self {
        match self.overflowing_shr(n) {
            (_, true) => Self::default(),
            (x, _) => x,
        }
    }
}

macro_rules! overflowing_shr_impl {
    ($type:ty) => {
        impl OverflowingShr for $type {
            #[inline(always)]
            fn overflowing_shr(self, n: u32) -> (Self, bool) {
                self.overflowing_shr(n)
            }
        }
    };
}

overflowing_shr_impl!(u8);
overflowing_shr_impl!(u16);
overflowing_shr_impl!(u32);
overflowing_shr_impl!(u64);
overflowing_shr_impl!(u128);
overflowing_shr_impl!(usize);