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: anyserde::Serialize/serde::Deserializetype to and from MessagePack (featureserde). value:ValueandValueRef, MessagePack data of any shape, and their readers and writers (featurevalue).raw: readers and writers for single MessagePack values with direct mapping to the binary format, no heap allocations; the only part available withoutstd.
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
stdfeature, works on byte slices inno_stdbuilds. - ser
- Serialize a Rust data structure into MessagePack data.
- value
ValueandValueRef: 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.