Skip to main content

Crate byteable

Crate byteable 

Source
Expand description

Byte-level serialization and deserialization for Rust types.

byteable provides two complementary paths for working with binary data:

§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

FeatureDefaultDescription
deriveyes#[derive(Byteable)] proc-macro
stdyesReadable / Writable I/O traits and std type impls
tokionoAsync I/O traits via tokio
ordered-floatnoImpls for OrderedFloat<T> and NotNan<T>
allnoAll of the above

Re-exports§

pub use byteable_trait::*;
pub use io::*;

Modules§

byteable_trait
io

Derive Macros§

Byteable
Derive macro that generates byte-serialization impls for structs and enums.