The protocol specification can be found here.
This crate targets simplicity and performance. No dependencies are used, just the standard Rust library.
It will implement Packable
and Unpackable
for Rust atomic types. The traits can also be implemented manually.
Vec
, String
, and unlocks custom extensions.MsgPacker
derive convenience macro.u32::MAX
.```rust use msgpacker::prelude::*; use std::collections::HashMap;
// boilerplate derives - those aren't required
// this convenience derive macro will implement Packable
and Unpackable
pub struct City {
// A String
implements MsgPacker
by default
name: String,
// Maps have a special treatment on the protocol. This directive will automatically
// implement `MsgPacker` for any map-like type (i.e. interacts with iterators of key/value
// pairs).
#[msgpacker(map)]
inhabitants_per_street: HashMap<String, u64>,
// Arrays also have a special treatment on the protocol. This directive will automatically
// implement `MsgPacker` for any iterator of types that implements `MsgPacker`.
// This is not implemented by default because there is a special case for `Vec<u8>`, that
// has a dedicated protocol binary format.
#[msgpacker(array)]
zones: Vec<String>,
}
// create an instance of a city. let city = City { name: "Kuala Lumpur".tostring(), inhabitantsperstreet: HashMap::from([ ("Street 1".tostring(), 10), ("Street 2".tostring(), 20), ]), zones: vec!["Zone 1".tostring(), "Zone 2".to_string()], };
// serialize the city into bytes let mut buf = Vec::new(); let n = city.pack(&mut buf); println!("serialized {} bytes", n);
// deserialize the city and assert correctness let (n, deserialized) = City::unpack(&buf).unwrap(); println!("deserialized {} bytes", n); assert_eq!(city, deserialized); ```
Results obtained with Intel(R) Core(TM) i9-9900X CPU @ 3.50GHz
.
The simplicity of the implementation unlocks a performance more than ~10x better than rmp-serde.