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
//! # chapa
//!
//! Bitfield structs, batteries included!
//!
//! `chapa` exposes a single attribute macro, [`bitfield`], that turns an ordinary
//! struct into a newtype backed by a single primitive. Every field maps to an exact
//! range of bits and gets a generated getter, setter, and `with_*` builder.
//!
//! ## Features
//!
//! - **MSB0 and LSB0 support**: Naturally write bit orders as per datasheet
//! - **Enum fields**: Use enums as bitfield fields with `#[derive(BitEnum)]`
//! - **Nested bitfields**: Embed one bitfield struct inside another
//! - **Readonly fields**: Suppress setter generation with `readonly` or a leading `_` prefix
//! - **Default values**: Give fields a `default = ...` and `#[derive(Default)]` to bake them in
//! - **Aliases**: Expose extra accessor names with `alias = "name"` or `alias = ["a", "b"]`
//! - **Overlays**: Allow multiple logically distinct field groups to share the same bit range
//! - **Bitwise operators**: `&`, `|`, `^`, `!`, `&=`, `|=`, `^=` with the backing storage type work directly on the struct
//! - **Bit extraction**: [`extract_bits!`] masks a value to keep only the specified bit ranges
//! - **Reflection**: Opt into the `reflection` feature for compile-time field metadata (`FIELDS`, bit positions, enum variants)
//!
//! ## MSRV
//!
//! Requires Rust 1.83 or newer (the generated getters, setters, and `with_*`
//! builders are `const fn`).
//!
//! ## Quick start
//!
//! ```rust
//! use chapa::bitfield;
//!
//! // An 8-bit status register, bit 0 is the LSB
//! #[bitfield(u8, order = lsb0)]
//! #[derive(Copy, Clone, Debug, PartialEq)]
//! pub struct StatusReg {
//! #[bits(0)] enabled: bool,
//! #[bits(1..=3)] mode: u8,
//! #[bits(4..=7)] _reserved: u8, // Can be omitted; "_" makes it readonly
//! }
//!
//! let r = StatusReg::zeroed()
//! .with_enabled(true)
//! .with_mode(5);
//!
//! assert_eq!(r.enabled(), true);
//! assert_eq!(r.mode(), 5);
//! assert_eq!(r.reserved(), 0); // accessible as `reserved`, not `_reserved`
//! ```
//!
//! ## `#[bitfield(...)]` options
//!
//! | Option | Required | Description |
//! |---|---|---|
//! | `u8` / `u16` / `u32` / `u64` / `u128` | Yes | Backing storage type |
//! | `order = msb0` / `order = lsb0` | Yes | Bit numbering convention |
//! | `width = N` | No | Effective logical width, must be <= storage width |
//!
//! ## `#[bits(...)]` options
//!
//! | Option | Description |
//! |---|---|
//! | `N` | Single bit at index N |
//! | `N..=M` | Inclusive range from bit N to bit M |
//! | `N..M` | Half-open range (equivalent to `N..=(M-1)`) |
//! | `readonly` | Suppress `set_*` and `with_*` generation |
//! | `default = <expr>` | Starting value applied by `default()` |
//! | `alias = "name"` | Generate additional accessor under `name` |
//! | `alias = ["a","b"]` | Multiple aliases |
//! | `overlay = "group"` | Allow overlap with fields in other overlay groups |
//!
//! ## MSB-0 example
//!
//! ```rust
//! use chapa::bitfield;
//!
//! // A 32-bit value where bit 0 is the most-significant bit
//! #[bitfield(u32, order = msb0)]
//! #[derive(Copy, Clone, Debug, PartialEq)]
//! pub struct ControlWord {
//! #[bits(0..=3)] opcode: u8,
//! #[bits(4..=7)] dst: u8,
//! #[bits(8..=31, readonly)] payload: u32,
//! }
//!
//! let cw = ControlWord::zeroed()
//! .with_opcode(0xA)
//! .with_dst(0x3);
//! assert_eq!(cw.raw(), 0xA300_0000);
//! ```
//!
//! ## Enum fields
//!
//! Use `#[derive(BitEnum)]` on an enum to automatically implement [`BitField`],
//! allowing it to be used as a bitfield field type. The enum must also derive
//! `Copy` + `Clone`, and mark exactly one variant `#[fallback]`.
//!
//! `from_raw` coerces any unrecognized raw value to the `#[fallback]` variant;
//! [`BitField::try_from_raw`] (or `TryFrom`) reports it as
//! [`InvalidBitPattern`] instead.
//!
//! ```rust
//! use chapa::{bitfield, BitEnum};
//!
//! #[derive(Debug, PartialEq, Clone, Copy, BitEnum)]
//! pub enum VideoFormat {
//! Ntsc = 0,
//! Pal = 1,
//! Mpal = 2,
//! #[fallback]
//! Debug = 3,
//! }
//!
//! #[bitfield(u16, order = lsb0)]
//! #[derive(Copy, Clone, Debug, PartialEq)]
//! pub struct DisplayConfig {
//! #[bits(0)] enable: bool,
//! #[bits(1..=2)] fmt: VideoFormat,
//! }
//!
//! let dc = DisplayConfig::zeroed()
//! .with_enable(true)
//! .with_fmt(VideoFormat::Pal);
//! assert_eq!(dc.fmt(), VideoFormat::Pal);
//! ```
//!
//! ## Nested bitfields
//!
//! A field whose type implements [`BitField`] (i.e. any type annotated with
//! `#[bitfield]`) can be used as a nested field.
//!
//! ```rust
//! use chapa::bitfield;
//!
//! #[bitfield(u8, order = msb0, width = 4)]
//! #[derive(Copy, Clone, Debug, PartialEq)]
//! pub struct Nibble {
//! #[bits(0..=1)] high: u8,
//! #[bits(2..=3)] low: u8,
//! }
//!
//! #[bitfield(u32, order = msb0)]
//! #[derive(Copy, Clone, Debug, PartialEq)]
//! pub struct Word {
//! #[bits(0..=3)] top: Nibble,
//! #[bits(28..=31)] bot: u8,
//! }
//! ```
//!
//! ## Overlay groups
//!
//! Fields in **different** overlay groups may share bit ranges. This is useful for
//! instruction formats where the same bits are interpreted differently depending on
//! other bits, such as instruction decoding or MMIO registers that change meaning
//! based on encoded bits.
//!
//! ```rust
//! use chapa::bitfield;
//!
//! #[bitfield(u32, order = msb0)]
//! #[derive(Copy, Clone, Debug, PartialEq)]
//! pub struct Instr {
//! #[bits(0..=5)] opcode: u8,
//!
//! #[bits(6..=10, overlay = "r_form")] rs: u8,
//! #[bits(11..=15, overlay = "r_form")] ra: u8,
//! #[bits(16..=20, overlay = "r_form")] rb: u8,
//!
//! #[bits(6..=10, overlay = "i_form")] dst: u8,
//! #[bits(11..=31, overlay = "i_form")] imm: u32,
//! }
//! ```
//!
//! ## Constructors and default values
//!
//! Every struct gets a `const fn zeroed()` returning an all-zero instance. There
//! is no `new()`. To give a field its own starting value, add `default = <expr>`
//! and `#[derive(Default)]`: the generated `default()` applies those values,
//! while `zeroed()` and `from_raw` always ignore them.
//!
//! Works on any field type (`bool`, integer, `#[derive(BitEnum)]` enum, or
//! nested bitfield, e.g. `default = Mode::On`), including `readonly` ones;
//! values wider than the field truncate to its width, like a setter. A `default`
//! without `#[derive(Default)]` is a compile error, since it would never apply.
//!
//! ```rust
//! use chapa::bitfield;
//!
//! #[bitfield(u16, order = lsb0)]
//! #[derive(Copy, Clone, Debug, PartialEq, Default)]
//! pub struct Config {
//! #[bits(0)] enabled: bool,
//! #[bits(1..=3, default = 5)] mode: u8,
//! #[bits(8, default = true)] ready: bool,
//! }
//!
//! let c = Config::default();
//! assert_eq!(c.mode(), 5);
//! assert_eq!(c.ready(), true);
//! assert_eq!(c.enabled(), false); // no default -> zero
//! // zeroed() and from_raw never inject defaults
//! assert_eq!(Config::zeroed().mode(), 0);
//! assert_eq!(Config::from_raw(0).mode(), 0);
//! ```
//!
//! ## Bitwise operations
//!
//! Every bitfield struct implements [`BitAnd`](core::ops::BitAnd),
//! [`BitOr`](core::ops::BitOr), [`BitXor`](core::ops::BitXor),
//! [`Not`](core::ops::Not), [`BitAndAssign`](core::ops::BitAndAssign),
//! [`BitOrAssign`](core::ops::BitOrAssign), and
//! [`BitXorAssign`](core::ops::BitXorAssign) against its backing storage type.
//!
//! ```rust
//! use chapa::bitfield;
//!
//! #[bitfield(u32, order = msb0)]
//! #[derive(Copy, Clone, PartialEq, Debug)]
//! pub struct StatusReg {
//! #[bits(0)] enabled: bool,
//! #[bits(1..=7)] flags: u8,
//! }
//!
//! const MASK: u32 = 0x0000_00FF;
//! let a = StatusReg::zeroed().with_enabled(true);
//! let b: u32 = 0x0000_00AA;
//!
//! let result = (a & !MASK) | (b & MASK); // result: StatusReg
//! ```
//!
//! ## Bit extraction with `extract_bits!`
//!
//! [`extract_bits!`] keeps only the specified bit positions from a value, zeroing all others.
//! Bits can be single indices or inclusive ranges; the ordering and storage type are either
//! supplied explicitly (for raw integers) or deduced from the struct's [`BitField`] impl.
//!
//! ```rust
//! use chapa::{bitfield, extract_bits};
//!
//! #[bitfield(u32, order = msb0)]
//! #[derive(Copy, Clone)]
//! pub struct Msr { /* ... */ }
//!
//! let msr = Msr::from_raw(0xFFFF_FFFF);
//!
//! // Struct form: ordering deduced; returns Msr with non-selected bits zeroed
//! let masked: Msr = extract_bits!(msr; 0..=0, 5..=9, 16..=31);
//!
//! // Explicit form for raw integers: const-evaluated mask
//! let raw: u32 = extract_bits!(msb0 u32; 0xFFFF_FFFFu32; 0, 5..=9, 16..=31);
//! assert_eq!(raw, masked.raw());
//! ```
//!
//! See the [`extract_bits!`] documentation for full syntax details.
//!
//! ## Reflection
//!
//! Enable the `reflection` feature to get compile-time field metadata for every
//! `#[bitfield]` struct and `#[derive(BitEnum)]` enum. Each struct gains an
//! inherent `FIELDS: &'static [FieldInfo]` const; the types ([`FieldInfo`],
//! [`FieldKind`], [`EnumInfo`]) and the [`Reflect`] trait are re-exported at the
//! crate root when the feature is on. Offsets and widths are **physical** (in
//! storage-value coordinates), so a field's value is always
//! `(raw >> offset) & ((1 << width) - 1)` regardless of `msb0`/`lsb0` ordering.
//!
//! ```ignore
//! use chapa::{bitfield, BitEnum, FieldKind};
//!
//! #[derive(Copy, Clone, BitEnum)]
//! pub enum Mode { Off = 0, On = 1, #[fallback] Reserved = 3 }
//!
//! #[bitfield(u16, order = lsb0)]
//! #[derive(Copy, Clone)]
//! pub struct Reg {
//! #[bits(0)] enabled: bool,
//! #[bits(1..=2)] mode: Mode,
//! #[bits(4..=7)] count: u8,
//! }
//!
//! let mode = Reg::FIELDS.iter().find(|f| f.name == "mode").unwrap();
//! assert_eq!((mode.offset, mode.width), (1, 2));
//! if let FieldKind::Enum(info) = mode.kind {
//! assert_eq!(info.variants, &[(0, "Off"), (1, "On"), (3, "Reserved")]);
//! }
//! ```
//!
//! ## Generated API
//!
//! For a field `foo: u8` spanning bits `4..=7` the macro generates:
//!
//! | Item | Signature |
//! |---|---|
//! | Constant | `pub const FOO_SHIFT: u32` |
//! | Constant | `pub const FOO_MASK: StorageType` |
//! | Getter | `pub const fn foo(&self) -> u8` |
//! | Setter | `pub const fn set_foo(&mut self, val: u8)` |
//! | Builder | `pub const fn with_foo(self, val: u8) -> Self` |
//!
//! Additionally, every struct implements the following traits:
//!
//! | Trait | Signature |
//! |-------------------|------------------------------------------------------|
//! | `BitAnd` | `fn bitand(self, rhs: StorageType) -> Self` |
//! | `BitOr` | `fn bitor(self, rhs: StorageType) -> Self` |
//! | `BitXor` | `fn bitxor(self, rhs: StorageType) -> Self` |
//! | `Not` | `fn not(self) -> Self` |
//! | `BitAndAssign` | `fn bitand_assign(&mut self, rhs: StorageType)` |
//! | `BitOrAssign` | `fn bitor_assign(&mut self, rhs: StorageType)` |
//! | `BitXorAssign` | `fn bitxor_assign(&mut self, rhs: StorageType)` |
pub use bitfield;
pub use BitEnum;
pub use ;
pub use ;
/// Compile-time field metadata emitted by the `#[bitfield]` and
/// `#[derive(BitEnum)]` macros when the `reflection` feature is enabled.
///
/// Every bitfield struct gains an inherent `FIELDS: &'static [FieldInfo]` const
/// and a [`Reflect`] impl; every `#[derive(BitEnum)]` enum gains a [`Reflect`]
/// impl carrying its variant table. This lets tools (debuggers, inspectors)
/// enumerate a value's fields, their bit positions, and their meaning without
/// any hand-written tables.
///
/// Offsets and widths are **physical** (in storage-value coordinates), so a
/// field's value is always `(raw >> offset) & ((1 << width) - 1)` regardless of
/// the struct's `msb0`/`lsb0` ordering.
/// Trait for types that can be used as the backing storage of a bitfield.
///
/// Implemented for `u8`, `u16`, `u32`, `u64`, and `u128`.
/// Trait implemented by every struct produced by the [`bitfield`] macro and
/// every enum annotated with `#[derive(BitEnum)]`.
///
/// Allows bitfield structs and enums to be used as nested field types inside
/// other bitfield structs.
/// Error returned by [`BitField::try_from_raw`] and the `TryFrom` impl generated
/// for `#[derive(BitEnum)]` enums when a raw storage value matches no variant.
///
/// Carries the offending value so callers can report or log it. It is a small
/// `Copy` value type: converting a bad pattern allocates nothing and never
/// unwinds.
// Blanket impls of BitStorage for every supported primitive integer.
impl_bit_storage!;