Expand description
§pack-io
Compact binary wire format for Rust. Combines speed, schema evolution, and zero-copy deserialization under a single coherent contract.
§At a glance
- Tier 1 —
encodeanddecode: one line each direction.decode_with_config/decode_view_with_configtake aConfigfor untrusted or hashed input. - Tier 2 —
Encoder/Decoderfor in-memory buffers.IoEncoder/IoDecoderforstd::io::Write/Readstreams (std-gated).encode_into/decode_fromconvenience helpers over Read / Write.
- Tier 3 — implement
Serialize/Deserializeon your own types, or derive them (featurederive). Both traits are generic over theEncode/Decodebehaviour traits, so one impl works through every encoder / decoder the crate ships.
§Primitive support
Integers (u8 … u128, i8 … i128, usize / isize, NonZero*),
bool, char, f32, f64, String / &str / Box<str>,
fixed-size arrays [T; N], tuples of arity 1…12, Option<T>,
Result<T, E>, Duration, PhantomData<T>, and (). Rc<T>,
Arc<T>, Box<T> and Cow<'_, T> encode transparently as the value they hold.
§Collection support
Vec<T> / &[T] / Box<[T]> / VecDeque<T>, BTreeMap<K, V>,
BTreeSet<T>, and (with the default std feature) HashMap<K, V> and
HashSet<T>. Maps and sets encode in canonical key-sorted order so
that hashing, signing, or content-addressing the output is safe
regardless of insertion order or hash randomisation.
§Stability
The wire format has not changed since 1.0: every 2.x decoder reads every
encoding a 1.x or 2.x encoder produced for valid data (checked against
golden vectors written by 1.0.1), and every 2.x encoder produces the
same bytes for the same value. 2.0 is a major release because decoding
now enforces limits and the derive rejects misuse 1.x accepted; see the
migration guide.
The normative spec is
docs/WIRE_FORMAT.md
and the public surface is listed in
docs/API.md.
§Quick start
use pack_io::{encode, decode};
let bytes = encode(&(7_u64, true, String::from("hello"))).unwrap();
let back: (u64, bool, String) = decode(&bytes).unwrap();
assert_eq!(back, (7, true, String::from("hello")));§Invariants
- Round-trip integrity —
decode(encode(v)) == vfor every supported type. - Determinism — the same value always produces the same bytes,
regardless of insertion order, platform, or build flags. With
Config::canonicalon, the reverse also holds: the decoder accepts only those bytes, so the bytes identify the value. For a versioned struct that means only frames at the reader’s own version (older and newer frames decode leniently). The guarantee covers types built from pack-io’s impls and derives; a hand-written impl orwithhelper must itself accept one encoding per value, and serde sets needpack_io::serde::sorted_set. - Safe decode: on any byte sequence, no panic, no read past the
input, no allocation larger than
Config::max_allocfor one value, no length-prefixed allocation larger than the input can back, the memory one decoded message builds (collection slots at their in-memory size, owned buffers, boxed values, elements that occupy no input) bounded byConfig::total_alloc_limit, and no nesting deeper thanConfig::max_depth(so no stack overflow). For peer input, decode underConfig::network. - Wire-format stability — any 2.x decoder reads any 1.x or 2.x encoding of valid data.
§Feature flags
| Feature | Default | Effect |
|---|---|---|
std | yes | std::error::Error, HashMap / HashSet, the io module |
derive | no | #[derive(Serialize, Deserialize, DeserializeView)] |
schema | no | alias for derive, kept for compatibility (the schema attributes work with derive alone) |
serde | no | the serde adapter: any serde type on the pack-io wire |
bytes | no | Serialize / Deserialize for bytes::Bytes / BytesMut, and Encode for BytesMut |
§no_std
pack-io is no_std-capable. The default build enables std for the
std::error::Error impl, HashMap / HashSet integration, and the
io module. Disable the default feature to compile against core +
alloc only; derives (including versioned ones) work there too:
pack-io = { version = "2", default-features = false, features = ["derive"] }Re-exports§
pub use crate::io::IoDecoder;stdpub use crate::io::IoEncoder;stdpub use crate::io::decode_from;stdpub use crate::io::decode_from_with_config;stdpub use crate::io::encode_into;std
Modules§
- io
std std::io::Read/std::io::Writeintegration: the streaming Tier-2 encoder and decoder pair, plus convenience free functions.- serde
serde serdeadapter (featureserde): put any type that implementsserde::Serialize/serde::Deserializeon the pack-io wire.
Structs§
- Config
- Configuration for a decode session.
- Decoder
- In-memory decoder. Borrows from an input slice and advances a position pointer as values are read. Bounds-checked on every operation.
- Encoder
- In-memory encoder. Writes into an owned
Vec<u8>; the buffer can be reused across encodes by callingEncoder::taketo swap it out.
Enums§
- Serial
Error - Every error returned by the codec.
Constants§
- VERSION
- Semantic version of this crate, as declared in
Cargo.toml.
Traits§
- Decode
- Source that a
Deserializeimplementation reads its wire-format bytes from. - Deserialize
- Types that know how to read themselves from any
Decodesource. - Deserialize
View - Types that decode by borrowing directly from the input slice rather than allocating owned copies.
- Encode
- Sink that a
Serializeimplementation writes its wire-format bytes into. - Serialize
- Types that know how to write themselves into any
Encodesink.
Functions§
- decode
- Decode a value of type
Tfrombytes, requiring the input to be fully consumed. - decode_
view - Decode a borrowed value of type
Tfrombytes, requiring the input to be fully consumed. - decode_
view_ with_ config decode_viewunder an explicitConfig.- decode_
with_ config decodeunder an explicitConfig: decode a value of typeTfrombytes, requiring the input to be fully consumed.- encode
- Encode
valueinto a freshly allocatedVec<u8>. - peek_
version - Peek the schema version of a payload produced by a
#[pack_io(version = N)]type without consuming the buffer. - varint_
len - Number of bytes the LEB128 varint encoding of
valueoccupies (1–10).
Type Aliases§
- Result
- Convenience alias for
Result<T, SerialError>.
Derive Macros§
- Deserialize
derive - Derive
pack_io::Deserializefor a struct or enum (owning decode). - Deserialize
View derive - Derive
pack_io::DeserializeViewfor a struct or enum (zero-copy decode). - Serialize
derive - Derive
pack_io::Serializefor a struct or enum.