pub trait BitReadSized<E: Endianness>: Sized {
// Required method
fn read(stream: &mut BitStream<E>, size: usize) -> Result<Self>;
// Provided methods
fn skip(stream: &mut BitStream<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
sizeattribute, - use a previously defined field as the size using the
sizeattribute - based on the input size by setting
sizeattribute to"input_size" - read a set number of bits as an integer, using the resulting value as size using the
size_bitsattribute
§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§
Provided Methods§
Sourcefn skip(stream: &mut BitStream<E>, size: usize) -> Result<()>
fn skip(stream: &mut BitStream<E>, size: usize) -> Result<()>
Skip the type
This might be faster than reading it if the size is known beforehand
Sourcefn bit_size_sized(_size: usize) -> Option<usize>
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
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<E: Endianness> BitReadSized<E> for i8
impl<E: Endianness> BitReadSized<E> for i8
Source§impl<E: Endianness> BitReadSized<E> for i16
impl<E: Endianness> BitReadSized<E> for i16
Source§impl<E: Endianness> BitReadSized<E> for i32
impl<E: Endianness> BitReadSized<E> for i32
Source§impl<E: Endianness> BitReadSized<E> for i64
impl<E: Endianness> BitReadSized<E> for i64
Source§impl<E: Endianness> BitReadSized<E> for i128
impl<E: Endianness> BitReadSized<E> for i128
Source§impl<E: Endianness> BitReadSized<E> for u8
impl<E: Endianness> BitReadSized<E> for u8
Source§impl<E: Endianness> BitReadSized<E> for u16
impl<E: Endianness> BitReadSized<E> for u16
Source§impl<E: Endianness> BitReadSized<E> for u32
impl<E: Endianness> BitReadSized<E> for u32
Source§impl<E: Endianness> BitReadSized<E> for u64
impl<E: Endianness> BitReadSized<E> for u64
Source§impl<E: Endianness> BitReadSized<E> for u128
impl<E: Endianness> BitReadSized<E> for u128
Source§impl<E: Endianness> BitReadSized<E> for String
impl<E: Endianness> BitReadSized<E> for String
Source§impl<E: Endianness, K: BitRead<E> + Eq + Hash, T: BitRead<E>> BitReadSized<E> for HashMap<K, T>
Read K and T size times and return as HashMap<K, T>
impl<E: Endianness, K: BitRead<E> + Eq + Hash, T: BitRead<E>> BitReadSized<E> for HashMap<K, T>
Read K and T size times and return as HashMap<K, T>
Source§impl<E: Endianness, T: BitRead<E>> BitReadSized<E> for Vec<T>
Read T size times and return as Vec<T>
impl<E: Endianness, T: BitRead<E>> BitReadSized<E> for Vec<T>
Read T size times and return as Vec<T>