Crates.io | xxai-msgpacker |
lib.rs | xxai-msgpacker |
version | 0.4.4 |
source | src |
created_at | 2023-06-18 02:55:43.611434 |
updated_at | 2023-06-18 03:17:05.743043 |
description | MessagePack protocol implementation for Rust. |
homepage | |
repository | https://github.com/codx-dev/msgpacker |
max_upload_size | |
id | 893229 |
size | 96,103 |
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
.Packable
and Unpackable
for std
collections.use msgpacker::prelude::*;
use std::collections::HashMap;
// boilerplate derives - those aren't required
#[derive(Debug, PartialEq, Eq)]
// this convenience derive macro will implement `Packable` and `Unpackable`
#[derive(MsgPacker)]
pub struct City {
name: String,
// The traits are implemented for stdlib collections. If you have a custom map, you can use the
// directive `#[msgpacker(map)]` so the traits will be automatically implemented through the
// iterators of the map.
inhabitants_per_street: HashMap<String, u64>,
// This is also automatically implemented. The manual implementation is via `#[msgpacker(array)]`.
zones: Vec<String>,
}
// create an instance of a city.
let city = City {
name: "Kuala Lumpur".to_string(),
inhabitants_per_street: HashMap::from([
("Street 1".to_string(), 10),
("Street 2".to_string(), 20),
]),
zones: vec!["Zone 1".to_string(), "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.