Crate serde_brief

source
Expand description

§Serde-Brief

Serde-Brief (German for letter) is a crate for encoding and decoding data into a binary format that is self-descriptive and serde-compatible.

§Design Goals

Not necessarily in order of importance:

  • Convenient to use for developers: Integrates into the Rust ecosystem via serde, supporting all of its features in its derived implementations (e.g. renaming, flattening, ..).
  • Compatibility: Easy to add or re-order fields/variants without breakage.
  • #![no_std] and std compatible.
  • Resource efficient: High performance, low memory usage.
  • Interoperability: Different architectures can communicate flawlessly.

§More Detailed Documentation

See more detailed documentation in the docs module. It contains information on the binary representation format.

§Feature Flags

This library is both no-std and std compatible. Additionally, there are some other features to enable additional functionality:

Feature FlagDefaultDescription
allocnoEnables the use of alloc types like serialization to a Vec.
heaplessnoEnables serialization to a heapless::Vec.
stdnoEnables the use of std types like serialization to a Writer and deserialization from a Reader.
tracingnoEnables tracing instrumentation.

§Flavors / Modes

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.

§Usage

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 Vecs and such with features like alloc or std.

§Example Serialization/Deserialization

The heapless feature was enabled for this example. It is similarly possible with std’s Vec or just slices.

use heapless::Vec;
use serde::{Deserialize, Serialize};

#[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);

Re-exports§

Modules§

  • Deserialization implementation.
  • Here lives more detailed documentation and explanations for this crate and its binary format.
  • Serialization implementation.
  • Generic value that can contain any value in our data format.

Structs§

  • Configuration for (de-)serialization.

Enums§

  • Error when (de-)serializing.

Functions§

Type Aliases§

  • Result type that uses the serde-brief error.