14 releases (6 breaking)

new 0.9.0 May 7, 2024
0.8.0 Apr 3, 2024
0.6.0 Mar 4, 2024
0.3.0 Feb 29, 2024
0.0.0 Sep 30, 2022

#1037 in Rust patterns

Download history 15/week @ 2024-02-20 246/week @ 2024-02-27 93/week @ 2024-03-05 50/week @ 2024-03-12 5/week @ 2024-03-26 147/week @ 2024-04-02 4/week @ 2024-04-09

156 downloads per month
Used in 2 crates (via psibase)

MIT license

41KB
941 lines

Rust support for the fracpack format.

Psibase uses a new binary format, fracpack, which has the following goals:

  • Quickly pack and unpack data, making it suitable for service-to-service communication, node-to-node communication, blockchain-to-outside communication, and database storage.
  • Forwards and backwards compatibility; it supports adding new optional fields to the end of structs and tuples, even when they are embedded in variable-length vectors, fixed-length arrays, optional, and other structs and tuples.
  • Option to read without unpacking (almost zero-copy); helps to efficiently handle large data. TODO: this library doesn't implement this feature yet.
  • Doesn't require a code generator to support either C++ or Rust; macros and metaprogramming handle it.
  • Efficient compression when combined with the compression algorithm from Cap 'n' Proto.

Example use

use fracpack::{Pack, Unpack, Result};

#[derive(Pack, Unpack, PartialEq, Debug)]
#[fracpack(fracpack_mod = "fracpack")]
struct Example {
    a_string: String,
    a_tuple: (u32, String),
}

let orig = Example {
    a_string: "content".into(),
    a_tuple: (1234, "5678".into()),
};

// Convert to fracpack format
let packed: Vec<u8> = orig.packed();

// Convert from fracpack format
let unpacked = Example::unpacked(&packed)?;

assert_eq!(orig, unpacked);

Note: #[fracpack(fracpack_mod = "fracpack")] is only needed when using the fracpack library directly instead of through the psibase crate.

Caution

It's easy to accidentally convert from a fixed-size array reference (&[T;7]) to a slice (&[T]). This matters to fracpack, which has different, and incompatible, encodings for the two types.

Dependencies

~2MB
~43K SLoC