byteable_derive
Procedural derive macro for the byteable crate.
This crate provides the #[derive(Byteable)] macro. You almost certainly want to depend on
byteable directly (which re-exports this macro via the derive feature) rather than adding
byteable_derive as a direct dependency.
[]
= "0.31" # includes #[derive(Byteable)] by default
What #[derive(Byteable)] generates
Applying the macro to a type generates byte-serialization trait impls automatically. The exact traits depend on the type and attributes used:
| Type / attribute | Generated traits |
|---|---|
| Struct (default) | RawRepr, FromRawRepr/TryFromRawRepr, IntoByteArray, FromByteArray/TryFromByteArray |
Struct #[byteable(io_only)] |
Readable, Writable |
| Unit enum | TryFromRawRepr, IntoByteArray, TryFromByteArray |
| Field enum | Readable, Writable |
Attributes
Struct / enum level
| Attribute | Effect |
|---|---|
#[byteable(little_endian)] |
All multi-byte fields use little-endian representation |
#[byteable(big_endian)] |
All multi-byte fields use big-endian representation |
#[byteable(io_only)] |
Generate Readable/Writable instead of fixed-size traits |
Field level
| Attribute | Effect |
|---|---|
#[byteable(little_endian)] |
This field uses little-endian (overrides struct-level) |
#[byteable(big_endian)] |
This field uses big-endian (overrides struct-level) |
#[byteable(try_transparent)] |
Field decode may fail; struct impl becomes TryFromRawRepr |
Examples
Fixed-size struct
use ;
let p = Point ;
let bytes: = p.into_byte_array;
let p2 = try_from_byte_array.unwrap;
assert_eq!;
Endianness control
use Byteable;
Dynamic struct (io_only)
use ;
use ;
Unit enum
use ;
// Auto-selected repr: u8 (3 variants fits in 1 byte)
assert_eq!;
let bytes = Green.into_byte_array;
assert_eq!;
Field enum
use ;
use ;
License
MIT — see LICENSE.