1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-04 00:54:07 +02:00

add derive macro for Read

This commit is contained in:
Robin Appelman 2019-02-27 23:53:17 +01:00
commit 8a19225d61
8 changed files with 251 additions and 5 deletions

View file

@ -0,0 +1,57 @@
use bitstream_reader::{BitBuffer, BitStream, LittleEndian};
use bitstream_reader_derive::Read;
#[derive(Read, PartialEq, Debug)]
struct TestStruct {
foo: u8,
str: String,
#[size = 2]
truncated: String,
bar: u16,
float: f32,
#[size = 3]
asd: u8,
#[size_bits = 2]
dynamic: u8,
#[size = "asd"]
previous_field: u8,
}
#[test]
fn test_read_struct() {
let float: [u8; 4] = 12.5f32.to_bits().to_le_bytes();
let bytes = vec![
12,
'h' as u8,
'e' as u8,
'l' as u8,
'l' as u8,
'o' as u8,
0,
'f' as u8,
'o' as u8,
'o' as u8,
0,
float[0],
float[1],
float[2],
float[3],
0b0101_0101,
0b1010_1010,
];
let buffer = BitBuffer::new(bytes, LittleEndian);
let mut stream = BitStream::from(buffer);
assert_eq!(
TestStruct {
foo: 12,
str: "hello".to_owned(),
truncated: "fo".to_owned(),
bar: 'o' as u16,
float: 12.5,
asd: 0b101,
dynamic: 0b10,
previous_field: 0b1010_0,
},
stream.read().unwrap()
);
}