Trait BitWrite

Source
pub trait BitWrite<E: Endianness> {
    // Required method
    fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>;
}
Expand description

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

The BitWrite trait can be used with #[derive] on structs and enums

§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 write_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

§Examples

#[derive(BitWrite)]
struct TestStruct {
    foo: u8,
    str: String,
    #[size = 2] // when `size` is set, the attributed will be read using `read_sized`
    truncated: String,
    bar: u16,
    float: f32,
    #[size = 3]
    asd: u8,
    #[size = "asd"] // use a previously defined field as size
    previous_field: 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 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(BitWrite)]
#[discriminant_bits = 2]
enum TestBareEnum {
    Foo,
    Bar,
    Asd = 3, // manually set the discriminant value for a field
}
#[derive(BitWrite)]
#[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
    Asd(u8),
}

Required Methods§

Source

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Write the type to stream

Implementations on Foreign Types§

Source§

impl<E: Endianness> BitWrite<E> for bool

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for f32

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for f64

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for i8

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for i16

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for i32

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for i64

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for i128

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for str

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for u8

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for u16

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for u32

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for u64

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for u128

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness> BitWrite<E> for String

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness, T1: BitWrite<E>, T2: BitWrite<E>> BitWrite<E> for (T1, T2)

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness, T1: BitWrite<E>, T2: BitWrite<E>, T3: BitWrite<E>> BitWrite<E> for (T1, T2, T3)

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness, T1: BitWrite<E>, T2: BitWrite<E>, T3: BitWrite<E>, T4: BitWrite<E>> BitWrite<E> for (T1, T2, T3, T4)

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<E: Endianness, T: BitWrite<E>, const N: usize> BitWrite<E> for [T; N]

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<T: BitWrite<E> + ToOwned + ?Sized, E: Endianness> BitWrite<E> for Cow<'_, T>

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<T: BitWrite<E>, E: Endianness> BitWrite<E> for Option<T>

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<T: BitWrite<E>, E: Endianness> BitWrite<E> for Box<T>

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<T: BitWrite<E>, E: Endianness> BitWrite<E> for Rc<T>

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<T: BitWrite<E>, E: Endianness> BitWrite<E> for Arc<T>

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Source§

impl<T: BitWrite<E>, E: Endianness> BitWrite<E> for Vec<T>

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Implementors§