Crates.io | serde-brief |
lib.rs | serde-brief |
version | 0.1.1 |
source | src |
created_at | 2024-09-28 21:39:18.663133 |
updated_at | 2024-11-06 11:27:54.473369 |
description | A brief, self-descriptive, serde-compatible binary format. |
homepage | https://github.com/FlixCoder/serde-brief |
repository | https://github.com/FlixCoder/serde-brief |
max_upload_size | |
id | 1390385 |
size | 269,918 |
Serde-Brief (German for letter) is a crate for encoding and decoding data into a binary format that is self-descriptive and serde-compatible.
Not necessarily in order of importance:
serde
, supporting all of its features in its derived implementations (e.g. renaming, flattening, ..).#![no_std]
and std compatible.unsafe
).The format is new and therefore NOT YET STABLE.
The format is specified here.
By default, structs' field names and enums' variant names are encoded as strings. This can be configured to be encoded as unsigned integers of their indices instead. However, this has compatibility implications and some serde features do not work with the index representation. See the format specification for more info.
How does Serde-Brief compare to ..?
Postcard is NOT a self-describing format. It's encoding solely consists of the raw data and the deserializer needs to have the same information on the data schema. This makes it more difficult to change the data format, e.g. add new fields.
Postcard is producing way smaller encoded data due to the missing schema information and field names. It is also faster.
Serde-Brief supports decoding unknown data and parsing it into the requested structures regardless of additional fields or different orders.
Pot is a self-describing format as well. It's encoding is more space-efficient due to reducing repeated type/schema definitions. This comes at the cost of serialization/deserialization speed.
It is also not no-std compatible.
Serde-Brief is faster most of the times, but less space-efficient.
JSON is a self-describing format as well. However, it is text based and therefore requires string escaping. Bytes cannot be efficiently represented. However, JSON is widely adopted, as you already know :D
In Serde-Brief, map keys can not only be strings. Unlike in JSON, keys can be nested data, so something like HashMap<MyKeyStruct, MyValueStruct>
can be serialized and deserialized without issues.
Serde-Brief is both more space-efficient and faster.
Add the library to your project with cargo add serde-brief
. By default, no features are enabled (currently), so it is no-std by default. You can enable use of Vec
s and such with features like alloc
or std
.
The heapless
feature was enabled for this example. It is similarly possible with std
's Vec
or just slices.
use heapless::Vec;
use serde::{Serialize, Deserialize};
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct MyBorrowedData<'a> {
name: &'a str,
age: u8,
}
let data = MyBorrowedData { name: "Holla", age: 21 };
let mut output: Vec<u8, 22> = serde_brief::to_heapless_vec(&data).unwrap();
assert_eq!(output, [
17,
11, 4, b'n', b'a', b'm', b'e', 11, 5, b'H', b'o', b'l', b'l', b'a',
11, 3, b'a', b'g', b'e', 3, 21,
18
]);
let parsed: MyBorrowedData = serde_brief::from_slice(&output).unwrap();
assert_eq!(parsed, data);
For benchmark results, see here.
I expect there to be plenty room for performance improvements still.
If you are interested in maximum performance, please try using profile-guided optimization. It measurably improves performance on this library. For further information, see the PGO usage docs.
The serialization/deserialization is reasonably fast. Between postcard and serde_json mostly. The data-size is also between postcard and JSON.
I expect there is a lot improvements possible, we are still way slower than postcard sadly.
cargo install cargo-make cargo-nextest
.search_project_root = true
into cargo-make's user configuration, so that cargo make
can be run from sub-folders.cargo make format
cargo make formatting
cargo make test
cargo make nextest
cargo make clippy
cargo make ci
Currently, I am always using the latest stable Rust version and do not put in effort to keep the MSRV. Please open an issue in case you need a different policy, I might consider changing the policy.
Licensed under the MIT license. All contributors agree to license under this license.