I am using bincode
crate(unstable version 2.0.0-rc.3
) to perform binary serialization and deserialization of structs(trying to perform C memcpy
of structs without padding into a buffer). While performing encode_into_slice
in big endian
format, the amount of bytes written into a [u8]
buff is incorrect when compared the total size of the struct.
use bincode::{Decode, Encode};
use serde::{Serialize, Deserialize};
#[derive(Default, Deserialize, Serialize, Decode, Encode, Debug)]
#[repr(C)]
// size of struct is 10
pub struct Data {
pub u: u16,
pub f: f64,
}
fn main() {
let data = Data {
u: 1111u16,
f: 1111.0,
};
let mut buff = [0u8; 16];
if let Ok(processed_len) = bincode::encode_into_slice(&data, &mut buff, bincode::config::standard().with_big_endian()) {
// prints value of 9 for smaller field values and sometimes 11 for larger field values
println!("{}", processed_len);
}
}
Cargo.toml
[dependencies]
bincode = "2.0.0-rc.3"
serde = { version = "1.0.195", features = ["derive"] }