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
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

/// A trait to get or set a single bit.
///
/// This trait is implemented for all type that implement `BitRange<T>`.
pub trait Bit {
    /// Get a single bit.
    fn bit(&self, bit: usize) -> bool;

    /// Set a single bit.
    fn set_bit(&mut self, bit: usize, value: bool);
}

/// A trait to get or set ranges of bits.
pub trait BitRange<T> {
    /// Get a range of bits.
    fn bit_range(&self, msb: usize, lsb: usize) -> T;
    /// Set a range of bits.
    fn set_bit_range(&mut self, msb: usize, lsb: usize, value: T);
}

/// A struct to support bits operations.
pub struct Bits<T>(pub T);

#[macro_export(local_inner_macros)]
macro_rules! impl_bits {
    () => {};
    (@inner $T:tt => bool) => {
        impl From<bool> for Bits<$T> {
            fn from(value: bool) -> Self {
                match value {
                    true => Self(1 as $T),
                    false => Self(0 as $T),
                }
            }
        }
        impl Into<bool> for Bits<$T> {
            fn into(self) -> bool {
                match self.0 {
                    0 => false,
                    _ => true,
                }
            }
        }
        impl Bit for Bits<$T> {
            fn bit(&self, bit: usize) -> bool {
                (self.0 & (1 << bit)) != 0
            }

            fn set_bit(&mut self, bit: usize, value: bool) {
                match value {
                    true => self.0 |= 1 << bit,
                    false => self.0 &= !(1 << bit),
                }
            }
        }
    };
    (@inner $T:tt => $U:tt) => {
        impl From<$U> for Bits<$T> {
            fn from(value: $U) -> Self {
                Self(value as $T)
            }
        }
        impl Into<$U> for Bits<$T> {
            fn into(self) -> $U {
                self.0 as $U
            }
        }
        impl BitRange<$U> for Bits<$T> {
            #[inline]
            #[allow(unknown_lints)]
            #[allow(cast_lossless)]
            fn bit_range(&self, msb: usize, lsb: usize) -> $U {
                let mask = ((1 << (msb - lsb + 1)) - 1) << lsb;
                ((self.0 & mask) >> lsb) as $U
            }

            #[inline]
            #[allow(unknown_lints)]
            #[allow(cast_lossless)]
            fn set_bit_range(&mut self, msb: usize, lsb: usize, value: $U) {
                let mask = ((1 << (msb - lsb + 1)) - 1) << lsb;
                self.0 = (self.0 & !mask) | ((value as $T) << lsb);
            }
        }
    };
    ($T:tt => [$($U:tt),*]; $($rest:tt)*) => {
        $(
            impl_bits!{ @inner $T => $U }
        )*
        impl_bits!{ $($rest)* }
    }
}

#[macro_export(local_inner_macros)]
macro_rules! impl_bitrange {
    () => {};
    (@inner $T:ty => $U:ty) => {
        impl From<$U> for Bits<$T> {
            fn from(value: $U) -> Self {
                Self(value as $T)
            }
        }
        impl Into<$U> for Bits<$T> {
            fn into(self) -> $U {
                self.0 as $U
            }
        }
        impl BitRange<$U> for Bits<$T> {
            #[inline]
            #[allow(unknown_lints)]
            #[allow(cast_lossless)]
            fn bit_range(&self, msb: usize, lsb: usize) -> $U {
                let mask = ((1 << (msb - lsb + 1)) - 1) << lsb;
                ((self.0 & mask) >> lsb) as $U
            }

            #[inline]
            #[allow(unknown_lints)]
            #[allow(cast_lossless)]
            fn set_bit_range(&mut self, msb: usize, lsb: usize, value: $U) {
                let mask = ((1 << (msb - lsb + 1)) - 1) << lsb;
                self.0 = (self.0 & !mask) | ((value as $T) << lsb);
            }
        }
    };
    ($T:ty => [$($U:ty),*]; $($rest:tt)*) => {
        $(
            impl_bitrange!{ @inner $T => $U }
        )*
        impl_bitrange!{ $($rest)* }
    }
}

