Trait bitbuffer::BitReadSized
source · 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§
sourcefn read(stream: &mut BitReadStream<'a, E>, size: usize) -> Result<Self>
fn read(stream: &mut BitReadStream<'a, E>, size: usize) -> Result<Self>
Read the type from stream
Provided Methods§
sourcefn skip(stream: &mut BitReadStream<'a, E>, size: usize) -> Result<()>
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
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
Object Safety§
Implementations on Foreign Types§
source§impl<'a, E: Endianness> BitReadSized<'a, E> for Cow<'a, str>
impl<'a, E: Endianness> BitReadSized<'a, E> for Cow<'a, str>
source§impl<'a, E: Endianness> BitReadSized<'a, E> for Cow<'a, [u8]>
impl<'a, E: Endianness> BitReadSized<'a, E> for Cow<'a, [u8]>
source§impl<'a, E: Endianness, K: BitRead<'a, E> + Eq + Hash, T: BitRead<'a, E>> BitReadSized<'a, E> for HashMap<K, T>
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§impl<'a, E: Endianness, T: BitRead<'a, E>> BitReadSized<'a, E> for Vec<T>
impl<'a, E: Endianness, T: BitRead<'a, E>> BitReadSized<'a, E> for Vec<T>
Read T
size
times and return as Vec<T>