#messagepack #tokio #functional #sync #read #read-write

messagepack-async

A simple functional library for read/writing messagepack with tokio

6 releases

0.2.3 Dec 23, 2024
0.2.2 Dec 22, 2024
0.1.1 Dec 16, 2024

#515 in Asynchronous

Download history 379/week @ 2024-12-16 163/week @ 2024-12-23 5/week @ 2024-12-30 1/week @ 2025-01-06

548 downloads per month

MIT/Apache

55KB
1.5K SLoC

MessagePack-Async

messagepack-async is a simple, functional library for reading and writing MessagePack with std::io::{Read, Write} and tokio::io::{AsyncRead, AsyncWrite}.

No features are enabled by default so you will either need to enable at least one of sync or tokio depending on what you're planning on reading/writing to.

[dependencies]
# Stdlib
messagepack-async = { version = "0.2.3", features = [ "sync" ] }
# Tokio
messagepack-async = { version = "0.2.3", features = [ "tokio" ] }

The Traits

This crate provides four traits; sync::ReadFrom, sync::WriteTo, tokio::ReadFrom, and tokio::WriteTo. These provide methods to read and write Values to sources and sinks that implement the read/write traits from the stdlib or tokio.

Note that write_to will write exactly the value you supply. That means if you write a Int::U64(0) to a sink, it won't write the value as a U8 even though it could be represented as such.

use messagepack_async::{Value, Int, sync::{ReadFrom, WriteTo}};
let mut data = vec![];
let value = Value::Int(Int::U64(0));
value.write_to(&mut data).unwrap();
let mut cursor = data.as_slice();
let read_value = Value::read_from(&mut cursor).unwrap();
assert_eq!(value, read_value);

The implementation of std::cmp::PartialEq is derived so differentiates between different variants of Int.

If you need to minimise the size in bytes of the values you write, create your values with the Value::int, Value::signed_int, and Value::unsigned_int constructors for Value.

Dependencies

~0–8.5MB
~65K SLoC