Files
neon/zenith_utils/tests/bin_ser_test.rs
Eric Seppanen 36c12247b9 add bin_ser module
This module adds two traits that implement bincode-based serialization.
BeSer implements methods for big-endian encoding/decoding.
LeSer implements methods for little-endian encoding/decoding.

Right now, the BeSer and LeSer methods have the same names, meaning you
can't `use` them both at the same time. This is intended to be a safety
mechanism: mixing big-endian and little-endian encoding in the same file
is error-prone. There are ways around this, but the easiest fix is to
put the big-endian code and little-endian code in different files or
submodules.
2021-05-10 16:21:05 -07:00

43 lines
1.1 KiB
Rust

use bytes::{Buf, BytesMut};
use hex_literal::hex;
use serde::{Deserialize, Serialize};
use std::io::Read;
use zenith_utils::bin_ser::LeSer;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct HeaderData {
magic: u16,
info: u16,
tli: u32,
pageaddr: u64,
len: u32,
}
// A manual implementation using BytesMut, just so we can
// verify that we decode the same way.
pub fn decode_header_data(buf: &mut BytesMut) -> HeaderData {
HeaderData {
magic: buf.get_u16_le(),
info: buf.get_u16_le(),
tli: buf.get_u32_le(),
pageaddr: buf.get_u64_le(),
len: buf.get_u32_le(),
}
}
pub fn decode2<R: Read>(reader: &mut R) -> HeaderData {
HeaderData::des_from(reader).unwrap()
}
#[test]
fn test1() {
let raw1 = hex!("8940 7890 5534 7890 1289 5379 8378 7893 4207 8923 4712 3218");
let mut buf1 = BytesMut::from(&raw1[..]);
let mut buf2 = &raw1[..];
let dec1 = decode_header_data(&mut buf1);
let dec2 = decode2(&mut buf2);
assert_eq!(dec1, dec2);
eprintln!("{} {}", buf1.len(), buf2.len());
assert_eq!(buf1, buf2);
}