BitRead

Trait BitRead 

Source
pub trait BitRead<'a, E: Endianness>: Sized {
    // Required method
    fn read(stream: &mut BitReadStream<'a, E>) -> Result<Self>;

    // Provided methods
    fn skip(stream: &mut BitReadStream<'a, E>) -> Result<()> { ... }
    fn bit_size() -> Option<usize> { ... }
}
Expand description

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

The BitRead 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 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 size_bits attribute

§Examples

#[derive(BitRead)]
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_bits = 2] // first read 2 bits as unsigned integer, then use the resulting value as size for the read
    dynamic_length: 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 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

#[derive(BitRead)]
#[discriminant_bits = 2]
enum TestBareEnum {
    Foo,
    Bar,
    Asd = 3, // manually set the discriminant value for a field
}
#[derive(BitRead)]
#[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 read(stream: &mut BitReadStream<'a, E>) -> Result<Self>

Read the type from stream

Provided Methods§

Source

fn skip(stream: &mut BitReadStream<'a, E>) -> Result<()>

Skip the type

This might be faster than reading it if the size is known beforehand

Source

fn bit_size() -> Option<usize>

The number of bits that will be read or None if the number of bits will change depending on the bit stream

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl BitRead<'_, BigEndian> for Option<NonZeroU8>

Source§

fn read(stream: &mut BitReadStream<'_, BigEndian>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl BitRead<'_, BigEndian> for Option<NonZeroU16>

Source§

fn read(stream: &mut BitReadStream<'_, BigEndian>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl BitRead<'_, BigEndian> for Option<NonZeroU32>

Source§

fn read(stream: &mut BitReadStream<'_, BigEndian>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl BitRead<'_, BigEndian> for Option<NonZeroU64>

Source§

fn read(stream: &mut BitReadStream<'_, BigEndian>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl BitRead<'_, BigEndian> for Option<NonZeroU128>

Source§

fn read(stream: &mut BitReadStream<'_, BigEndian>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl BitRead<'_, LittleEndian> for Option<NonZeroU8>

Source§

impl BitRead<'_, LittleEndian> for Option<NonZeroU16>

Source§

impl BitRead<'_, LittleEndian> for Option<NonZeroU32>

Source§

impl BitRead<'_, LittleEndian> for Option<NonZeroU64>

Source§

impl BitRead<'_, LittleEndian> for Option<NonZeroU128>

Source§

impl<'a, E: Endianness> BitRead<'a, E> for Cow<'a, str>

Source§

fn read(stream: &mut BitReadStream<'a, E>) -> Result<Cow<'a, str>>

Source§

impl<'a, E: Endianness, T1: BitRead<'a, E>, T2: BitRead<'a, E>> BitRead<'a, E> for (T1, T2)

Source§

fn read(stream: &mut BitReadStream<'a, E>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl<'a, E: Endianness, T1: BitRead<'a, E>, T2: BitRead<'a, E>, T3: BitRead<'a, E>> BitRead<'a, E> for (T1, T2, T3)

Source§

fn read(stream: &mut BitReadStream<'a, E>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl<'a, E: Endianness, T1: BitRead<'a, E>, T2: BitRead<'a, E>, T3: BitRead<'a, E>, T4: BitRead<'a, E>> BitRead<'a, E> for (T1, T2, T3, T4)

Source§

fn read(stream: &mut BitReadStream<'a, E>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl<'a, E: Endianness, T: BitRead<'a, E>> BitRead<'a, E> for Option<T>

Read a boolean, if true, read T, else return None

Source§

fn read(stream: &mut BitReadStream<'a, E>) -> Result<Self>

Source§

impl<'a, E: Endianness, T: BitRead<'a, E>> BitRead<'a, E> for Box<T>

Source§

fn read(stream: &mut BitReadStream<'a, E>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl<'a, E: Endianness, T: BitRead<'a, E>> BitRead<'a, E> for Rc<T>

Source§

fn read(stream: &mut BitReadStream<'a, E>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl<'a, E: Endianness, T: BitRead<'a, E>> BitRead<'a, E> for Arc<T>

Source§

fn read(stream: &mut BitReadStream<'a, E>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl<'a, E: Endianness, T: BitRead<'a, E>, const N: usize> BitRead<'a, E> for [T; N]

Source§

fn read(stream: &mut BitReadStream<'a, E>) -> Result<Self>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for bool

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<bool>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for f32

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<f32>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for f64

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<f64>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for i8

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<i8>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for i16

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<i16>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for i32

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<i32>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for i64

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<i64>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for i128

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<i128>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for u8

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<u8>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for u16

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<u16>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for u32

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<u32>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for u64

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<u64>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for u128

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<u128>

Source§

fn bit_size() -> Option<usize>

Source§

impl<E: Endianness> BitRead<'_, E> for String

Source§

fn read(stream: &mut BitReadStream<'_, E>) -> Result<String>

Implementors§

Source§

impl<'a, T: BitRead<'a, E>, E: Endianness> BitRead<'a, E> for LazyBitRead<'a, T, E>