Automatically generate BitRead
and BitReadSized
implementations for structs and enums
Structs
The implementation can be derived for a struct as long as every field in the struct implements BitRead
or BitReadSized
The struct is read field by field in the order they are defined in, if the size for a field is set stream.read_sized()
will be used, otherwise stream_read()
will be used.
The size for a field can be set using 3 different methods
- set the size as an integer using the
size
attribute, - use a previously defined field as the size using the
size
attribute - read a set number of bits as an integer, using the resulting value as size using the
read_bits
attribute
When deriving BitReadSized
the input size can be used in the size attribute as the input_size
field.
Examples
use BitRead;
use BitReadSized;
Enums
The implementation can be derived for an enum as long as every variant of the enum either has no field, or an unnamed field that implements BitRead
or BitReadSized
The enum is read by first reading a set number of bits as the discriminant of the enum, then the variant for the read discriminant is read.
For details about setting the input size for fields implementing BitReadSized
see the block about size in the Structs
section above.
The discriminant for the variants defaults to incrementing by one for every field, starting with 0
.
You can overwrite the discriminant for a field, which will also change the discriminant for every following field.
Examples
# use BitRead;
#
# use BitRead;
#
# use BitReadSized;
#
Endianness
If the struct that BitRead
or BitReadSized
is derived for requires a Endianness type parameter, you need to tell the derive macro the name of the type parameter used
# use ;
#
This is also required if you specify which endianness the struct has
# use ;
#