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

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

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

The meaning of the set sized depends on the type being read (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 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 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"
  • read a set number of bits as an integer, using the resulting value as size using the size_bits attribute

§Examples

#[derive(BitReadSized, 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 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(BitReadSized)]
#[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§

source

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

Read the type from stream

Provided Methods§

source

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

Skip the type

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

source

fn bit_size_sized(_size: usize) -> Option<usize>

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

impl<'a, E: Endianness> BitReadSized<'a, E> for Cow<'a, [u8]>

source§

fn read(stream: &mut BitReadStream<'a, E>, size: usize) -> Result<Cow<'a, [u8]>>

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

impl<'a, E: Endianness, K: BitRead<'a, E> + Eq + Hash, T: BitRead<'a, E>> BitReadSized<'a, E> for HashMap<K, T>

Read K and T size times and return as HashMap<K, T>

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

Read T size times and return as Vec<T>

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

source§

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

source§

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

source§

fn bit_size_sized(size: usize) -> Option<usize>

Implementors§

source§

impl<'a, E: Endianness> BitReadSized<'a, E> for BitReadStream<'a, E>

source§

impl<'a, T: BitReadSized<'a, E>, E: Endianness> BitReadSized<'a, E> for LazyBitReadSized<'a, T, E>