ax_codec
A Rust binary codec with predictable runtime, minimal allocation, and zero-copy decoding.
Features
no_stdcompatible (withallocfeature)- Zero-copy borrow decoding via
View<'a> - Derive macros for
Encode,Decode, andView - Varint encoding (LEB128 / Protocol Buffers style)
- SIMD fast-path for varint decode on x86_64 (optional)
- Decode limits — configurable max allocation, depth, string/vec length
- Tokio integration —
tokio_util::codec::Encoder/Decoderviaax_codec<T>
Quick Start
Note: ax_codec works on stable Rust (see
rust-toolchain.toml).
Add to your Cargo.toml:
[]
= { = "0.1", = ["std"] }
Derive macros
use ;
Zero-copy view
use ;
let msg = Message ;
let mut w = new;
msg.encode.unwrap;
let bytes = w.into_vec;
let decoded = view.unwrap;
assert_eq!;
Validation (without allocation)
use ;
let packet = Packet ;
let mut w = new;
packet.encode.unwrap;
// Validate wire format without constructing Packet
let mut r = new;
validate.unwrap;
Tokio codec
use ax_codec;
use Framed;
let framed = new;
Workspace Crates
| Crate | API | Description |
|---|---|---|
Encode |
Serialize a value to bytes | |
Decode |
Deserialize from bytes (allocating) | |
View<'a> |
Deserialize without allocation (zero-copy) | |
BufferWriter |
Trait for output sinks (Vec, pool, IO) | |
BufferReader<'a> |
Trait for input sources (slice, IO) | |
encode_to_vec |
Helper: value.encode_to_vec() |
|
decode_with_checksum |
CRC32-checked wrapper | |
decode_versioned |
Version-gated decode with range check |
Testing
Full test suite
ax_codec uses stable Rust (see rust-toolchain.toml). Cargo will automatically use the pinned toolchain:
# Run all tests with all features
# Test individual crate
# no_std compatibility (alloc only)
Code quality
Benchmarks
Quick benchmarks
Comprehensive metrics (decode latency, payload size, scaling)
# Payload size comparison
# Decode latency + scaling behavior
# Allocation count (dhat)
# Full metrics script (compile time, binary size, RSS)
Profiling tools
| Tool | Metric | Platform | Command |
|---|---|---|---|
| dhat | alloc count, heap profile | All | cargo run --features dhat-heap --example dhat_profile |
| heaptrack | heap tracking | Linux only | heaptrack cargo run --example dhat_profile --features std |
| cargo-bloat | binary size breakdown | All | cargo bloat --release -p ax-codec-core |
| /usr/bin/time | RSS, peak memory | All | /usr/bin/time -l cargo run --example dhat_profile |
| jemalloc | heap stats, fragmentation | All | See examples/jemalloc_profile.rs |
| Instruments | Time profiler, allocations | macOS | instruments -t 'Time Profiler' cargo run ... |
See ax-codec-core/benches/ for ax_codec-specific benchmarks covering varint, struct encode/decode, and throughput scaling.
Fuzzing
License
MIT