2022-06-16 18:34:04 +02:00
|
|
|
use postcard::{from_bytes, to_stdvec};
|
2022-06-14 01:25:18 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-06-15 00:58:58 +02:00
|
|
|
use zerocopy::byteorder::{I32, U16};
|
|
|
|
use zerocopy::{AsBytes, FromBytes, NetworkEndian, Unaligned};
|
2022-06-13 23:22:15 +02:00
|
|
|
|
2022-06-14 19:16:38 +02:00
|
|
|
#[derive(AsBytes, FromBytes, Unaligned, Debug, Eq, PartialEq)]
|
|
|
|
#[repr(C, packed)]
|
|
|
|
struct ZeroCopyTest {
|
|
|
|
some_bool: u8,
|
|
|
|
some_u16: U16<NetworkEndian>,
|
|
|
|
some_i32: I32<NetworkEndian>,
|
2022-06-15 00:58:58 +02:00
|
|
|
some_float: [u8; 4],
|
2022-06-14 19:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
|
|
|
struct PostcardTest {
|
|
|
|
some_bool: u8,
|
|
|
|
some_u16: u16,
|
|
|
|
some_i32: i32,
|
2022-06-15 00:58:58 +02:00
|
|
|
some_float: f32,
|
2022-06-13 23:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 18:34:04 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
|
|
|
struct SliceSerTest<'slice> {
|
|
|
|
some_u8: u8,
|
|
|
|
some_u32: u32,
|
|
|
|
some_slice: &'slice [u8],
|
|
|
|
}
|
|
|
|
|
2022-05-26 19:19:16 +02:00
|
|
|
fn main() {
|
2022-06-14 19:16:38 +02:00
|
|
|
let pc_test = PostcardTest {
|
|
|
|
some_bool: true as u8,
|
|
|
|
some_u16: 0x42,
|
|
|
|
some_i32: -200,
|
2022-06-15 00:58:58 +02:00
|
|
|
some_float: 7.7_f32,
|
2022-06-14 19:16:38 +02:00
|
|
|
};
|
2022-06-13 23:22:15 +02:00
|
|
|
|
2022-06-14 19:16:38 +02:00
|
|
|
let out = to_stdvec(&pc_test).unwrap();
|
|
|
|
println!("{:#04x?}", out);
|
2022-06-12 01:49:06 +02:00
|
|
|
|
2022-06-14 19:16:38 +02:00
|
|
|
let sample_hk = ZeroCopyTest {
|
|
|
|
some_bool: true as u8,
|
|
|
|
some_u16: U16::from(0x42),
|
|
|
|
some_i32: I32::from(-200),
|
2022-06-18 15:03:26 +02:00
|
|
|
some_float: 7.7_f32.to_be_bytes(),
|
2022-06-14 19:16:38 +02:00
|
|
|
};
|
|
|
|
let mut slice = [0; 11];
|
|
|
|
sample_hk.write_to(slice.as_mut_slice());
|
|
|
|
println!("{:#04x?}", slice);
|
2022-06-16 18:34:04 +02:00
|
|
|
|
|
|
|
let ser_vec;
|
|
|
|
{
|
|
|
|
let test_buf = [0, 1, 2, 3];
|
|
|
|
let test_with_slice = SliceSerTest {
|
|
|
|
some_u8: 12,
|
|
|
|
some_u32: 1,
|
|
|
|
some_slice: test_buf.as_slice(),
|
|
|
|
};
|
|
|
|
ser_vec = to_stdvec(&test_with_slice).unwrap();
|
|
|
|
println!("{:#04x?}", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let test_deser: SliceSerTest = from_bytes(ser_vec.as_slice()).unwrap();
|
|
|
|
println!("{:?}", test_deser);
|
|
|
|
}
|
2022-05-26 19:19:16 +02:00
|
|
|
}
|