Expand description
Byte-level serialization and deserialization for Rust types.
byteable provides two complementary paths for working with binary data:
-
Fixed-size path — For types whose wire size is known at compile time. Derive
Byteableand get zero-copyIntoByteArray::into_byte_array/TryFromByteArray::try_from_byte_arraywith a compile-timeIntoByteArray::BYTE_SIZEconstant. The derive macro generates a#[repr(C, packed)]raw struct and usestransmute, so no heap allocation or per-field iteration is required. -
Dynamic path — For types that contain variable-length data (strings, vecs, maps). Add
#[byteable(io_only)]to deriveReadable/Writableinstead, which stream data through anystd::io::Read/std::io::Write(or the async tokio equivalents when thetokiofeature is enabled).
§Quick Start
§Fixed-size struct
use byteable::{Byteable, IntoByteArray, TryFromByteArray};
#[derive(Byteable)]
struct Point3D {
x: f32,
y: f32,
z: f32,
}
let p = Point3D { x: 1.0, y: 2.0, z: 3.0 };
let bytes: [u8; 12] = p.into_byte_array();
let p2 = Point3D::try_from_byte_array(bytes).unwrap();
assert_eq!(p.x, p2.x);§Dynamic struct (I/O streaming)
use byteable::{Byteable, Writable, Readable};
use byteable::io::{WriteValue, ReadValue};
#[derive(Byteable)]
#[byteable(io_only)]
struct Waypoint {
id: u32,
label: String,
}
let wp = Waypoint { id: 1, label: "home".into() };
let mut buf = Vec::new();
buf.write_value(&wp).unwrap();
let wp2 = std::io::Cursor::new(&buf).read_value::<Waypoint>().unwrap();
assert_eq!(wp.id, wp2.id);§Feature Flags
Re-exports§
pub use byteable_trait::*;pub use io::*;
Modules§
Derive Macros§
- Byteable
- Derive macro that generates byte-serialization impls for structs and enums.