[−][src]Trait bitstream_reader::BitRead
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
Loading content...Implementations on Foreign Types
impl<E: Endianness> BitRead<E> for u8
[src]
impl<E: Endianness> BitRead<E> for u16
[src]
impl<E: Endianness> BitRead<E> for u32
[src]
impl<E: Endianness> BitRead<E> for u64
[src]
impl<E: Endianness> BitRead<E> for u128
[src]
impl<E: Endianness> BitRead<E> for i8
[src]
impl<E: Endianness> BitRead<E> for i16
[src]
impl<E: Endianness> BitRead<E> for i32
[src]
impl<E: Endianness> BitRead<E> for i64
[src]
impl<E: Endianness> BitRead<E> for i128
[src]
impl BitRead<LittleEndian> for Option<NonZeroU8>
[src]
fn read(stream: &mut BitStream<LittleEndian>) -> Result<Self>
[src]
impl BitRead<BigEndian> for Option<NonZeroU8>
[src]
impl BitRead<LittleEndian> for Option<NonZeroU16>
[src]
fn read(stream: &mut BitStream<LittleEndian>) -> Result<Self>
[src]
impl BitRead<BigEndian> for Option<NonZeroU16>
[src]
impl BitRead<LittleEndian> for Option<NonZeroU32>
[src]
fn read(stream: &mut BitStream<LittleEndian>) -> Result<Self>
[src]
impl BitRead<BigEndian> for Option<NonZeroU32>
[src]
impl BitRead<LittleEndian> for Option<NonZeroU64>
[src]
fn read(stream: &mut BitStream<LittleEndian>) -> Result<Self>
[src]
impl BitRead<BigEndian> for Option<NonZeroU64>
[src]
impl BitRead<LittleEndian> for Option<NonZeroU128>
[src]
fn read(stream: &mut BitStream<LittleEndian>) -> Result<Self>
[src]
impl BitRead<BigEndian> for Option<NonZeroU128>
[src]
impl<E: Endianness> BitRead<E> for f32
[src]
impl<E: Endianness> BitRead<E> for f64
[src]
impl<E: Endianness> BitRead<E> for bool
[src]
impl<E: Endianness> BitRead<E> for String
[src]
impl<E: Endianness, T: BitRead<E>> BitRead<E> for Rc<T>
[src]
impl<E: Endianness, T: BitRead<E>> BitRead<E> for Arc<T>
[src]
impl<E: Endianness, T: BitRead<E>> BitRead<E> for Option<T>
[src]
Read a boolean, if true, read T
, else return None