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:
T for[Length],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:
?forOption<T>types with presence byte - Boxing:
boxforBox<T>types - Phantom:
= exprfor constant values with zero bytes - Remaining:
..to consume rest of input - Tuples:
(a, b, ...N)as built-in codecs for N sized tuples - Custom:
{ expr }for custom codec expressions
Features Flags
std(default): Enable standard library support. Disable forno_stdenvironments withdefault-features = falsealloc(default viastd): Enable types that require allocation (Vec,Box,String). Can be used inno_stdwith an allocator.anyhow(default): Integration with theanyhowerror handling crate (requiresstd)derive(default): Enable derive macros for self-coded traits. Works in all modes (core-only, alloc, std).
Using in no_std Environments
Without an allocator (core only)
For embedded systems without an allocator, use only core types:
[]
= { = "0.0", = false }
This provides support for:
- Primitive types (
u8,i8,bool, etc.) - Arrays and slices
- Borrowed data (
&str,&[u8],&CStr) - Endian conversion
- Fixed-size types
With derive macros:
[]
= { = "0.0", = false, = ["derive"] }
Note: When using derive macros in core-only mode, avoid using Vec, Box, String, or other allocation-dependent types in your structs.
With an allocator (core + alloc)
For no_std environments with an allocator:
[]
= { = "0.0", = false, = ["alloc"] }
This adds support for:
Vec<T>collectionsBox<T>heap allocation- Owned strings (
String) - Variable-length encoding (
UVarBECodec)
With derive macros (recommended for most use cases):
[]
= { = "0.0", = false, = ["alloc", "derive"] }
Note: The derive feature works in all modes (core-only, alloc, and std). The generated code will only use features that are enabled.
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)nostd.rs: Simple FS structures forno_stdenvironmentsinline.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! Please read our Contributing Guidelines for details on how to submit pull requests, report issues, and contribute to the project.
This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.