Byteable
A Rust crate for zero-overhead, zero-copy serialization and deserialization of byte-oriented data.
byteable provides traits and utilities for seamless conversion between data structures and byte arrays, with full support for both synchronous and asynchronous I/O operations, and comprehensive endianness handling.
Features
ByteableTrait: Core trait for types that can be converted to/from byte arraysReadByteable&WriteByteable: Extension traits forstd::io::Readandstd::io::WriteAsyncReadByteable&AsyncWriteByteable: Async I/O support with tokio (optional)- Endianness Support:
BigEndian<T>andLittleEndian<T>wrappers for explicit byte order #[derive(UnsafeByteable)]: Procedural macro for automatic trait implementation (optional)- Extensive Documentation: Every function, trait, and type is thoroughly documented with examples
- Inline Comments: All implementations include detailed explanatory comments
- Zero Overhead: Compiles down to simple memory operations with no runtime cost
Why byteable?
- Binary Protocols: Perfect for implementing network protocols (TCP, UDP, custom formats)
- File I/O: Read/write binary file formats with ease
- Cross-Platform: Consistent behavior across different architectures with endianness control
- Type-Safe: Rust's type system ensures correctness at compile time
- No Dependencies: Core functionality has zero dependencies (tokio is optional)
Installation
Add byteable to your Cargo.toml:
[]
= "0.14" # Check crates.io for the latest version
Optional Features
[]
= { = "0.14", = ["derive", "tokio"] }
derive(default): Enables the#[derive(UnsafeByteable)]procedural macrotokio: Enables async I/O traits for use with tokio
Quick Start
Basic File I/O Example
use ;
use File;
Network Protocol Example
use ;
let header = TcpHeader ;
// Convert to bytes for transmission
let bytes = header.as_byte_array;
Async I/O with Tokio
use ;
use TcpStream;
async
Usage Patterns
Working with Different Endianness
use ;
Reading Multiple Values
use ReadByteable;
use Cursor;
let data = vec!;
let mut reader = new;
let header: u32 = reader.read_byteable?;
let length: u16 = reader.read_byteable?;
let checksum: u32 = reader.read_byteable?;
Custom Byteable Implementation
For types that need special handling, you can use the impl_byteable_via! macro:
use ;
// Raw representation (for byte conversion)
// User-friendly representation
// Implement conversions
// Now Point implements Byteable via PointRaw
impl_byteable_via!;
Safety Considerations
The #[derive(UnsafeByteable)] macro uses unsafe code (std::mem::transmute) internally. When using it, you must ensure:
Safe to Use With:
- Primitive numeric types (
u8,i32,f64, etc.) BigEndian<T>andLittleEndian<T>wrappers- Arrays of safe types
- Structs with
#[repr(C, packed)]or#[repr(transparent)]
Never Use With:
bool,char, or enums (have invalid bit patterns)String,Vec, or any heap-allocated types- References or pointers (
&T,Box<T>,*const T) - Types with
Dropimplementations NonZero*types or types with invariants
Requirements:
- Explicit memory layout: Always use
#[repr(C, packed)]or similar - All byte patterns valid: Every possible byte combination must be valid for your type
- No padding with undefined values: Use
packedto avoid alignment padding - No drop glue: Types must be
Copyand have no cleanup logic
Documentation
The crate includes extensive documentation:
- API Documentation: Every trait, type, and function is documented with examples
- Inline Comments: All implementations include explanatory comments
- Safety Guidelines: Clear warnings about unsafe usage
- Examples: Multiple real-world usage examples in the
examples/directory
Generate and view the documentation locally:
See Also
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Acknowledgments
Built with ❤️ for the Rust community.