Documentation coming soon. Geared towards netcode, so all types are serialized in network-byte-order.
Why not just use serde? Serde has basic types, such as strings, which are variable-length and require some sort of format to serialize. This crate chooses to not force a particular protocol on the user, so if you have a variable-length type, you will need to implement a serialization protocol on it yourself.
Sample usage:
use std::io::{self, Cursor, Read, Write};
use netpack::{PackError, Packable, Unpackable, unpack, unpack_from};
#[derive(Debug, PartialEq)]
struct Sample {
num: i128,
sample: SubSample,
b: bool,
}
impl Packable for Sample {
type Error = io::Error;
fn pack_into(&self, stream: &mut impl Write) -> Result<(), Self::Error> {
self.num.pack_into(stream)?;
self.sample.pack_into(stream)?;
self.b.pack_into(stream)?;
Ok(())
}
}
impl Unpackable for Sample {
type Error = PackError;
fn unpack_from(rdr: &mut impl Read) -> Result<Self, Self::Error> {
Ok(Self {
num: unpack_from(rdr)?,
sample: unpack_from(rdr)?,
b: unpack_from(rdr)?,
})
}
}
#[derive(Debug, PartialEq)]
struct SubSample {
num: i32,
arr: [u16; 5],
float: f32,
}
impl Packable for SubSample {
type Error = io::Error;
fn pack_into(&self, stream: &mut impl Write) -> Result<(), Self::Error> {
self.num.pack_into(stream)?;
self.arr.pack_into(stream)?;
self.float.pack_into(stream)?;
Ok(())
}
}
impl Unpackable for SubSample {
type Error = PackError;
fn unpack_from(rdr: &mut impl Read) -> Result<Self, Self::Error> {
Ok(Self {
num: unpack_from(rdr)?,
arr: unpack_from(rdr)?,
float: unpack_from(rdr)?,
})
}
}
fn main() {
let sub = SubSample {
num: 20,
arr: [10, 100, 1000, 5, 0],
float: 0.3
};
let sample = Sample {
num: -5,
sample: sub,
b: true
};
let mut v = Vec::new();
let mut c = Cursor::new(&mut v);
sample.pack_into(&mut c).unwrap();
assert_eq!(&v, &vec![
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 251,
0, 0, 0, 20,
0, 10, 0, 100, 3, 232, 0, 5, 0, 0,
62, 153, 153, 154,
1
]);
let mut v = sample.pack().unwrap();
assert_eq!(&v, &vec![255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 251, 0, 0, 0, 20, 0, 10, 0, 100, 3, 232, 0, 5, 0, 0, 62, 153, 153, 154, 1]);
v.push(0xff);
let mut c = Cursor::new(&mut v);
let new_sample: Sample = unpack_from(&mut c).unwrap();
assert_eq!(&new_sample, &sample);
let (new_sample, rest): (Sample, &[u8]) = unpack(&v).unwrap();
assert_eq!(&new_sample, &sample);
assert_eq!(&rest, &[0xff]);
}