Crates.io | msgpackin |
lib.rs | msgpackin |
version | 0.0.4 |
source | src |
created_at | 2022-02-15 23:21:34.272679 |
updated_at | 2023-08-31 15:15:11.731704 |
description | Msgpackin pure Rust MessagePack encoding / decoding library |
homepage | |
repository | https://github.com/neonphog/msgpackin |
max_upload_size | |
id | 532977 |
size | 114,173 |
Msgpackin pure Rust MessagePack encoding / decoding library.
Msgpackin supports no_std
, but requires the alloc
crate.
If you're looking for no alloc support, see the msgpackin_core
crate.
Msgpackin supports serde with the serde
feature in either no_std
or std
mode. If you want std
mode, you must also enable the
serde_std
feature, as a temporary workaround until weak dependency
features are stablized.
from_ref
no_std
serde supportstd::io::{Read, Write}
support in std
modefutures-io
or tokio
features-1
))std
- enabled by default, pulls in the rust std library, enabling
encoding and decoding via std::io::{Read, Write}
traitsserde
- enables serialization / deserialization through the serde
cratefutures-io
- enables async encoding and decoding through the futures
io::{AsyncRead, AsyncWrite}
traitstokio
- enables async encoding and decoding through the tokio
io::{AsyncRead, AsyncWrite}
traitsno_std
Exampleuse msgpackin::*;
let expect = Value::Map(vec![
("nil".into(), ().into()),
("bool".into(), true.into()),
("int".into(), (-42_i8).into()),
("bigInt".into(), u64::MAX.into()),
("float".into(), 3.141592653589793_f64.into()),
("str".into(), "hello".into()),
("ext".into(), Value::Ext(-42, b"ext-data".to_vec().into())),
("arr".into(), Value::Arr(vec!["one".into(), "two".into()])),
]);
let encoded = expect.to_bytes().unwrap();
let decoded = ValueRef::from_ref(&encoded).unwrap();
assert_eq!(expect, decoded);
std
Exampleuse msgpackin::*;
let expect = Value::Map(vec![("foo".into(), "bar".into())]);
let mut buf = Vec::new();
{
let writer: Box<dyn std::io::Write> = Box::new(&mut buf);
expect.to_sync(writer).unwrap();
}
let reader: Box<dyn std::io::Read> = Box::new(buf.as_slice());
let decoded = Value::from_sync(reader).unwrap();
assert_eq!(expect, decoded);
use msgpackin::*;
let expect = Value::Map(vec![("foo".into(), "bar".into())]);
let mut buf = Vec::new();
{
let writer: Box<dyn tokio::io::AsyncWrite + Unpin> = Box::new(&mut buf);
futures::executor::block_on(async { expect.to_async(writer).await })
.unwrap();
}
let reader: Box<dyn tokio::io::AsyncRead + Unpin> =
Box::new(buf.as_slice());
let decoded =
futures::executor::block_on(async { Value::from_async(reader).await })
.unwrap();
assert_eq!(expect, decoded);
serde
Exampleuse msgpackin::*;
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
struct X {
pub nil: (),
pub bool_: bool,
pub int: i8,
pub big_int: u64,
pub float: f64,
pub str_: String,
pub arr: Vec<String>,
}
let expect = X {
nil: (),
bool_: true,
int: -42,
big_int: u64::MAX,
float: 3.141592653589793,
str_: "hello".into(),
arr: vec!["one".into(), "two".into()],
};
let encoded = to_bytes(&expect).unwrap();
let decoded: X = from_sync(encoded.as_slice()).unwrap();
assert_eq!(expect, decoded);
License: Apache-2.0