bitcraft 1.0.0

A zero-cost, hardware-aligned bitfield and enumeration generator.
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
/// A declarative macro for generating atomic bitfields.
///
/// This macro generates a `#[repr(transparent)]` struct wrapping an atomic integer
/// (`AtomicU8`, `AtomicU16`, `AtomicU32`, `AtomicU64`). It automatically generates
/// getters and setters that take memory orderings, allowing lock-free concurrent mutation
/// of individual bitfields safely via `fetch_update`.
///
/// # Example
///
/// ```rust
/// use bitcraft::atomic_bitstruct;
/// use portable_atomic::Ordering;
///
/// atomic_bitstruct! {
///     pub struct ConcurrentFlags(AtomicU32) {
///         pub is_ready: bool = 1,
///         pub status: u8 = 3,
///         pub retries: i16 = 12, // Native signed extraction
///     }
/// }
///
/// let flags = ConcurrentFlags::new(0);
/// flags.set_is_ready(true, Ordering::Release);
/// flags.set_retries(-500, Ordering::Release);
///
/// assert!(flags.is_ready(Ordering::Acquire));
/// assert_eq!(flags.retries(Ordering::Acquire), -500);
/// ```
#[macro_export]
macro_rules! atomic_bitstruct {
    (
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicU8) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicU8, u8, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

(
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicU16) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicU16, u16, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

(
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicU32) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicU32, u32, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

(
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicU64) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicU64, u64, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

(
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicU128) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicU128, u128, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

(
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicI8) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicI8, i8, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

(
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicI16) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicI16, i16, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

(
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicI32) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicI32, i32, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

(
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicI64) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicI64, i64, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

(
    $(#[$meta:meta])*
    $vis:vis struct $struct_name:ident (AtomicI128) {
        $(
            $field_vis:vis $field_name:ident: $field_type:tt = $bits:tt
        ),* $(,)?
    }
) => {
    $crate::atomic_bitstruct!(@impl $(#[$meta])* $vis $struct_name $crate::reexport::portable_atomic::AtomicI128, i128, { $( $field_vis $field_name: $field_type = $bits ),* }, $($field_vis $field_name $field_type $bits)*);
};

    (@impl $(#[$meta:meta])* $vis:vis $struct_name:ident $atomic_ty:ty, $base_type:ty, { $($field_vis_struct:vis $field_name_struct:ident: $field_type_struct:tt = $bits_struct:tt),* }, $($field_vis:vis $field_name:ident $field_type:tt $bits:tt)*) => {
        $(#[$meta])*
        #[repr(transparent)]
        $vis struct $struct_name(pub $atomic_ty);

        impl core::fmt::Debug for $struct_name {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                f.debug_struct(stringify!($struct_name))
                    .field("raw", &self.0.load($crate::reexport::portable_atomic::Ordering::Relaxed))
                    $(
                        .field(stringify!($field_name), &self.$field_name($crate::reexport::portable_atomic::Ordering::Relaxed))
                    )*
                    .finish()
            }
        }

        const _: () = {
            let _ = <$base_type as $crate::IsValidBaseInt>::ASSERT_VALID;
            $crate::bitstruct!(@check_fields $($field_type)*);

            #[allow(dead_code)]
            const TOTAL_BITS: usize = 0 $( + $bits )*;
            assert!(TOTAL_BITS <= <$base_type as $crate::IsValidBaseInt>::MAX_BITS, "Sum of field bits exceeds base type max bits");
        };

        impl Default for $struct_name {
            fn default() -> Self {
                Self::new(0)
            }
        }

        impl $struct_name {
            #[allow(dead_code)]
            pub const BITS: usize = <$base_type as $crate::BitLength>::BITS;

            /// Creates a new instance from a raw integer value.
            #[inline(always)]
            #[allow(dead_code)]
            pub const fn new(val: $base_type) -> Self {
                Self(<$atomic_ty>::new(val))
            }

            /// Returns the raw interior integer value via `load`.
            #[inline(always)]
            #[allow(dead_code)]
            pub fn load(&self, order: $crate::reexport::portable_atomic::Ordering) -> $base_type {
                self.0.load(order)
            }

            /// Stores a raw integer value via `store`.
            #[inline(always)]
            #[allow(dead_code)]
            pub fn store(&self, val: $base_type, order: $crate::reexport::portable_atomic::Ordering) {
                self.0.store(val, order)
            }

            $crate::atomic_bitstruct!(@impl_getters_setters $base_type, 0, $($field_vis $field_name $field_type $bits)*);
        }

        $crate::paste::paste! {
            $crate::bitstruct! {
                #[doc = concat!("A non-atomic value snapshot of `", stringify!($struct_name), "` used for batch updates.")]
                $vis struct [<$struct_name Value>]($base_type) {
                    $(
                        $field_vis_struct $field_name_struct: $field_type_struct = $bits_struct,
                    )*
                }
            }

            impl $struct_name {
                /// Returns a non-atomic snapshot of the current state as a `Value` struct.
                #[inline]
                pub fn get(&self, order: $crate::reexport::portable_atomic::Ordering) -> [<$struct_name Value>] {
                    [<$struct_name Value>]::from_bits(self.0.load(order))
                }
                /// Completely overwrites the entire atomic state with the given `Value`.
                /// This is a direct atomic `store` operation and does not perform a CAS loop.
                #[inline]
                pub fn set(&self, val: [<$struct_name Value>], order: $crate::reexport::portable_atomic::Ordering) {
                    self.0.store(val.to_bits(), order);
                }

                /// Atomically updates multiple fields using a Compare-And-Swap (CAS) loop.
                ///
                /// The provided closure is called with a mutable `Value` representing the current state.
                /// Modify the value, and the changes will be applied atomically.
                ///
                /// Unlike `set`, this method guarantees that fields you do not modify within the closure
                /// will retain any concurrent updates made by other threads between the load and the store.
                #[inline]
                pub fn update<F>(&self, set_order: $crate::reexport::portable_atomic::Ordering, fetch_order: $crate::reexport::portable_atomic::Ordering, mut f: F) -> [<$struct_name Value>]
                where
                    F: FnMut(&mut [<$struct_name Value>])
                {
                    let raw_prev = self.0.fetch_update(set_order, fetch_order, |raw| {
                        let mut snap = [<$struct_name Value>]::from_bits(raw);
                        f(&mut snap);
                        Some(snap.to_bits())
                    }).unwrap();
                    [<$struct_name Value>]::from_bits(raw_prev)
                }

                /// Conditionally updates multiple fields using a Compare-And-Swap (CAS) loop.
                ///
                /// The provided closure must return `Some(())` to commit the new state, or `None` to abort the loop.
                /// If `None` is returned, the CAS loop is aborted and `Err(Value)` containing the un-modified state is returned.
                #[inline]
                pub fn update_or_abort<F>(&self, set_order: $crate::reexport::portable_atomic::Ordering, fetch_order: $crate::reexport::portable_atomic::Ordering, mut f: F) -> Result<[<$struct_name Value>], [<$struct_name Value>]>
                where
                    F: FnMut(&mut [<$struct_name Value>]) -> Option<()>
                {
                    self.0.fetch_update(set_order, fetch_order, |raw| {
                        let mut snap = [<$struct_name Value>]::from_bits(raw);
                        f(&mut snap).map(|_| snap.to_bits())
                    }).map(|raw| [<$struct_name Value>]::from_bits(raw))
                    .map_err(|raw| [<$struct_name Value>]::from_bits(raw))
                }
            }
        }
    };

    (@impl_getters_setters $base_type:ty, $shift:expr, ) => {};

    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident bool $bits:tt $($rest:tt)*) => {
        $crate::paste::paste! {
            pub const [<$field_name:upper _OFFSET>]: usize = $shift;
            pub const [<$field_name:upper _BITS>]: usize = $bits;
            #[doc(hidden)]
            const [<$field_name:upper _MASK>]: $base_type = ((!0 as <$base_type as $crate::IsValidBaseInt>::Unsigned) >> (<$base_type as $crate::BitLength>::BITS - Self::[<$field_name:upper _BITS>])) as $base_type;

            #[allow(dead_code)]
            #[inline]
            $field_vis fn $field_name(&self, order: $crate::reexport::portable_atomic::Ordering) -> bool {
                ((self.0.load(order) >> Self::[<$field_name:upper _OFFSET>]) & Self::[<$field_name:upper _MASK>]) != 0
            }

            #[allow(dead_code)]
            #[inline]
            $field_vis fn [<set_ $field_name>](&self, val: bool, order: $crate::reexport::portable_atomic::Ordering) {
                let val_masked = val as $base_type;
                self.0.fetch_update(order, $crate::reexport::portable_atomic::Ordering::Relaxed, |raw| {
                    Some((raw & !(Self::[<$field_name:upper _MASK>] << Self::[<$field_name:upper _OFFSET>])) | (val_masked << Self::[<$field_name:upper _OFFSET>]))
                }).unwrap();
            }

            #[allow(dead_code)]
            $field_vis fn [<try_set_ $field_name>](&self, val: bool, order: $crate::reexport::portable_atomic::Ordering) -> Result<(), $crate::BitstructError> {
                self.[<set_ $field_name>](val, order);
                Ok(())
            }
        }
        $crate::atomic_bitstruct!(@impl_getters_setters $base_type, $shift + $bits, $($rest)*);
    };

    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident u8 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_int $base_type, $shift, $field_vis $field_name u8 $bits $($rest)*); };
    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident u16 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_int $base_type, $shift, $field_vis $field_name u16 $bits $($rest)*); };
    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident u32 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_int $base_type, $shift, $field_vis $field_name u32 $bits $($rest)*); };
    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident u64 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_int $base_type, $shift, $field_vis $field_name u64 $bits $($rest)*); };
    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident u128 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_int $base_type, $shift, $field_vis $field_name u128 $bits $($rest)*); };
    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident i8 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_signed_int $base_type, $shift, $field_vis $field_name i8 $bits $($rest)*); };
    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident i16 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_signed_int $base_type, $shift, $field_vis $field_name i16 $bits $($rest)*); };
    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident i32 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_signed_int $base_type, $shift, $field_vis $field_name i32 $bits $($rest)*); };
    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident i64 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_signed_int $base_type, $shift, $field_vis $field_name i64 $bits $($rest)*); };
    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident i128 $bits:tt $($rest:tt)*) => { $crate::atomic_bitstruct!(@impl_signed_int $base_type, $shift, $field_vis $field_name i128 $bits $($rest)*); };

    (@impl_int $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident $field_type:tt $bits:tt $($rest:tt)*) => {
        $crate::paste::paste! {
            pub const [<$field_name:upper _OFFSET>]: usize = $shift;
            pub const [<$field_name:upper _BITS>]: usize = $bits;
            #[doc(hidden)]
            const [<$field_name:upper _MASK>]: $base_type = ((!0 as <$base_type as $crate::IsValidBaseInt>::Unsigned) >> (<$base_type as $crate::BitLength>::BITS - Self::[<$field_name:upper _BITS>])) as $base_type;

            #[allow(dead_code)]
            #[inline]
            $field_vis fn $field_name(&self, order: $crate::reexport::portable_atomic::Ordering) -> $field_type {
                ((self.0.load(order) >> Self::[<$field_name:upper _OFFSET>]) & Self::[<$field_name:upper _MASK>]) as $field_type
            }

            #[allow(dead_code)]
            #[inline]
            $field_vis fn [<set_ $field_name>](&self, val: $field_type, order: $crate::reexport::portable_atomic::Ordering) {
                debug_assert!((val as $base_type) <= Self::[<$field_name:upper _MASK>], "Value {} overflows allocated {} bits", val, $bits);
                let val_masked = (val as $base_type) & Self::[<$field_name:upper _MASK>];
                self.0.fetch_update(order, $crate::reexport::portable_atomic::Ordering::Relaxed, |raw| {
                    Some((raw & !(Self::[<$field_name:upper _MASK>] << Self::[<$field_name:upper _OFFSET>])) | (val_masked << Self::[<$field_name:upper _OFFSET>]))
                }).unwrap();
            }

            #[allow(dead_code)]
            $field_vis fn [<try_set_ $field_name>](&self, val: $field_type, order: $crate::reexport::portable_atomic::Ordering) -> Result<(), $crate::BitstructError> {
                if (val as $base_type) > Self::[<$field_name:upper _MASK>] {
                    return Err($crate::BitstructError::Overflow { value: (val as $base_type) as u128, allocated_bits: $bits });
                }
                let val_masked = (val as $base_type) & Self::[<$field_name:upper _MASK>];
                self.0.fetch_update(order, $crate::reexport::portable_atomic::Ordering::Relaxed, |raw| {
                    Some((raw & !(Self::[<$field_name:upper _MASK>] << Self::[<$field_name:upper _OFFSET>])) | (val_masked << Self::[<$field_name:upper _OFFSET>]))
                }).unwrap();
                Ok(())
            }
        }
        $crate::atomic_bitstruct!(@impl_getters_setters $base_type, $shift + $bits, $($rest)*);
    };

    (@impl_signed_int $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident $field_type:tt $bits:tt $($rest:tt)*) => {
        $crate::paste::paste! {
            pub const [<$field_name:upper _OFFSET>]: usize = $shift;
            pub const [<$field_name:upper _BITS>]: usize = $bits;
            #[doc(hidden)]
            const [<$field_name:upper _MASK>]: $base_type = ((!0 as <$base_type as $crate::IsValidBaseInt>::Unsigned) >> (<$base_type as $crate::BitLength>::BITS - Self::[<$field_name:upper _BITS>])) as $base_type;

            #[doc(hidden)]
            pub const [<$field_name:upper _MIN>]: $field_type = (!0 as $field_type) << (Self::[<$field_name:upper _BITS>] - 1);
            #[doc(hidden)]
            pub const [<$field_name:upper _MAX>]: $field_type = !Self::[<$field_name:upper _MIN>];
            #[doc(hidden)]
            const [<$field_name:upper _SHIFT_UP>]: usize = <$field_type as $crate::BitLength>::BITS - Self::[<$field_name:upper _BITS>];

            #[allow(dead_code)]
            #[inline]
            $field_vis fn $field_name(&self, order: $crate::reexport::portable_atomic::Ordering) -> $field_type {
                let raw = ((self.0.load(order) >> Self::[<$field_name:upper _OFFSET>]) & Self::[<$field_name:upper _MASK>]) as $field_type;
                (raw << Self::[<$field_name:upper _SHIFT_UP>]) >> Self::[<$field_name:upper _SHIFT_UP>]
            }

            #[allow(dead_code)]
            #[inline]
            $field_vis fn [<set_ $field_name>](&self, val: $field_type, order: $crate::reexport::portable_atomic::Ordering) {
                debug_assert!(val >= Self::[<$field_name:upper _MIN>] && val <= Self::[<$field_name:upper _MAX>], "Value {} out of bounds for {} bits", val, $bits);
                let val_masked = (val as $base_type) & Self::[<$field_name:upper _MASK>];
                self.0.fetch_update(order, $crate::reexport::portable_atomic::Ordering::Relaxed, |raw| {
                    Some((raw & !(Self::[<$field_name:upper _MASK>] << Self::[<$field_name:upper _OFFSET>])) | (val_masked << Self::[<$field_name:upper _OFFSET>]))
                }).unwrap();
            }

            #[allow(dead_code)]
            $field_vis fn [<try_set_ $field_name>](&self, val: $field_type, order: $crate::reexport::portable_atomic::Ordering) -> Result<(), $crate::BitstructError> {
                if val < Self::[<$field_name:upper _MIN>] || val > Self::[<$field_name:upper _MAX>] {
                    return Err($crate::BitstructError::Overflow { value: val as i128 as u128, allocated_bits: $bits });
                }
                let val_masked = (val as $base_type) & Self::[<$field_name:upper _MASK>];
                self.0.fetch_update(order, $crate::reexport::portable_atomic::Ordering::Relaxed, |raw| {
                    Some((raw & !(Self::[<$field_name:upper _MASK>] << Self::[<$field_name:upper _OFFSET>])) | (val_masked << Self::[<$field_name:upper _OFFSET>]))
                }).unwrap();
                Ok(())
            }
        }
        $crate::atomic_bitstruct!(@impl_getters_setters $base_type, $shift + $bits, $($rest)*);
    };

    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident ($field_type:ty) $bits:tt $($rest:tt)*) => {
        $crate::paste::paste! {
            pub const [<$field_name:upper _OFFSET>]: usize = $shift;
            pub const [<$field_name:upper _BITS>]: usize = $bits;
            #[doc(hidden)]
            const [<$field_name:upper _MASK>]: $base_type = ((!0 as <$base_type as $crate::IsValidBaseInt>::Unsigned) >> (<$base_type as $crate::BitLength>::BITS - Self::[<$field_name:upper _BITS>])) as $base_type;

            #[allow(dead_code)]
            #[inline]
            $field_vis fn $field_name(&self, order: $crate::reexport::portable_atomic::Ordering) -> $field_type {
                ((self.0.load(order) >> Self::[<$field_name:upper _OFFSET>]) & Self::[<$field_name:upper _MASK>]) as $field_type
            }

            #[allow(dead_code)]
            #[inline]
            $field_vis fn [<set_ $field_name>](&self, val: $field_type, order: $crate::reexport::portable_atomic::Ordering) {
                debug_assert!((val as $base_type) <= Self::[<$field_name:upper _MASK>], "Value {} overflows allocated {} bits", val, $bits);
                let val_masked = (val as $base_type) & Self::[<$field_name:upper _MASK>];
                self.0.fetch_update(order, $crate::reexport::portable_atomic::Ordering::Relaxed, |raw| {
                    Some((raw & !(Self::[<$field_name:upper _MASK>] << Self::[<$field_name:upper _OFFSET>])) | (val_masked << Self::[<$field_name:upper _OFFSET>]))
                }).unwrap();
            }

            #[allow(dead_code)]
            $field_vis fn [<try_set_ $field_name>](&self, val: $field_type, order: $crate::reexport::portable_atomic::Ordering) -> Result<(), $crate::BitstructError> {
                if (val as $base_type) > Self::[<$field_name:upper _MASK>] {
                    return Err($crate::BitstructError::Overflow { value: (val as $base_type) as u128, allocated_bits: $bits });
                }
                let val_masked = (val as $base_type) & Self::[<$field_name:upper _MASK>];
                self.0.fetch_update(order, $crate::reexport::portable_atomic::Ordering::Relaxed, |raw| {
                    Some((raw & !(Self::[<$field_name:upper _MASK>] << Self::[<$field_name:upper _OFFSET>])) | (val_masked << Self::[<$field_name:upper _OFFSET>]))
                }).unwrap();
                Ok(())
            }
        }
        $crate::atomic_bitstruct!(@impl_getters_setters $base_type, $shift + $bits, $($rest)*);
    };

    (@impl_getters_setters $base_type:ty, $shift:expr, $field_vis:vis $field_name:ident $field_type:tt $bits:tt $($rest:tt)*) => {
        $crate::paste::paste! {
            pub const [<$field_name:upper _OFFSET>]: usize = $shift;
            pub const [<$field_name:upper _BITS>]: usize = $bits;
            #[doc(hidden)]
            const [<$field_name:upper _MASK>]: $base_type = ((!0 as <$base_type as $crate::IsValidBaseInt>::Unsigned) >> (<$base_type as $crate::BitLength>::BITS - Self::[<$field_name:upper _BITS>])) as $base_type;

            #[allow(dead_code)]
            #[inline]
            $field_vis fn $field_name(&self, order: $crate::reexport::portable_atomic::Ordering) -> $field_type {
                $field_type::from_bits(((self.0.load(order) >> Self::[<$field_name:upper _OFFSET>]) & Self::[<$field_name:upper _MASK>]) as _)
            }

            #[allow(dead_code)]
            #[inline]
            $field_vis fn [<set_ $field_name>](&self, val: $field_type, order: $crate::reexport::portable_atomic::Ordering) {
                const _: () = assert!(<$field_type>::BITS <= $bits, "Enum bit width exceeds allocated field width");
                let val_masked = (val.to_bits() as $base_type) & Self::[<$field_name:upper _MASK>];
                self.0.fetch_update(order, $crate::reexport::portable_atomic::Ordering::Relaxed, |raw| {
                    Some((raw & !(Self::[<$field_name:upper _MASK>] << Self::[<$field_name:upper _OFFSET>])) | (val_masked << Self::[<$field_name:upper _OFFSET>]))
                }).unwrap();
            }

            #[allow(dead_code)]
            $field_vis fn [<try_set_ $field_name>](&self, val: $field_type, order: $crate::reexport::portable_atomic::Ordering) -> Result<(), $crate::BitstructError> {
                self.[<set_ $field_name>](val, order);
                Ok(())
            }
        }
        $crate::atomic_bitstruct!(@impl_getters_setters $base_type, $shift + $bits, $($rest)*);
    };
}