Serdine is a tiny serialization library for de/serializing instances in a binary, serial format, focused on ease of use.
This is convenient for example, when interfacing with data files belonging to C programs, where the files are often the image of the in-memory instances.
This library is currently used by another project of mine (Catacomb II-64k); I'm in the process of updating it, with documentation and new features.
The crate provides de/serialization implementations for the numeric primites and arrays, in little endian format; any field, particularly those of user-defined types, can use a custom de/serialization function. De/serialization is recursive, so any type can be nested.
The de/serialization traits are extremely simple:
```rs
pub trait Serialize {
fn serialize
pub trait Deserialize {
fn deserialize
The following are examples of de/serialization:
```rs use serdine_derive::Deserialize;
pub struct MyNamedFieldsStruct {
pub myi16: i16,
pub myu32: u32,
pub myf32: f32,
pub myf64: f64,
pub myarr: [u16; 2],
pub mybool: bool,
#[deserialize = "deserializevec"]
pub myvec: Vec
fn deserializevec
fn deserializenamedfieldsstruct() { let serializedbytes: &[u8] = &[ 0x80, 0x00, 0xBE, 0xBA, 0xFE, 0xCA, 0xC9, 0x3E, 0x7B, 0x44, 0x0C, 0x07, 0x42, 0xB2, 0x80, 0x19, 0x24, 0x40, 0x00, 0x01, 0x02, 0x03, 0xCA, 0x04, 0x05, 0x06 ];
let instance = MyNamedFieldsStruct::deserialize(serialized_bytes).unwrap();
assert_eq!(0x80, instance.my_i16);
assert_eq!(0xCAFEBABE, instance.my_u32);
assert_eq!(1004.981_f32, instance.my_f32);
assert_eq!(10.04981_f64, instance.my_f64);
assert_eq!([0x0100, 0x0302], instance.my_arr);
assert_eq!(true, instance.my_bool);
assert_eq!(vec![4, 5, 6], instance.my_vec);
} ```
```rs // Enums are supported, as long as they declare their representation.
enum MyEnum { VarA = 0, VarB = 1, VarC = 65534, }
fn serializeenum() { let mut serializedinstance = Vec::new();
MyEnum::VarA.serialize(&mut serialized_instance).unwrap();
MyEnum::VarC.serialize(&mut serialized_instance).unwrap();
MyEnum::VarB.serialize(&mut serialized_instance).unwrap();
let expected_bytes: &[u8] = &[
0x00, 0x00,
0xFE, 0xFF,
0x01, 0x00,
];
assert_eq!(expected_bytes, serialized_instance);
} ```