Struct Bitfields

Has there been any discussion into adding bitfields into Rust structs? C bitfields are tempting to use for packing/unpacking data, but they’re often dangerous to use because the ultimate layout of the struct is up to the compiler. So, for portability, C programmers are forced to use bitwise operations to pack and unpack data, which is unfortunate. It seems like if Rust gave a stronger guarantee of the layout of structs with bitfields, this could be an area where Rust is better than C for low-level programming.

Is there any reason we shouldn’t have bitfields with strong layout guarantees?

:+1:

though I’m not sure if there’s no viable alternative for this now.

Has there been any discussion into adding bitfields into Rust structs?

Yep:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.