Trait bitbuffer::BitWriteSized[][src]

pub trait BitWriteSized<E: Endianness> {
    fn write_sized(
        &self,
        stream: &mut BitWriteStream<'_, E>,
        len: usize
    ) -> Result<()>; }
Expand description

Trait for types that can be written to a stream, requiring the size to be configured

The meaning of the set sized depends on the type being written (e.g, number of bits for integers, number of bytes for strings, number of items for Vec’s, etc)

The BitReadSized trait can be used with #[derive] on structs

The implementation can be derived for a struct as long as every field in the struct implements BitWrite or BitWriteSized

The struct is written field by field in the order they are defined in, if the size for a field is set stream.write_sized() will be used, otherwise stream.write() will be used.

The size for a field can be set using 4 different methods

  • set the size as an integer using the size attribute,
  • use a previously defined field as the size using the size attribute
  • based on the input size by setting size attribute to "input_size"

Examples

#[derive(BitWriteSized, PartialEq, Debug)]
struct TestStructSized {
    foo: u8,
    #[size = "input_size"]
    string: String,
    #[size = "input_size"]
    int: u8,
}

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 BitWrite or BitWriteSized

The enum is written by first writing a set number of bits as the discriminant of the enum, then the variant is written.

For details about setting the input size for fields implementing BitWriteSized 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

#[derive(BitWriteSized)]
#[discriminant_bits = 2]
enum TestUnnamedFieldEnum {
    #[size = 5]
    Foo(i8),
    Bar(bool),
    #[discriminant = 3] // since rust only allows setting the discriminant on field-less enums, you can use an attribute instead
    #[size = "input_size"]
    Asd(u8),
}

Required methods

Write the type to stream

Implementations on Foreign Types

Implementors