3 ms·
Disclaimer: I haven’t used bitflags (but I don’t think it applies for my need). The nginx example in the above blog post seems like a bitfield! equivalent woul
by boulos 6y ago
Disclaimer: I haven’t used bitflags (but I don’t think it applies for my need).
The nginx example in the above blog post seems like a bitfield! equivalent would be... unpleasant.
For very small structs that are just only wire formats, I might use bitfield!. But encoding/decoding protocol headers (e.g., a VP8 header [1], which has a 19-bit “size” field) or interop with other systems where you’d like some checking a la serde, bitfield! seems more verbose than “fine, I’ll do it myself right here against the u8”.
That is, even if that nginx example had a two-bit field in there, I’d take everything at the end and round up to a u8. Then read/write some of these “by hand”, rather than pollute the struct with lots and lots of macro “noise”.
[1] https://tools.ietf.org/html/rfc6386#section-9.1 https://tools.ietf.org/html/rfc6386#section-9.1
- steveklabnik 6y agoYes, this is what I mean by "it's not perfect", there's totally things that could be better. All I'm saying is that this works out well enough for our particular needs generally, and isn't a giant pain point, so that's probably why it wasn't mentioned.