#binary #no-std #serialization #packable

no-std packable

A crate for packing and unpacking binary representations

13 releases (6 breaking)

Uses new Rust 2021

0.7.0 Oct 10, 2022
0.6.2 Aug 26, 2022
0.5.0 Jul 14, 2022
0.4.0 Jun 13, 2022
0.1.0 Jan 14, 2022

#101 in Rust patterns

Download history 1254/week @ 2022-08-18 1172/week @ 2022-08-25 1101/week @ 2022-09-01 1435/week @ 2022-09-08 1617/week @ 2022-09-15 2491/week @ 2022-09-22 1056/week @ 2022-09-29 1099/week @ 2022-10-06 1226/week @ 2022-10-13 1202/week @ 2022-10-20 1211/week @ 2022-10-27 1375/week @ 2022-11-03 1325/week @ 2022-11-10 1544/week @ 2022-11-17 1281/week @ 2022-11-24 1047/week @ 2022-12-01

5,616 downloads per month
Used in 11 crates (7 directly)

Apache-2.0

75KB
1.5K SLoC

Packable is a binary serialization and deserialization framework.

Design

Values of a type can be serialized and deserialized if the type implements the Packable trait. The serialization strategy used for each type is up to the user. However, Packable can also be derived, this provides a consistent serialization strategy.

For more information about the design of this crate please read the Packable, unpacker, packer, UnpackError and UnpackErrorExt documentation.

no_std compatibility

Packable is no_std compatible. This is achieved by introducing the Packer and Unpacker traits to abstract away any IO operation without relying on std::io. This has the additional benefit of allowing us to pack and unpack values from different kinds of buffers.

Types that implement Packable

The Packable trait is implemented for every sized integer type by encoding the value as an array of bytes in little-endian order.

Booleans are packed following Rust's data layout, meaning that true is packed as a 1 byte and false as a 0 byte. However, boolean unpacking is less strict and unpacks any non-zero byte as true.

Types such as Box<[T]>, [T; N] and Option<T> implement Packable if T implements Packable.

This crate also provides bounded integers under the bounded module which have additional syntactical checks to guarantee that the deserialized values are in-bounds. It is also possible to serialize and deserialize sequences of values by using the types provided in the prefix module, which represent linear sequences of values with a length prefix.

Check the Packable impl section for further information.

Features

io

This feature provides the types IoPacker and IoUnpacker which allow packing and unpacking from values whose types implement Write and Read respectively.

primitive-types

This feature implements Packable for U256 encoding its values as arrays of bytes in little-endian order.

serde

This feature derives Serialize and Deserialize for the types provided in the bounded and prefix modules

std

This feature implements Error for all the error types provided by this crate.

usize

This feature implements Packable for usize, isize, Vec<T>, Box<[T]> and String. This is done serializing and deserializing pointer sized integers as 64-bit integers. This feature will not work for targets with a pointer width larger than 64.

License: Apache-2.0

Dependencies

~0.6–1.3MB
~28K SLoC