#[macro_export(local_inner_macros)]
macro_rules! bitfield_fields {
    // Empty.
    () => {};
    // Dummy.
    (@field ($(#[$attribute:meta])*) ($($vis:tt)*) _, _ : $sty:tt [] in $slot:tt) => {
    };
    (@field ($(#[$attribute:meta])*) ($($vis:tt)*) _, _ : $sty:tt [$msb:tt..$lsb:tt] in $slot:tt as $vty:tt) => {
    };
    // Return all bits.
    (@field ($(#[$attribute:meta])*) ($($vis:tt)*) $getter:tt, _ : $sty:tt [] in $slot:tt) => {
        $(#[$attribute])*
        $($vis)* fn $getter(&self) -> $sty {
            self.$slot
        }
    };
    (@getter ($(#[$attribute:meta])*) ($($vis:tt)*) $getter:tt : $sty:tt [] in $slot:tt) => {
        $(#[$attribute])*
        $($vis)* fn $getter(&self) -> $sty {
            self.$slot
        }
    };
    // Return msb bit as bool.
    (@field ($(#[$attribute:meta])*) ($($vis:tt)*) $getter:tt, _ : $sty:tt [$msb:tt..$lsb:tt] in $slot:tt as bool) => {
        $(#[$attribute])*
        $($vis)* fn $getter(&self) -> bool {
            Bits::<$sty>(self.$slot).bit($msb)
        }
    };
    // Return bit range of [msb..lsb] as U.
    (@field ($(#[$attribute:meta])*) ($($vis:tt)*) $getter:tt, _ : $sty:tt [$msb:tt..$lsb:tt] in $slot:tt as $vty:tt) => {
        $(#[$attribute])*
        $($vis)* fn $getter(&self) -> $vty {
            Bits::<$sty>(self.$slot).bit_range($msb, $lsb)
        }
    };
    // Set all bits with T.
    (@field ($(#[$attribute:meta])*) ($($vis:tt)*) _, $setter:tt : $sty:tt [] in $slot:tt) => {
        $(#[$attribute])*
        $($vis)* fn $setter(&mut self, value: $sty) -> &mut Self {
            self.$slot = value;
            self
        }
    };
    (@setter ($(#[$attribute:meta])*) ($($vis:tt)*) $setter:tt : $sty:tt [] in $slot:tt) => {
        $(#[$attribute])*
        $($vis)* fn $setter(&mut self, value: $sty) -> &mut Self {
            self.$slot = value;
            self
        }
    };
    // Set msb bit with bool.
    (@field ($(#[$attribute:meta])*) ($($vis:tt)*) _, $setter:tt : $sty:tt [$msb:tt..$lsb:tt] in $slot:tt as bool) => {
        $(#[$attribute])*
        $($vis)* fn $setter(&mut self, value: bool) -> &mut Self {
            let mut bits = Bits::<$sty>(self.$slot);
            bits.set_bit($msb, value);
            self.$slot = bits.into();
            self
        }
    };
    // Set bit range of [msb..lsb] with U.
    (@field ($(#[$attribute:meta])*) ($($vis:tt)*) _, $setter:tt : $sty:tt [$msb:tt..$lsb:tt] in $slot:tt as $vty:tt) => {
        $(#[$attribute])*
        $($vis)* fn $setter(&mut self, value: $vty) -> &mut Self {
            let mut bits = Bits::<$sty>(self.$slot);
            bits.set_bit_range($msb, $lsb, value);
            self.$slot = bits.into();
            self
        }
    };
    // Match: pub? <getter>,<setter> : <T> []
    ($(#[$attribute:meta])* $vis:vis $getter:ident, $setter:ident : $sty:tt []; $($rest:tt)*) => {
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [] in 0 }
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) _, $setter : $sty [] in 0 }
        bitfield_fields!{ $($rest)* }
    };
    // Match: pub? <getter>,<setter> : <T> [] in <slot>
    ($(#[$attribute:meta])* $vis:vis $getter:ident, _ : $sty:tt [] in $slot:tt; $($rest:tt)*) => {
        bitfield_fields!{ @getter ($(#[$attribute])*) ($vis) $getter : $sty [] in $slot }
        //bitfield_fields!{ @field ($(#[$attribute])*) () _, $setter : $sty [] in $slot }
        bitfield_fields!{ $($rest)* }
    };
    ($(#[$attribute:meta])* $vis:vis _, $setter:ident : $sty:tt [] in $slot:tt; $($rest:tt)*) => {
        //bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [] in $slot }
        bitfield_fields!{ @setter ($(#[$attribute])*) ($vis) $setter : $sty [] in $slot }
        bitfield_fields!{ $($rest)* }
    };
    // Match: pub? <getter>,<setter> : <T> [<msb>]
    ($(#[$attribute:meta])* $vis:vis $getter:ident, $setter:ident : $sty:tt [$msb:tt]; $($rest:tt)*) => {
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [$msb..$msb] in 0 as $sty }
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) _, $setter : $sty [$msb..$msb] in 0 as $sty }
        bitfield_fields!{ $($rest)* }
    };
    // Match: pub? <getter>,<setter> : <T> [<msb>] in <slot>
    ($(#[$attribute:meta])* $vis:vis $getter:ident, $setter:ident : $sty:tt [$msb:tt] in $slot:tt; $($rest:tt)*) => {
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [$msb..$msb] in $slot as $sty }
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) _, $setter : $sty [$msb..$msb] in $slot as $sty }
        bitfield_fields!{ $($rest)* }
    };
    // Match: pub? <getter>,<setter> : <T> [<msb>] as <U>
    ($(#[$attribute:meta])* $vis:vis $getter:ident, $setter:ident : $sty:tt [$msb:tt] as $vty:tt; $($rest:tt)*) => {
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [$msb..$msb] in 0 as $vty }
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) _, $setter : $sty [$msb..$msb] in 0 as $vty }
        bitfield_fields!{ $($rest)* }
    };
    // Match: pub? <getter>,<setter> : <T> [<msb>] in <slot> as <U>
    ($(#[$attribute:meta])* $vis:vis $getter:ident, $setter:ident : $sty:tt [$msb:tt] in $slot:tt as $vty:tt; $($rest:tt)*) => {
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [$msb..$msb] in $slot as $vty }
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) _, $setter : $sty [$msb..$msb] in $slot as $vty }
        bitfield_fields!{ $($rest)* }
    };
    // Match: pub? <getter>,<setter> : <T> [<msb>..<lsb>]
    ($(#[$attribute:meta])* $vis:vis $getter:ident, $setter:ident : $sty:tt [$msb:tt..$lsb:tt]; $($rest:tt)*) => {
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [$msb..$lsb] in 0 as $sty }
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) _, ($setter) : $sty [$msb..$lsb] in 0 as $sty }
        bitfield_fields!{ $($rest)* }
    };
    // Match: pub? <getter>,<setter> : <T> [<msb>..<lsb>] in <slot>
    ($(#[$attribute:meta])* $vis:vis $getter:ident, $setter:ident : $sty:tt [$msb:tt..$lsb:tt] in $slot:tt; $($rest:tt)*) => {
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [$msb..$lsb] in $slot as $sty }
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) _, $setter : $sty [$msb..$lsb] in $slot as $sty }
        bitfield_fields!{ $($rest)* }
    };
    // Match: pub? <getter>,<setter> : <T> [<msb>..<lsb>] as <U>
    ($(#[$attribute:meta])* $vis:vis $getter:ident, $setter:ident : $sty:tt [$msb:tt..$lsb:tt] as $vty:tt; $($rest:tt)*) => {
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [$msb..$lsb] in 0 as $vty }
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) _, $setter : $sty [$msb..$lsb] in 0 as $vty }
        bitfield_fields!{ $($rest)* }
    };
    // Match: pub? <getter>,<setter> : <T> [<msb>..<lsb>] in <slot> as <U>
    ($(#[$attribute:meta])* $vis:vis $getter:ident, $setter:ident : $sty:tt [$msb:tt..$lsb:tt] in $slot:tt as $vty:tt; $($rest:tt)*) => {
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) $getter, _ : $sty [$msb..$lsb] in $slot as $vty }
        bitfield_fields!{ @field ($(#[$attribute])*) ($vis) _, $setter : $sty [$msb..$lsb] in $slot as $vty }
        bitfield_fields!{ $($rest)* }
    };
}

impl_bits! {
    i8 => [bool, i8, i16, i32, i64, u8, u16, u32, u64];
    i16 => [bool, i8, i16, i32, i64, u8, u16, u32, u64];
    i32 => [bool, i8, i16, i32, i64, u8, u16, u32, u64];
    i64 => [bool, i8, i16, i32, i64, u8, u16, u32, u64];
    u8 => [bool, i8, i16, i32, i64, u8, u16, u32, u64];
    u16 => [bool, i8, i16, i32, i64, u8, u16, u32, u64];
    u32 => [bool, i8, i16, i32, i64, u8, u16, u32, u64];
    u64 => [bool, i8, i16, i32, i64, u8, u16, u32, u64];
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Copy, Clone, Default, Debug)]
    struct FooBar(u8,u16,u32,u64);

    impl FooBar {
        bitfield_fields! {
            // u8
            f1a, set_f1a : u8 [0];
            f1b, set_f1b : u8 [1] in 0;
            f1c, set_f1c : u8 [2] as bool;
            f1d, set_f1d : u8 [4..3] in 0 as u8;
            f1e, set_f1e : u8 [7..5] in 0 as u16;
            pub all1, _ : u8 [] in 0;
            // u16
            f2a, set_f2a : u16 [15] in 1 as bool;
            f2b, set_f2b : u16 [14..0] in 1;
            pub all2, _ : u16 [] in 1;
            // u32
            f3a, set_f3a : u32 [7..0] in 2 as u8;
            f3b, set_f3b : u32 [15..8] in 2 as u8;
            f3c, set_f3c : u32 [15..8] in 2 as u8;
            pub all3, _ : u32 [] in 2;
            // u64
            rsv, _ : u64 [] in 3;
            _, set_rsv: u64 [] in 3;
        }
    }

    #[test]
    fn test_foobar() {
        let mut a = FooBar(0x77, 0x55AA, 0xFF77_AA55, 0x0000_FFFF_FF77_AA55);
        assert_eq!(1, a.f1a());
        assert_eq!(1, a.f1b());
        assert_eq!(true, a.f1c());
        assert_eq!(false, a.set_f1c(false).f1c());
        assert_eq!(0x73, a.all1());
        assert_eq!(2, a.f1d());
        assert_eq!(0, a.set_f1d(0).f1d());
        assert_eq!(0x63, a.all1());
        assert_eq!(3, a.f1e());
        assert_eq!(7, a.set_f1e(7).f1e());
        assert_eq!(0xE3, a.all1());
        assert_eq!(0x0000_FFFF_FF77_AA55, a.rsv());
    }
}