oxidecomputer / amd-apcb

AMD Generic Encapsulated Software Architecture Platform Security Processor Configuration Block manipulation library
Mozilla Public License 2.0
13 stars 1 forks source link

ondisk: tokens: Make MemPmuBistTestSelect a bitfield. #54

Closed daym closed 2 years ago

daym commented 2 years ago

Fixes https://github.com/oxidecomputer/amd-apcb/issues/53.

daym commented 2 years ago

I have a love-hate relationship with syntax-rules macros in general and Rust macros in particular. I try to avoid them (in favor of regular functions) whenever I can. Here, they cannot reasonably be avoided.

First, there's some high-level overview in RFD 0199 (section "Library design").

Edit: More detail in block comment in https://github.com/oxidecomputer/amd-apcb/blob/main/src/lib.rs now. (which is exactly what this comment here said before)

daym commented 2 years ago

Thanks for the (separate PR) with comments on this. I think I followed it well enough to approve this; the magic is in the bitfield stuff with respect to bool it seems.

Yes, modular_bitfield, which we are using for bitfields, has a procedural macro (handling #[bitfield]) automatically treating bool as B1. It also automatically implements the builder pattern.

https://docs.rs/modular-bitfield/latest/modular_bitfield/ says

Any type that implements the Specifier trait can be used as a bitfield field. Besides the already mentioned B1, .. B128 also the bool, u8, u16, u32, u64 or u128 primitive types can be used from prelude.

(If modular_bitfield was a little bit more careful in handling errors it would have been a real contender for replacing all the regular structs in amd-apcb. As it is, they only have a well-monitored sideshow here :) )