Skip to main content

bitsize

Attribute Macro bitsize 

Source
#[bitsize]
Expand description

Defines the bitsize of a struct or an enum.

e.g. #[bitsize(4)] represents the item as a u4, which is UInt<u8, 4> underneath. The size of structs is currently limited to 128 bits. The size of enums is limited to 64 bits. Please open an issue if you have a usecase for bigger bitfields.

After the size, structs may take extra options:

  • hide_value: place the generated struct in a private module so the raw value field cannot be read or written. To write the whole value, use From/TryFrom instead, which is more type-safe. Relative visibility (pub(super), pub(self), inherited) is shifted one super so it works like you wrote it.
  • new = <vis>: visibility of new (private by default, like other Rust items). Examples: new = pub, new = pub(crate).

Struct fields may use #[at(n)] or #[at(n..=m)] to place a field at backing-integer bit n (0 = LSB). Width comes from the field type; a range is checked against that width. Fields without #[at] fill sequentially from the end of the previous field. Skipped bits are implicit padding. Overlap or reordering is a compile error.

Enums may use #[discriminant_at(n)] or #[discriminant_at(n..=m)] when the tag sits in the same integer, and #[discriminant(TagType)] when the tag is a separate value. Variants look like Variant(Payload) = tag. Use TryFromBits when not every tag is used.

ⓘ
#[bitsize(8, hide_value, new = pub(crate))]
struct Register { ... }