Releases: wrenger/bitfield-struct-rs
0.5.4
0.5.3
In this release, we have a new option for the bit order.
The fields can now be ordered beginning with the most significant bits first, as shown below.
# use bitfield_struct::bitfield;
#[bitfield(u8, order = Msb)]
struct MyMsbByte {
/// The first field occupies the most significant bits
#[bits(4)]
kind: usize,
system: bool,
#[bits(2)]
level: usize,
present: bool
}
let my_byte_msb = MyMsbByte::new()
.with_kind(10)
.with_system(false)
.with_level(2)
.with_present(true);
// .- kind
// | .- system
// | | .- level
// | | | .- present
assert!(my_byte_msb.0 == 0b1010_0_10_1);
Thanks to @pwfff for implementing this feature.
0.5.2
Add the option to opt out from implementing the Default
trait similar to the fmt::Debug
trait.
#[bitfield(u64, debug = false, default = false)]
struct CustomDebug {
data: u64
}
impl fmt::Debug for CustomDebug {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{:x}", self.data())
}
}
impl Default for CustomDebug {
fn default() -> Self {
Self(123) // note: you can also use `#[bits(64, default = 123)]`
}
}
let val = CustomDebug::default();
println!("{val:?}")
0.5.1
This release redesigned the support for custom types.
They are now also const
(compile time evaluated) but require conversion functions (from_bits
/into_bits
; this can be changed in the attribute parameters). Beware, this breaks compatibility with the previous From/Into based implementation.
Similarly, the fields can now specify custom defaults in the bits attribute like #[bits(7, default = 42)]
. This also works for padding.
Both features are shown below:
#[bitfield(u16)]
#[derive(PartialEq, Eq)]
struct MyBitfield {
/// Interpreted as 1-bit flag, with custom default
#[bits(default = true)]
flag: bool,
/// Supports any type, with default/to/from expressions (that are const eval)
/// - into/from call #ty::into_bits/#ty::from_bits if nothing else is specified
#[bits(13, default = CustomEnum::B, from = CustomEnum::my_from_bits)]
custom: CustomEnum,
// Padding with default
#[bits(2, default = 0b10)]
__: (),
}
/// A custom enum
#[derive(Debug, PartialEq, Eq)]
#[repr(u16)]
enum CustomEnum {
A, B, C,
}
impl CustomEnum {
// This has to be const eval
const fn into_bits(self) -> u16 {
self as _
}
const fn my_from_bits(value: u16) -> Self {
match value {
0 => Self::A,
1 => Self::B,
_ => Self::C,
}
}
}
// New sets defaults
let val = MyBitfield::new();
assert_eq!(val.flag(), true);
assert_eq!(val.custom(), CustomEnum::B);
assert_eq!(val.0 >> 14, 0b10); // padding
- 0.5.1 fixes an oversight in the Default implementation
0.4.4
0.4.2
0.4.1
0.4.0
This release has two main changes:
Firstly syn was updated to 2.0, and secondly, usize
and isize
fields now work correctly when cross-compiling.
However, the bit sizes must now be explicitly specified for fields with these types.
Previously there was a problem when the target architecture that used a different byte size for these types (e.g., when compiling for 32-bit i686 on an x86_64 host). The proc-macro incorrectly used the size of the host architecture (which executed the macro) instead of the target's type sizes.