Skip to main content

Crate rmpx

Crate rmpx 

Source
Expand description

Pure Rust MessagePack: serde support, a dynamic value type and a low-level API in one crate.

A reworked fork of rmp, rmp-serde and rmpv: one crate, no unsafe, no panics, faster; the wire format is the same, the API is not. The README lists the differences and the trade-offs.

  • The crate root and ser / de: any serde::Serialize / serde::Deserialize type to and from MessagePack (feature serde).
  • value: Value and ValueRef, MessagePack data of any shape, and their readers and writers (feature value).
  • raw: readers and writers for single MessagePack values with direct mapping to the binary format, no heap allocations; the only part available without std.

All three features are on by default; default-features = false leaves only raw, usable in no_std builds.

§Serde

use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Human {
    age: u32,
    name: String,
}

let john = Human { age: 42, name: "John".into() };

// Structs as arrays of their fields: compact, but both sides must agree on the field order.
let buf = rmpx::to_vec(&john).unwrap();
assert_eq!(buf, [0x92, 0x2a, 0xa4, b'J', b'o', b'h', b'n']);

// Structs as maps with field names, readable by any MessagePack implementation.
let named = rmpx::to_vec_named(&john).unwrap();

// Either form decodes into the struct.
assert_eq!(rmpx::from_slice::<Human>(&buf).unwrap(), john);
assert_eq!(rmpx::from_slice::<Human>(&named).unwrap(), john);

Serializer and Deserializer work with any std::io::Write / std::io::Read and take their options through config.

§Dynamic values

use rmpx::Value;

let buf = rmpx::to_vec_named(&std::collections::BTreeMap::from([("id", 7)])).unwrap();

let value = rmpx::value::decode::read_value(&mut &buf[..]).unwrap();
assert_eq!(value, Value::Map(vec![("id".into(), 7.into())]));

§Low level

let mut buf = Vec::new();
rmpx::raw::encode::write_sint(&mut buf, 300).unwrap();
assert_eq!(buf, [0xcd, 0x01, 0x2c]);
assert_eq!(rmpx::raw::decode::read_int::<u16, _>(&mut &buf[..]).unwrap(), 300);

Re-exports§

pub use crate::de::Deserializer;
pub use crate::de::from_read;
pub use crate::de::from_slice;
pub use crate::ser::Serializer;
pub use crate::ser::to_vec;
pub use crate::ser::to_vec_named;
pub use crate::ser::to_writer;
pub use crate::ser::to_writer_named;
pub use crate::value::Value;
pub use crate::value::ValueRef;

Modules§

config
Change MessagePack behavior with configuration wrappers.
de
Generic MessagePack deserialization.
raw
The low-level API: readers and writers for single MessagePack values, with direct mapping to the binary format. It makes no heap allocations and, without the std feature, works on byte slices in no_std builds.
ser
Serialize a Rust data structure into MessagePack data.
value
Value and ValueRef: MessagePack data of any shape, owned or borrowed from the input, with conversions from and to Rust types, and their readers (decode) and writers (encode).

Constants§

MSGPACK_EXT_STRUCT_NAME
Name of the serde newtype struct that stands for a MessagePack ext value.