@@ -20,7 +20,7 @@ Add this to your `Cargo.toml`:
20
20
21
21
``` toml
22
22
[dependencies ]
23
- bitfield-struct = " 0.5 "
23
+ bitfield-struct = " 0.6 "
24
24
```
25
25
26
26
## Basics
@@ -120,18 +120,18 @@ struct MyBitfield {
120
120
121
121
/// A custom enum
122
122
#[derive(Debug , PartialEq , Eq )]
123
- #[repr(u64 )]
123
+ #[repr(u16 )]
124
124
enum CustomEnum {
125
125
A = 0 ,
126
126
B = 1 ,
127
127
C = 2 ,
128
128
}
129
129
impl CustomEnum {
130
130
// This has to be a const fn
131
- const fn into_bits (self ) -> u64 {
131
+ const fn into_bits (self ) -> u16 {
132
132
self as _
133
133
}
134
- const fn from_bits (value : u64 ) -> Self {
134
+ const fn from_bits (value : u16 ) -> Self {
135
135
match value {
136
136
0 => Self :: A ,
137
137
1 => Self :: B ,
@@ -215,30 +215,42 @@ use bitfield_struct::bitfield;
215
215
#[derive(PartialEq , Eq )]
216
216
struct Bits {
217
217
/// Supports any convertible type
218
- #[bits(16 , default = CustomEnum :: B , from = CustomEnum :: my_from_bits)]
218
+ #[bits(8 , default = CustomEnum :: B , from = CustomEnum :: my_from_bits)]
219
219
custom : CustomEnum ,
220
+ /// And nested bitfields
221
+ #[bits(8)]
222
+ nested : Nested ,
220
223
}
221
224
222
225
#[derive(Debug , PartialEq , Eq )]
223
- #[repr(u16 )]
226
+ #[repr(u8 )]
224
227
enum CustomEnum {
225
228
A = 0 ,
226
229
B = 1 ,
227
230
C = 2 ,
228
231
}
229
232
impl CustomEnum {
230
233
// This has to be a const fn
231
- const fn into_bits (self ) -> u16 {
234
+ const fn into_bits (self ) -> u8 {
232
235
self as _
233
236
}
234
- const fn my_from_bits (value : u16 ) -> Self {
237
+ const fn my_from_bits (value : u8 ) -> Self {
235
238
match value {
236
239
0 => Self :: A ,
237
240
1 => Self :: B ,
238
241
_ => Self :: C ,
239
242
}
240
243
}
241
244
}
245
+
246
+ /// Bitfields implement the conversion functions automatically
247
+ #[bitfield(u8 )]
248
+ struct Nested {
249
+ #[bits(4)]
250
+ lo : u8 ,
251
+ #[bits(4)]
252
+ hi : u8 ,
253
+ }
242
254
```
243
255
244
256
## Bit Order
0 commit comments