byten
A binary codec library for efficient encoding and decoding of Rust data structures.
⚠️ Early Development: This library is in active development and the API may change.
Features
- 🚀 Derive macros for automatic codec implementation (
Encode,Decode,Measure) - 🔢 Primitive types with custom byte ordering (BE/LE) and variable-length encoding
- 📦 Collections support:
Vec, arrays, slices with configurable length prefixes - 🔤 String handling: UTF-8 strings, C strings (
CStr), and byte slices - 💾 Zero-copy decoding with borrowed data (
'encodedlifetime) - 🎯 Type-safe enums with discriminant encoding
- 🔧 Flexible attribute-based customization with inline codec syntax
- 🎨 Nested structures including boxed types for recursive data
Quick Start
Add to your Cargo.toml:
[]
= "0.0"
Basic Usage
use ;
Advanced Examples
Zero-Copy Borrowed Data
use ;
use CStr;
Enums with Discriminants
use ;
Recursive Structures
use ;
use CString;
Inline Codec Syntax
use ;
Attribute Syntax
The #[byten(...)] attribute supports a flexible syntax for customizing encoding:
- Endianness:
$be(big-endian),$le(little-endian) - Variable-length:
$uvarbe(variable-length unsigned, big-endian) - Collections:
$vec(T)[Length],$arr[T] - Bytes:
$bytes[Length]for raw byte slices - Strings:
$utf8for UTF-8 strings,CStrfor C strings - Ownership:
$ownto decode into owned data (e.g.,String,Vec) - Optional:
$optforOption<T>types with presence byte - Remaining:
..to consume rest of input - Custom:
{ expr }for custom codec expressions
Features Flags
derive(default): Enable derive macros forEncode,Decode, andMeasureanyhow(default): Integration with theanyhowerror handling crate
Examples
The byten/examples directory contains several complete examples:
array.rs: Encoding arrays with variable-length integersborrowed.rs: Zero-copy decoding with borrowed data and lifetimesarchive.rs: Recursive structures (file system directory tree)icmp.rs: Network packet encoding (ICMP header)inline.rs: Using the inlinebyten!()macro for ad-hoc codecs
Run examples with:
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! This project is in early development.