Skip to content

Commit 1f8620e

Browse files
committed
🚀 Release 0.6 and update readme
1 parent 0fee566 commit 1f8620e

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Add this to your `Cargo.toml`:
2020

2121
```toml
2222
[dependencies]
23-
bitfield-struct = "0.5"
23+
bitfield-struct = "0.6"
2424
```
2525

2626
## Basics
@@ -120,18 +120,18 @@ struct MyBitfield {
120120

121121
/// A custom enum
122122
#[derive(Debug, PartialEq, Eq)]
123-
#[repr(u64)]
123+
#[repr(u16)]
124124
enum CustomEnum {
125125
A = 0,
126126
B = 1,
127127
C = 2,
128128
}
129129
impl CustomEnum {
130130
// This has to be a const fn
131-
const fn into_bits(self) -> u64 {
131+
const fn into_bits(self) -> u16 {
132132
self as _
133133
}
134-
const fn from_bits(value: u64) -> Self {
134+
const fn from_bits(value: u16) -> Self {
135135
match value {
136136
0 => Self::A,
137137
1 => Self::B,
@@ -215,30 +215,42 @@ use bitfield_struct::bitfield;
215215
#[derive(PartialEq, Eq)]
216216
struct Bits {
217217
/// 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)]
219219
custom: CustomEnum,
220+
/// And nested bitfields
221+
#[bits(8)]
222+
nested: Nested,
220223
}
221224

222225
#[derive(Debug, PartialEq, Eq)]
223-
#[repr(u16)]
226+
#[repr(u8)]
224227
enum CustomEnum {
225228
A = 0,
226229
B = 1,
227230
C = 2,
228231
}
229232
impl CustomEnum {
230233
// This has to be a const fn
231-
const fn into_bits(self) -> u16 {
234+
const fn into_bits(self) -> u8 {
232235
self as _
233236
}
234-
const fn my_from_bits(value: u16) -> Self {
237+
const fn my_from_bits(value: u8) -> Self {
235238
match value {
236239
0 => Self::A,
237240
1 => Self::B,
238241
_ => Self::C,
239242
}
240243
}
241244
}
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+
}
242254
```
243255

244256
## Bit Order

0 commit comments

Comments
 (0)