NewYour coding agent can read the release notes before it upgrades.Set up the MCP server →
crates.io
crates.io, #2166 most downloaded on crates.io
Struct-like procedural macro for bitfields.
Latest stable
0.13.0
Last release 7 months ago
04 Apr 2026
Release timing varies
gaps range from 2 weeks to 6 months
Most releases are documented
notes for 30 of 40 stable releases
2 versions withdrawn
withdrawn after publishing
5 years old
42 releases · first in 2021
Drag across the ruler to compare, or tap a release to open it.
This release adds support for const generics.
This release adds support for const generics.
Bitfields can now use const generics directly. Just declare them on the struct as usual:
use bitfield_struct::bitfield;
#[bitfield(u16)]
#[derive(PartialEq, Eq)]
struct Packet<const VERSION: u8> {
#[bits(4)]
tag: u8,
#[bits(12)]
payload: u16,
}
Support #[cfg(...)] on bit enum variants
Support #[cfg(...)] on bit enum variants
This release contains a few fixes for #64 and #68, as well as optional support for the binrw traits #65 from @Maccraft123.
This release contains a few fixes for #64 and #68, as well as optional support for the binrw traits #65 from @Maccraft123.
Additionally, it introduces a small new macro for generating the from_bits/into_bits function for enums:
use bitfield_struct::bitenum;
#[bitenum]
#[repr(u8)]
#[derive(Debug, PartialEq, Eq)]
enum MyEnum {
#[fallback]
A = 0,
B = 1,
C = 2,
}
assert_eq!(MyEnum::from_bits(1), MyEnum::B);
assert_eq!(MyEnum::C.into_bits(), 2);
#[bitfield(u8)]
struct Test {
#[bits(8)]
value: MyEnum,
}
Thanks to @MolotovCherry and @xTachyon for reporting these issues.
Your coding agent can read these notes before it upgrades. Set up the MCP server →
Add the option to auto-generate std::Hash as suggested by @freemin7 #62
Add the option to auto-generate std::hash::Hash as suggested by @freemin7 #62
#[bitfield(u32, hash = true)]
struct MyBitfield {
data: u16,
__: u8,
#[bits(8)]
extra: u8,
}
The hash implementation ignores any padding, but requires that all fields also implement Hash (similar to Debug).
Thanks to @freemin7 for suggesting this.
Fix clippy unnecessary cast warning #60. Thanks to @martiege for reporting this.
Fix clippy unnecessary cast warning #60. Thanks to @martiege for reporting this.
This release includes an option to disable automatic Copy and Clone implementation. This might be useful when you want to implement your own Clone.
This release includes an option to disable automatic Copy and Clone implementation. This might be useful when you want to implement your own Clone.
/// We have a custom clone implementation -> opt out
#[bitfield(u64, clone = false)]
struct Full {
data: u64,
}
impl Clone for Full {
fn clone(&self) -> Self {
Self::new().with_data(self.data())
}
}
impl Copy for Full {}
Thanks to @kevinhartman for implementing this :)
From this release on, the set_<field> and set_<field>_checked setters are const. This is possible due to Rust 1.83.0 stabilizing const mut refs. Consequently, the minimum supported Rust version was bumped to 1.83.0. This also opens the door to a more generic bitfield representation (#7).
Fix clippy unnecessary cast warning #60. Thanks to @martiege for reporting this.
Fix clippy unnecessary cast warning #60. Thanks to @martiege for reporting this.
Backport making Copy and Clone optional (#59).
Backport making Copy and Clone optional (#59).
This release is intended to support older rust versions, that lack the const_mut_refs feature.
This release fixes that defaults for padding were not correctly applied on Msb ordering #57.
This release fixes that defaults for padding were not correctly applied on Msb ordering #57.
Thanks to @gregorygaines for finding and fixing this bug.
This release fixes a clippy warning for certain edge cases #53.
This release fixes a clippy warning for certain edge cases #53.
Thanks to @s5suzuki for reporting this.
This release includes some minor fixes, namely #52 (broken defaults for repr) and #51 (documentation for Clone/Copy traits).
This release includes some minor fixes, namely #52 (broken defaults for repr) and #51 (documentation for Clone/Copy traits).
Thanks to @hftsai256 and @endrift for finding reporting this.
This release adds new checked setters, which check for integer overflows and return an error instead of panicing (like the normal setters) #49.
This release adds new checked setters, which check for integer overflows and return an error instead of panicing (like the normal setters) #49.
#[bitfield(u32)]
struct MyBitfield {
#[bits(3)]
number: i32,
#[bits(29)]
__: (),
}
assert!(MyBitfield::new().with_number_checked(4).is_err());
Also, the automatic generation of the new function can now also be disable, similar to debug or default #50.
Lastly, the crate now specifies a minimal supported rust version #44.
Thanks to @EngJay, @ultimaweapon, and @DavidAntliff for reporting these issues.
This release adds support for auto-generating the defmt::Format trait (#42):
This release adds support for auto-generating the defmt::Format trait (#42):
#[bitfield(u64, defmt = true)]
struct DefmtExample {
data: u64
}
defmt::println!("{}", DefmtExample::new());
Thanks to @agrif for implementing this.
Also, the auto-trait implementations (Default, Debug, defmt::Format) now support cfg(...) attributes:
#[bitfield(u64, debug = cfg(test), default = cfg(feature = "foo"))]
struct CustomDebug {
data: u64
}
These cfg attributes are added to the trait implementations, only enabling them if the conditions are met.
This release adds a new way to specify the memory layout of the bitfield via the repr attribute. This makes it possible to specify the endianness for
This release adds a new way to specify the memory layout of the bitfield via the repr attribute. This makes it possible to specify the endianness for the whole bitfield as shown below.
use endian_num::be16;
#[bitfield(u16, repr = be16, from = be16::from_ne, into = be16::to_ne)]
struct MyBeBitfield {
#[bits(4)]
first_nibble: u8,
#[bits(12)]
other: u16,
}
let my_be_bitfield = MyBeBitfield::new()
.with_first_nibble(0x1)
.with_other(0x234);
assert_eq!(my_be_bitfield.into_bits().to_be_bytes(), [0x23, 0x41]);
Here, the Bitfield is represented as an be16 integer in memory. All the conversion is done automatically by calling from and into.
Thanks to @mkroening for implementing and documenting this feature (#39, #41).
In this tiny release, we added documentation to conversion functions to stop the Rust from complaining with #![warn(missing_docs)].
In this tiny release, we added documentation to conversion functions to stop the Rust from complaining with #![warn(missing_docs)].
Thanks to @osteffenrh for fixing this (#37)
This minor release just fixes #35 (raw field names) and #36 (failing overflow tests). Thanks to @osteffenrh and @marcfir for reporting them.
This minor release just fixes #35 (raw field names) and #36 (failing overflow tests). Thanks to @osteffenrh and @marcfir for reporting them.
The first issue reported a bug when using r# raw identifiers, as shown below. This is now supported.
#[bitfield(u8)]
#[derive(PartialEq)]
struct Raw {
r#type: u8,
}
let raw = Raw::new().with_type(0xff);
assert_eq!(raw.r#type(), 0xff);
This release added the auto-generation of the (from|into)_bits conversion functions for bitfields (which can be disabled by [bitfield(u8, conversion =
This release added the auto-generation of the (from|into)_bits conversion functions for bitfields (which can be disabled by [bitfield(u8, conversion = false)]).
Together with the laxer type requirements for the conversion functions, this makes it simple to nest bitfields.
#[bitfield(u8)]
#[derive(PartialEq)]
struct Child {
contents: u8,
}
#[bitfield(u16)]
#[derive(PartialEq)]
struct Parent {
#[bits(8)]
child: Child,
other: u8,
}
let child = Child::new().with_contents(0xff);
let parent = Parent::new().with_child(child);
assert_eq!(child.into_bits(), 0xff);
assert_eq!(parent.into_bits(), 0xff);
Thanks to @Frostie314159 for implementing this feature and to @akauppi and @deeglaze for additional fixes.
This release introduces a new parameter to the #[bits] attribute for making fields read-only or write-only.
This release introduces a new parameter to the #[bits] attribute for making fields read-only or write-only.
#[bitfield(u16)]
struct MyBitfield {
#[bits(8, access = RO)]
read: u8,
#[bits(8, access = WO)]
write: u8,
}
let v = MyBitfield::new().with_write(0xea);
assert_eq!(v.0, 0xea00);
assert_eq!(v.read(), 0);
Thanks to @paul1999 for implementing this feature.
This release fixes two issues (#22 and #24) related to the bounds check for signed integers.
This release fixes two issues (#22 and #24) related to the bounds check for signed integers.
Thanks to @jerrinot and @snakehand for finding and investigating them.
This minor release adds the #[track_caller] attribute to the setter functions, making it easier to figure out where a value overflows (#20).
This minor release adds the #[track_caller] attribute to the setter functions, making it easier to figure out where a value overflows (#20).
Thanks to @rj00a for implementing this feature.
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.
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.
Add the option to opt out from implementing the Default trait similar to the fmt::Debug trait.
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:?}")
This release redesigned the support for custom types. They are now also const (compile time evaluated) but require conversion functions (from_bits/int
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
Nothing published for this version
Suppress new clippy::bad_bit_mask lint.
Suppress new clippy::bad_bit_mask lint.
Nothing published for this version
Fixed negative integer bounds checks.
Fixed negative integer bounds checks.
Fixing regression: dependency to std.
Fixing regression: dependency to std.
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. Howev
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.
Extending and flashing out the documentation. Apart from that, only minor changes have been made to the code generation.
Extending and flashing out the documentation. Apart from that, only minor changes have been made to the code generation.
Nothing published for this version
Support for custom types that are convertible from/into the bitfield's underlying type.
Support for custom types that are convertible from/into the bitfield's underlying type.
There is now a way to opt out from generating Debug implementations using #[bitfield(u64, debug = false)]. Additionally, the macro creates constant me
There is now a way to opt out from generating Debug implementations using #[bitfield(u64, debug = false)].
Additionally, the macro creates constant members with the bit size (<NAME>_BITS) and offset (<NAME>_OFFSET) for each member.
Auto-implement debug traits and support any zero-sized type (e.g. markers).
Auto-implement debug traits and support any zero-sized type (e.g. markers).
Nothing published for this version
Nothing published for this version
Nothing published for this version
Nothing published for this version
Nothing published for this version
Nothing published for this version
Nothing published for this version
Nothing published for this version
One column per quarter.