Skip to main content

Crate capnpack

Crate capnpack 

Source
Expand description

§capnpack

This is an implementation of CapnProto’s packing algorithm in pure Rust. Along with a few slight modifications to allow the algorithm to work for any binary payload of any size.

§Usage

// Packing:
let data = vec![1, 0, 0, 0, 2, 3, 4, 5];
let packed = capnpack::pack(&data);

// Un-packing:
let packed = vec![0xF1, 1, 2, 3, 4, 5];
let unpacked_data = capnpack::unpack(&packed, 8).unwrap(); // size hint can be any reasonable number

assert_eq!(unpacked_data, data);

Note: Data packed by this library may not be compatible with other implementations of CapnProto’s packing algorithm due to slight modifications to the algorithm to allow for any sized payloads.

§Modifications to the packing algorithm

If the input data to the packing algorithm does not align to a multiple of 8 bytes, and is compressible via the typical format. Then the remaining data is processed via the typical packing process, but with the corresponding places for the missing bytes set to 1.

Ex: Say we have data 0, 0, 1, 2, 0
The tag will be: 0b 111 01100. 01100 is the normal packing algorithm, and the 111 is the filler 1s for the missing bytes.
This data packed will be: 0b11101100, 1, 2.

This allows the decoder/unpacker to know when to stop processing without any additional overhead.

Structs§

UnexpectedEOF
This error indicates capnpack::unpack needed more data, but was unable to get it.
self.0 contains the tag that generated this error.

Functions§

pack
Pack some data using Capn-Pack
unpack
Un-pack some data packed using Capn-Pack

expected_size_hint is a hint for how many bytes you expect to receive. This can be any number, unpack() will always allocate an output buffer of at least this size. Performance greatly increases if expected_size_hint is >= what unpack actually needs.

unpack() may return capnpack::UnexpectedEOF if ran out of data while unpacking. However, the lack of this error does not guarantee any thing in terms of weather or not the data is valid.