Skip to content

Commit

Permalink
🚀 Release 0.6 and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
wrenger committed Feb 16, 2024
1 parent 0fee566 commit 1f8620e
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
bitfield-struct = "0.5"
bitfield-struct = "0.6"
```

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

/// A custom enum
#[derive(Debug, PartialEq, Eq)]
#[repr(u64)]
#[repr(u16)]
enum CustomEnum {
A = 0,
B = 1,
C = 2,
}
impl CustomEnum {
// This has to be a const fn
const fn into_bits(self) -> u64 {
const fn into_bits(self) -> u16 {
self as _
}
const fn from_bits(value: u64) -> Self {
const fn from_bits(value: u16) -> Self {
match value {
0 => Self::A,
1 => Self::B,
@@ -215,30 +215,42 @@ use bitfield_struct::bitfield;
#[derive(PartialEq, Eq)]
struct Bits {
/// Supports any convertible type
#[bits(16, default = CustomEnum::B, from = CustomEnum::my_from_bits)]
#[bits(8, default = CustomEnum::B, from = CustomEnum::my_from_bits)]
custom: CustomEnum,
/// And nested bitfields
#[bits(8)]
nested: Nested,
}

#[derive(Debug, PartialEq, Eq)]
#[repr(u16)]
#[repr(u8)]
enum CustomEnum {
A = 0,
B = 1,
C = 2,
}
impl CustomEnum {
// This has to be a const fn
const fn into_bits(self) -> u16 {
const fn into_bits(self) -> u8 {
self as _
}
const fn my_from_bits(value: u16) -> Self {
const fn my_from_bits(value: u8) -> Self {
match value {
0 => Self::A,
1 => Self::B,
_ => Self::C,
}
}
}

/// Bitfields implement the conversion functions automatically
#[bitfield(u8)]
struct Nested {
#[bits(4)]
lo: u8,
#[bits(4)]
hi: u8,
}
```

## Bit Order

0 comments on commit 1f8620e

Please sign in to comment.