bzipper
bzipper is a binary (de)serialiser for the Rust language.
In contrast to Serde/Bincode, the primary goal of bzipper is to serialise with a known size constraint. Therefore, this crate may be more suited for networking or other cases where a fixed-sized buffer is needed.
Keep in mind that this project is still work-in-progress.
This crate is compatible with no_std.
Data model
Most primitive types serialise losslessly, with the exception being usize and isize.
These serialise as u32 and i32, respectively, for portability reasons.
Unsized types, such as str and slices, are not supported.
Instead, arrays should be used.
For strings, the FixedString type is also provided.
Usage
This crate revolves around the Serialise and Deserialise traits, both of which use streams – or more specifically – s-streams and d-streams.
Many core types come implemented with bzipper, including primitives as well as some standard library types such as Option and Result.
It is recommended in most cases to just derive these two traits for custom types (although this is only supported with enumerations and structures). Here, each field is chained according to declaration order:
use ;
let mut buf = new;
buf.write.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
Serialisation
To serialise an object implementing Serialise, simply allocate a buffer for the serialisation and wrap it in an s-stream (serialisation stream) with the Sstream type.
use ;
let mut buf = ;
let mut stream = new;
'Ж'.serialise.unwrap;
assert_eq!;
The maximum size of any given serialisation is specified by the MAX_SERIALISED_SIZE constant.
We can also use streams to chain multiple elements together:
use ;
let mut buf = ;
let mut stream = new;
// Note: For serialising multiple characters, the
// `FixedString` type is usually preferred.
'ل'.serialise.unwrap;
'ا'.serialise.unwrap;
'م'.serialise.unwrap;
'د'.serialise.unwrap;
'ا'.serialise.unwrap;
assert_eq!;
When serialising primitives, the resulting byte stream is in big endian (a.k.a. network endian). It is recommended for implementors to adhere to this convention as well.
Deserialisation
Deserialisation works with a similar syntax to serialisation.
D-streams (deserialisation streams) use the Dstream type and are constructed in a manner similar to s-streams.
To deserialise a buffer, simply call the deserialise method with the strema:
use ;
let data = ;
let stream = new;
assert_eq!;
And just like s-streams, d-streams can also be used to handle chaining:
use ;
let data = ;
let stream = new;
assert_eq!;
assert_eq!;
// The data can also be deserialised as a tuple (up
// to twelve elements).
let stream = new;
assert_eq!;