[−][src]Trait bitstream_reader::BitReadSized
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
Loading content...Implementations on Foreign Types
impl<E: Endianness> BitReadSized<E> for u8
[src]
impl<E: Endianness> BitReadSized<E> for u16
[src]
impl<E: Endianness> BitReadSized<E> for u32
[src]
impl<E: Endianness> BitReadSized<E> for u64
[src]
impl<E: Endianness> BitReadSized<E> for u128
[src]
impl<E: Endianness> BitReadSized<E> for i8
[src]
impl<E: Endianness> BitReadSized<E> for i16
[src]
impl<E: Endianness> BitReadSized<E> for i32
[src]
impl<E: Endianness> BitReadSized<E> for i64
[src]
impl<E: Endianness> BitReadSized<E> for i128
[src]
impl<E: Endianness> BitReadSized<E> for String
[src]
impl<E: Endianness, T: BitReadSized<E>> BitReadSized<E> for Option<T>
[src]
impl<E: Endianness, T: BitRead<E>> BitReadSized<E> for Vec<T>
[src]
Read T
size
times and return as Vec<T>
impl<E: Endianness, K: BitRead<E> + Eq + Hash, T: BitRead<E>> BitReadSized<E> for HashMap<K, T>
[src]
Read K
and T
size
times and return as HashMap<K, T>