-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bitfield Integration in Structs #1497
Comments
We don't have any special support for bitfields, nor any plans to add that support. However, as part of the Would that give you the support you need? |
I have a similar request. I think the approach would work at least in theory for everything I've run into. If zercopy could explicitly support bitfields, that'd be nice, but understood that this isn't your focus. I am in fact already using bitfields extensively and it works well for the most part, but have a few edge cases. I'm using the bitfield_struct crate, and deriving Example enum: #[derive(TryFromBytes, Debug)]
#[repr(u8)]
pub enum IntBackedEnum {
FirstVariant = 0,
SecondVariant = 0,
// Some other variants, with gaps across the u8 space...
} Trivial bitfield struct: #[derive(TryFromBytes)]
#[bitfield(u8)]
struct MyBitfield {
// We're bit packing and only care about 6 bits!
#[bits(6)]
enum_value: IntBackedEnum
#[bits(2)]
other_field: u8, // You get the idea...
} Could be that I'm holding it wrong, but I get the following errors in this situation:
|
@ianthetechie I believe you've run into #388. Try putting |
|
Edits by maintainers:
bitflags/bitflags#326 (comment) suggests that there's no way to support zerocopy, but I think we can work around that.
cc @KodrAus @qwandor
IIUC, the problem is that:
zerocopy_0_N
feature for each zerocopy 0.N version#[cfg_attr(feature = "zerocopy_0_N", derive(...)]
feature = "zerocopy_0_N"
isn't set, and so it won't have the intended behaviorHowever, I think we can work around this by instead modifying bitflags so that certain internals of its macros are gated by
#[cfg(feature = "zerocopy_0_N")]
. That way, the code emitted into the caller's crate will simply have#[derive(...)]
attributes which are already correctly chosen.The naive solution requires the caller to also depend on
zerocopy
so that derive-emitted code can refer tozerocopy
. A more ergonomic solution depends on supporting a#[zerocopy(crate = ...)]
attribute (#11) so that only bitflags needs to take a zerocopy dependency (which it then re-exports).Original text by @shilonic:
I'm currently working on a project that requires integrating bitfields into structs while maintaining zero-copy functionality. I believe adding native support for bitfield fields would enhance the usability and flexibility of the zero-copy crate.
I have tried integrating the bitfield crate and modular-bitfield, but using these options requires two levels of validation:
Questions:
What is the best way to integrate bitfields into structs using this crate?
Are there existing patterns or recommendations for handling bitfields efficiently in a zero-copy context?
The text was updated successfully, but these errors were encountered: