byteable_derive
This crate provides custom derive macros for the byteable crate.
Available Derives
#[derive(Byteable)]
The main derive macro that automatically implements byte conversion traits for your types. Supports:
- Structs (named, tuple, and unit structs)
- Enums (C-like enums with explicit discriminants)
- Endianness control via field and type-level attributes
- Nested byteable types via
transparentandtry_transparentattributes
Struct Support
Basic Struct
use Byteable;
Struct with Endianness
use Byteable;
Field Attributes
#[byteable(big_endian)]- Store field in big-endian byte order#[byteable(little_endian)]- Store field in little-endian byte order#[byteable(transparent)]- Use field's raw representation (for nestedByteabletypes)#[byteable(try_transparent)]- Use field's raw representation with fallible conversion
Enum Support
The #[derive(Byteable)] macro supports C-like enums with explicit discriminants.
Basic Enum
use Byteable;
Enum with Endianness
Enums support type-level endianness attributes:
use Byteable;
// Little-endian (for file formats)
// Big-endian (for network protocols)
Enum Requirements
When deriving Byteable for enums, you must:
- Use an explicit repr type:
#[repr(u8)],#[repr(u16)],#[repr(u32)],#[repr(u64)],#[repr(i8)],#[repr(i16)],#[repr(i32)], or#[repr(i64)] - Have only unit variants (no fields)
- Provide explicit discriminant values for all variants
- Use
TryFromByteArrayfor deserialization (returnsInvalidDiscriminantErrorfor invalid discriminants)
Type-Level Attributes for Enums
#[byteable(big_endian)]- Store enum discriminant in big-endian byte order#[byteable(little_endian)]- Store enum discriminant in little-endian byte order- No attribute - Use native endianness
Error Handling
Enums use fallible conversion because not all byte patterns represent valid enum variants:
use ;
Advanced Usage
Nested Structs with Enums
use Byteable;
Sparse Discriminants
Enums with non-sequential discriminants work perfectly:
use Byteable;
// Only defined discriminants (1, 5, 10, 100) are valid
// All other values return errors during conversion
See Also
For comprehensive documentation and examples, see the main byteable crate documentation.