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:

This crate is experimental: the wire format and API are still settling, and breaking changes should be expected between releases. Pin an exact version if you depend on wire-format stability, or use #[byteable(fingerprint = ..)] (see WireFingerprint) to catch drift at compile time.

§Quick Start

§Fixed-size struct

use byteable::{Byteable, ToByteArray, 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.to_byte_array();
let p2 = Point3D::try_from_byte_array(bytes).unwrap();
assert_eq!(p.x, p2.x);

§Dynamic struct (I/O streaming)

This example needs the std feature (for byteable::io and String) on top of derive; the body is a no-op without it, since a no_std build (e.g. embedded-io alone) can’t compile a std::io-based example at all.

use byteable::Byteable;
use byteable::io::{Writable, Readable, 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
stdyesio::Readable / io::Writable I/O traits and std type impls
tokionoAsync I/O traits via tokio
ordered-floatnoImpls for OrderedFloat<T> and NotNan<T>
bitflagsnoimpl_bitflags!/impl_bitflags_endian! opt-in macros for bitflags::Flags types
heaplessnoio_only support for heapless’s fixed-capacity collections, on every enabled I/O pipeline
arrayvecnoio_only support for arrayvec::ArrayVec/ArrayString, on every enabled I/O pipeline
tinyvecnoio_only support for tinyvec::ArrayVec, on every enabled I/O pipeline
defmtnodefmt::Format impls for this crate’s own error/wrapper types (DecodeError, LittleEndian<T>, BigEndian<T>, eio::EioReadableError<E>, eio::EioReadExactError<E>)
allocno (implied by std)alloc-backed collection types over the eio and eio_async modules
embedded-ionoeio module: embedded-io-based (sync) I/O traits for no_std targets
embedded-io-asyncnoeio_async module: embedded-io-async-based (async) I/O traits for no_std targets
allnoAll of the above

Re-exports§

pub use byteable_trait::*;

Modules§

byteable_trait
ext
Support for third-party crates, grouped by crate.
io

Structs§

FingerprintBuilder
Builds a WireFingerprint value by folding a type’s wire shape into an FNV-1a hash.

Enums§

Constraint
UnicodeScalar and Utf8 are not redundant: UnicodeScalar is a value-range check on one fixed-width integer (paired with FixedInt, for char); Utf8 is a grammar check over a variable-length byte sequence (paired with Sequence, for String/str).
Endianness
The byte order of a multi-byte scalar on the wire.
FingerprintTag
What kind of thing a fingerprint describes - the first value folded into every hash.
Signedness
Whether a fixed-width integer (or an enum discriminant) is signed.

Traits§

WireFingerprint
A type’s wire shape, as a compile-time-computed fingerprint. See the module docs.

Derive Macros§

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