📦 lencode
Compact, fast binary encoding with varints, optional deduplication, and opportunistic zstd compression for bytes and strings. no_std by default, with an opt‑in std feature.
Highlights
- Fast varints: efficient for small and large integers
- Optional deduplication: replace repeats with compact IDs for supported types
- Bytes/strings compression: flagged header + zstd when smaller
- no_std + alloc: works without
std(useszstd-safe) - Derive macros:
#[derive(Encode, Decode)]for your types - Solana support: feature
solanaadds v2/v3 SDK types - Big-endian ready: CI runs tests on s390x
Install
[]
= "0.1"
# With standard library types (e.g., Cow)
= { = "0.1", = ["std"] }
# With Solana type support (implies std)
= { = "0.1", = ["solana"] }
Quick start
Derive and round‑trip
use *;
let p = Point ;
let mut buf = Vecnew;
encode?;
let q: Point = decode?;
assert_eq!;
Collections and primitives
use *;
let values: = .collect;
let mut buf = Vecnew;
encode?;
let rt: = decode?;
assert_eq!;
Deduplication (optional)
To benefit from deduplication for your own types, implement Pack and the marker traits and pass the encoder/decoder via encode_ext/decode_ext.
use *;
;
let vals = vec!;
// Encode with deduplication enabled
let mut enc = new;
let mut buf = Vecnew;
encode_ext?;
// Decode with deduplication enabled
let mut dec = new;
let roundtrip: = decode_ext?;
assert_eq!;
Compact bytes and strings
&[u8], Vec<u8], VecDeque<u8], &str, and String use a compact flagged header: varint((payload_len << 1) | flag) + payload.
flag = 0→ raw bytes/UTF‑8flag = 1→ zstd frame (original size stored inside the frame)
The encoder picks whichever is smaller per value.
Supported types
- Primitives: all ints,
bool,f32,f64 - Arrays:
[T; N] - Option:
Option<T> - Bytes/strings:
&[u8],Vec<u8],VecDeque<u8],&str,String - Collections (alloc):
Vec<T>,BTreeMap<K,V>,BTreeSet<V>,VecDeque<T>,LinkedList<T>,BinaryHeap<T> - Tuples:
(T1,)… up to 11 elements stdfeature: adds support forstd::borrow::Cow<'_, T>solanafeature:Pubkey,Signature,Hash, messages (legacy/v0), and related v2/v3 types
Note: HashMap/HashSet are not implemented.
Cargo features
default: core +no_std(usesalloc)std: enablesstdadapters andCowsolana: Solana SDK v2 + Agave v3 types (impliesstd)
Big‑endian and portability
- Varints are decoded efficiently on little‑endian and portably on big‑endian
- CI runs tests on
s390x-unknown-linux-gnuusingcross Packalways uses a stable little‑endian layout
Benchmarks
# Full suite
# Compare against borsh/bincode
# Solana‑specific
Errors
Errors use lencode::io::Error and map to std::io::Error under std.
use *;
use Error;
let mut buf = Vecnew;
match encode
Examples
examples/size_comparison.rs: space savings on repeated Solana pubkeysexamples/versioned_tx_compression.rs: end‑to‑end on Solana versioned transactions
Run with --features solana.
License
MIT
Changelog
0.1.0
- Lencode varints for signed/unsigned ints
- Optional deduplication via
DedupeEncoder/DedupeDecoder - Bytes/strings with flagged header + zstd
no_stdby default;stdandsolanafeatures- Derive macros for
Encode/Decode - Big‑endian test coverage