pack_bools: an easy way to pack all bools in your struct
pack_bools transforms structs with boolean fields into a struct containing an integer with bit flags for each boolean
value:
use pack_bools;
gets transformed into something like this:
Usage
Simply run cargo add pack_bools in your project directory, use pack_bools::pack_bools; and add the #[pack_bools]
macro on top of your struct. By default, this will behave as the example above: it will replace all fields of type
bool with a single numeric field packed_bools and add getters and setters for each field. By default, both the
getter and setter will inherit their visibility from the field, so if the field is declared pub(super), the getters
and setters will too.
There are lots of customization available, by adding options to the #[pack_bools(...)] attribute. Additionally, every
boolean field can have a #[pack_bools(...)] attribute with additional options.
Global options available when using #[pack_bools(...)] on a struct:
#[pack_bools(getters = [vis] <name>)]changes the name (and possibly visibility) of the getters. Use%as a substitution for the field name. Using#[pack_bools(getters = pub get_field_%)]will make all getters public namedget_field_<field-name>. The visibility modifier is optional. Aliased asget/getter. For setters, use#[pack_bools(set/setter/setters = ...)]instead.#[pack_bools(no_getters)]will not generate getters (aliased asno_get/no_getter), similarly#[pack_bools(no_set/no_setter/no_setters)]will not generate setters.#[pack_bools(type = u16)]will useu16as the data type for the bit flags. Available options areu8/u16/u32/u64/u128/auto, whereauto(the default option) automatically use the smallest of those types that can fit all the bools in the struct.#[pack_bools(field = <name>)]will set the name of the field containing the bitflags, by defaultpacked_bools.#[pack_bools(inline)]will use the inline pattern for the bitflag field, i.e. create fields of the patternpacked_bools: u8. This is the default option. Compare tonewtypebelow.#[pack_bools(newtype [= name])]will make a new single-valued tuple struct for holding the bitflags, similar tostruct MyStructPackedBools(u8);. If a name is specified, the newtype struct will be defined with that name, otherwisePackedBoolswill be suffixed to the name of the struct. The example at the top of this document, with#[pack_bools(newtype)]will be compiled into:
;
You may add the #[pack_bools(...)] attribute on fields of type bool to configure the output of that specific field.
Available options are:
#[pack_bools(skip)]excludes that field from being packed with the other bools.#[pack_bools(getter = [vis] <name>)changes the name (and possibly visibility) of the getter to that field.#[pack_bools(getter = pub debug_mode)]added to a fielddebug: boolwill create a getter likepub fn debug_mode(&self) -> bool { ... }. Aliased asget. For setters, use#[pack_bools(set/setter = [vis] <name>)].#[pack_bools(no_getter)]skips generating a getter for that field. Aliased asno_get. For setters, use#[pack_bools(no_set/no_setter)].#[pack_bools(default = <true/false>)]sets the default value for the field. If set totrue, thenewtypepattern must be used, but then aimpl Defaultwill be generated for that newtype with this field set totrue. If all other fields of the struct has appropriate default values, this will allow you to use#[derive(Default)]on the struct, while having some boolean values set totrue. Defaults tofalse.