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
//! The field-value abstraction shared by every bit-packable type.
//!
//! [`Bits`] is the universal trait: a value that occupies a fixed number of bits
//! inside a bitfield. `bool`, the primitive unsigned integers, the
//! [`UInt`](crate::int::UInt) arbitrary-width integers, nested `#[bitfield]`
//! structs, and `#[derive(BitEnum)]` enums all implement it, so they compose as
//! fields. `u128` is the universal carrier — wide enough for any field this
//! crate supports (the maximum width is 128 bits).
/// Byte order of a bitfield's backing integer when it is serialized.
///
/// # Examples
///
/// ```
/// use bnb::ByteOrder;
/// assert_eq!(ByteOrder::default(), ByteOrder::Big); // network order is the default
/// ```
/// Bit packing order within a bitfield: does the first declared field occupy the
/// most-significant or least-significant bits of the backing integer.
///
/// Most network protocols (and the ASCII-art layouts in their RFCs) are
/// most-significant-first, so [`Msb`](BitOrder::Msb) is the crate default.
///
/// # Examples
///
/// ```
/// use bnb::BitOrder;
/// assert_eq!(BitOrder::default(), BitOrder::Msb); // first field in the high bits
/// ```
/// A value that occupies a fixed number of bits within a bitfield.
///
/// The contract: [`into_bits`](Bits::into_bits) yields the value in the low
/// [`BITS`](Bits::BITS) bits of a `u128` (higher bits zero), and
/// [`from_bits`](Bits::from_bits) reconstructs from the low `BITS` bits of its
/// argument (higher bits ignored). Implementations must round-trip:
/// `T::from_bits(x.into_bits()) == x` for every representable `x`.
///
/// `bool`, the primitive unsigned integers, and the [`UInt`](crate::UInt) types
/// implement it out of the box; `#[bitfield]` and `#[derive(BitEnum)]` generate
/// impls so those types nest as fields too.
///
/// # Const dispatch (`#[bitfield]` field types)
///
/// The accessors `#[bitfield]` generates are `const fn`. A `const fn` cannot call
/// trait methods on stable Rust, so the generated code does not go through this
/// trait: `bool` and the primitive unsigned integers are converted inline, and
/// every other field type is called through a pair of inherent `const fn`s with
/// the same contract as `into_bits`/`from_bits`. [`UInt`](crate::UInt) and every
/// `#[bitfield]`/`#[derive(BitEnum)]`/`#[bitflags]` type provides the pair
/// automatically (their `Bits` impls delegate to it, so the two can never
/// disagree). **Implement a hand-written field type with
/// [`impl_bits!`](macro@crate::impl_bits)**, which emits the trait impl and the
/// inherent pair from one definition — never write the pair by hand. The trait
/// alone still suffices everywhere else (the `#[bin]` codec and the bitstream
/// derives). Field types must be named directly — a `type` alias of a primitive
/// is not recognized by the inline conversion.
///
/// ```
/// use bnb::Bits;
///
/// assert_eq!(<u8 as Bits>::BITS, 8);
/// assert_eq!(0xABu8.into_bits(), 0xAB);
/// assert_eq!(u8::from_bits(0x1FF), 0xFF); // from_bits truncates to the width
/// assert!(!bool::from_bits(0b10)); // only the low bit is read
/// ```
/// Implements [`Bits`] for the primitive unsigned integers (full width).
impl_bits_for_primitive!;
/// Implements [`Bits`] for a hand-written field type from one pair of `const fn`
/// conversion bodies — the supported way to make a custom type usable as a
/// `#[bitfield]` field.
///
/// The accessors `#[bitfield]` generates are `const fn`, and a `const fn` cannot
/// call trait methods on stable Rust — so alongside its [`Bits`] impl, a field
/// type needs the same conversions reachable as inherent `const fn`s. This macro
/// emits **both from one definition**: the trait impl delegates to the inherent
/// pair, so the two can never disagree, and the pair's naming stays an
/// implementation detail of the crate.
///
/// The bodies must satisfy the [`Bits`] contract: `into_bits` yields the value in
/// the low `BITS` bits of a `u128`, `from_bits` reconstructs from the low `BITS`
/// bits (higher bits ignored), and the two round-trip.
///
/// ```
/// use bnb::{bitfield, u7};
///
/// #[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// struct Percent(u7);
///
/// bnb::impl_bits! {
/// impl Bits for Percent {
/// const BITS: u32 = 7;
/// const fn into_bits(self) -> u128 { self.0.value() as u128 }
/// const fn from_bits(raw: u128) -> Self { Percent(u7::from_raw(raw as u8)) }
/// }
/// }
///
/// // `Percent` now nests as a `#[bitfield]` field like any built-in `Bits` type.
/// #[bitfield(u8, bits = msb)]
/// #[derive(Clone, Copy)]
/// struct Meter { pct: Percent, on: bool }
///
/// let m = Meter::new().with_pct(Percent(u7::new(42))).with_on(true);
/// assert_eq!(m.pct(), Percent(u7::new(42)));
/// assert!(m.on());
/// ```
) => ;
}
/// The seam every `#[bitfield]` struct implements — the stable interface the
/// `#[bin]` codec builds on, independent of how the fields are accessed.
///
/// A bitfield is a thin wrapper over a single backing unsigned integer; this
/// trait exposes that backing plus the declared layout metadata. The generated
/// type also provides allocation-free byte conversions: inherent `to_bytes`/`from_bytes`
/// (which use the declared [`BYTE_ORDER`](Bitfield::BYTE_ORDER)) plus the
/// endianness-explicit `to_be_bytes`/`to_le_bytes`/`from_be_bytes`/`from_le_bytes`.
///
/// # Examples
///
/// ```
/// use bnb::{bitfield, u4, Bitfield, BitOrder, ByteOrder};
///
/// #[bitfield(u8, bits = msb, bytes = big)]
/// #[derive(Clone, Copy)]
/// struct Byte { hi: u4, lo: u4 }
///
/// let b = Byte::new().with_hi(u4::new(0xA)).with_lo(u4::new(0xB));
/// assert_eq!(b.to_raw(), 0xAB); // the backing integer
/// assert_eq!(Byte::from_raw(0xCD).hi().value(), 0xC);
/// assert_eq!(Byte::WIDTH, 8); // declared layout metadata
/// assert_eq!(Byte::BYTE_ORDER, ByteOrder::Big);
/// assert_eq!(Byte::BIT_ORDER, BitOrder::Msb);
/// ```