[−][src]Macro byte_struct::bitfields
Generates a structure that implements ByteStructUnspecifiedByteOrder
with bit field semantics.
The bit fields are packed to / unpacked from the base integer type,
which is then packed / unpacked using the primitive type's ByteStructUnspecifiedByteOrder
implementation.
Therefore, the byte order of bit fields is unspecified internally, and is only specified
by the parent structure that derives ByteStruct
, just like all primitive
types.
Note that the memory representation of the generated structure during runtime is NOT in bit field layout. This macro only provides conversion method between the plain structure and the bit-field-packed bytes.
Example
bitfields!( // Specifies the struct name and the base type. // The base type must be one of unsigned integer types. // Attributes and visibility specifier can be attached before the struct name. #[derive(PartialEq, Debug)] SampleBitField: u16 { // Specifies members and bit length from the least significant bit to the most. // The bit layout is assumed packed, and paddings must be explicitly specified. // The sum of bit length of all fields must equal the bit length of the base type. // Attributes and visibility specifier can be attached before the field name. // This creates bit field structure in the following layout: // // | MSB LSB | // | 15| 14| 13| 12| 11| 10| 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | // | z |pad| y | x | // pub x: 4, pub y: 8, padding: 1, pub z: 3, } ); // The macro above generates the structure below. #[derive(PartialEq, Debug)] struct SampleBitField { pub x: u16, pub y: u16, padding: u16, pub z: u16, } impl ByteStructUnspecifiedByteOrder for SampleBitField { ... }