| Crates.io | le-stream-derive |
| lib.rs | le-stream-derive |
| version | 1.3.2 |
| created_at | 2024-09-18 13:45:22.899384+00 |
| updated_at | 2025-08-01 17:45:55.589764+00 |
| description | Derive macros to de-/serialize objects from/to little endian byte streams |
| homepage | |
| repository | https://github.com/PaulmannLighting/le-stream-derive |
| max_upload_size | |
| id | 1379303 |
| size | 24,171 |
Derive macros for le-stream.
use le_stream::{FromLeStream, ToLeStream};
use le_stream_derive::{FromLeStream, ToLeStream};
const MY_STRUCT_BYTES: [u8; 39] = [
0x42, 0x37, 0x13, 0x12, 0x34, 0x56, 0x78, 0xff, 0xaa, 0xbb, 0xcc, 0xdd, 0x34, 0x12, 0x56, 0x78,
0x9a, 0xbc, 0x34, 0x12, 0x56, 0x78, 0x9a, 0xbc, 0x34, 0x12, 0x56, 0x78, 0x9a, 0xbc, 0x00, 0x42,
0x37, 0x13, 0x12, 0x42, 0x37, 0x13, 0x12,
];
#[derive(Clone, Debug, Eq, PartialEq, FromLeStream, ToLeStream)]
struct MyStruct {
flag: u8,
num: u16,
array: [u8; 4],
tail: u8,
array_u16: [u16; 2],
array_sub_struct: [SubStruct; 3],
is_working: bool,
size: usize,
}
#[test]
fn serialize_struct() {
let my_struct = MyStruct {
flag: 0x42,
num: 0x1337,
array: [0x12, 0x34, 0x56, 0x78],
tail: 0xff,
array_u16: [0xBBAA, 0xDDCC],
array_sub_struct: [
SubStruct {
num: 0x1234,
array: [0x56, 0x78, 0x9A, 0xBC],
},
SubStruct {
num: 0x1234,
array: [0x56, 0x78, 0x9A, 0xBC],
},
SubStruct {
num: 0x1234,
array: [0x56, 0x78, 0x9A, 0xBC],
},
],
is_working: false,
size: 0x1213_3742_1213_3742,
};
assert_eq!(
my_struct.to_le_stream().collect::<Vec<_>>(),
MY_STRUCT_BYTES
);
}
#[test]
fn deserialize_struct_exact() {
let my_struct = MyStruct::from_le_stream_exact(MY_STRUCT_BYTES.into_iter())
.expect("Could not create struct from byte stream.");
assert_eq!(my_struct.flag, 0x42);
assert_eq!(my_struct.num, 0x1337);
assert_eq!(my_struct.array, [0x12, 0x34, 0x56, 0x78]);
assert_eq!(my_struct.tail, 0xff);
assert_eq!(my_struct.array_u16, [0xBBAA, 0xDDCC]);
assert!(!my_struct.is_working);
assert_eq!(my_struct.size, 0x1213_3742_1213_3742);
}
cargo fmtcargo clippy