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
//! 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.
///
/// ```
/// 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!;
/// 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);
/// ```