#messagepack #protocols #derive #macro #msgpacker #msg-packer

macro msgpacker-derive

Derive macros for the MessagePack protocol implementation for Rust

6 releases

0.3.1 Jun 9, 2023
0.3.0 Jun 9, 2023
0.2.0 Jun 7, 2023
0.1.2 Jul 31, 2022

#27 in #msgpack

Download history 129/week @ 2023-12-06 527/week @ 2023-12-13 537/week @ 2023-12-20 186/week @ 2023-12-27 453/week @ 2024-01-03 330/week @ 2024-01-10 209/week @ 2024-01-17 156/week @ 2024-01-24 237/week @ 2024-01-31 354/week @ 2024-02-07 764/week @ 2024-02-14 464/week @ 2024-02-21 329/week @ 2024-02-28 140/week @ 2024-03-06 194/week @ 2024-03-13 170/week @ 2024-03-20

908 downloads per month
Used in 16 crates (2 directly)

MIT/Apache

25KB
502 lines

MessagePacker - a no-std msgpack implementation

crates.io Documentation License

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.

Features

  • alloc: Implements the functionality for Vec, String, and unlocks custom extensions.
  • derive: Enables MsgPacker derive convenience macro.
  • strict: Will panic if there is a protocol violation of the size of a buffer; the maximum allowed size is u32::MAX.
  • std: Will implement the Packable and Unpackable for std collections.

Example

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);

Benchmarks

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.

Pack 1.000 elements

image image

Unpack 1.000 elements

image image

Dependencies

~275–720KB
~17K SLoC