ax-codec-core
Low-level binary codec primitives for ax_codec. This crate provides the core encoding/decoding traits, buffer abstractions, and utility functions.
Features
no_stdcompatible (withallocfeature)- Zero-copy borrow decoding via
View<'a> - 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
- CRC32 checksums for data integrity (optional)
- Version-gated decoding for wire format evolution
Installation
Add to your Cargo.toml:
[]
= { = "0.1", = ["std"] }
Feature Flags
std- Enables std library support (default)alloc- Enables alloc library support (required for most types)simd- Enables SIMD-accelerated varint decoding (x86_64, aarch64)crc32- Enables CRC32 checksum supportbytes- Enables integration withbytescrateaxhash- Enables integration withaxhash-mapcratedhat-heap- Enables dhat heap profilingjemalloc- Enables jemalloc allocator integration
Core Traits
Encode
use ;
let data = MyData ;
let mut writer = new;
data.encode.unwrap;
let encoded = writer.into_vec;
Decode
use ;
let mut reader = new;
let decoded = decode.unwrap;
View (Zero-Copy)
use ;
let mut reader = new;
let view = view.unwrap;
// No allocation - borrows directly from input
Validate
use ;
let mut reader = new;
// Validates wire format without constructing the type
validate.unwrap;
Buffer Types
VecWriter
Allocating writer that grows as needed:
use VecWriter;
let mut writer = new;
writer.write_all.unwrap;
let bytes = writer.into_vec;
PooledVecWriter
Thread-local pooled writer for reduced allocations:
use PooledVecWriter;
let mut writer = new;
writer.write_all.unwrap;
let bytes = writer.into_vec; // Returns buffer, doesn't recycle
// Writer automatically recycles on drop if not finished
SliceReader
Non-allocating reader from byte slices:
use SliceReader;
let data = b"hello world";
let mut reader = new;
let byte = reader.next.unwrap;
let remaining = reader.remaining;
Decode Limits
Control resource usage during decoding:
use ;
let limits = DecodeLimits ;
let reader = new;
let mut limited = new;
let decoded = decode.unwrap;
Preset Limits
use DecodeLimits;
// Conservative limits for untrusted input
let limits = conservative;
// Unlimited limits (use with caution)
let limits = unlimited;
// Custom limits
let limits = DecodeLimits ;
Varint Encoding
Variable-length integer encoding (LEB128 style):
use varint;
let mut writer = new;
encode_uvarint.unwrap;
encode_svarint.unwrap;
let mut reader = new;
let decoded_u = decode_uvarint.unwrap;
let decoded_s = decode_svarint.unwrap;
Checksums
CRC32 checksums for data integrity:
use checksum;
let data = MyData ;
let mut writer = new;
// Encode with checksum
encode_with_checksum.unwrap;
// Decode with checksum verification
let mut reader = new;
let decoded = .unwrap;
Versioned Encoding
Wire format versioning:
use version;
let data = MyData ;
let mut writer = new;
// Encode with version
encode_versioned.unwrap;
// Decode with version range check
let mut reader = new;
let = .unwrap;
assert_eq!;
Supported Types
Primitives
- All integer types:
u8,u16,u32,u64,u128,i8,i16,i32,i64,i128 - Floating point:
f32,f64 - Boolean:
bool
Collections
Vec<T>- withallocfeature&[T]- zero-copy for FixedSize typesOption<T>String- withallocfeature&str- zero-copyHashMap<K, V>- withaxhashfeatureHashSet<T>- withaxhashfeatureCow<'a, str>- withallocfeatureCow<'a, [u8]>- withallocfeature
FixedSize Trait
Types with known constant size can implement FixedSize for optimizations:
use FixedSize;
License
MIT