| Crates.io | byteable_derive |
| lib.rs | byteable_derive |
| version | 0.12.0 |
| created_at | 2025-09-29 08:46:07.950461+00 |
| updated_at | 2026-01-08 23:46:03.148156+00 |
| description | Custom derive macros for the byteable crate. |
| homepage | |
| repository | https://github.com/PaulDepping/byteable |
| max_upload_size | |
| id | 1859237 |
| size | 55,488 |
byteable_deriveThis crate provides custom derive macros for the byteable crate.
#[derive(Byteable)]The main derive macro that automatically implements byte conversion traits for your types. Supports:
transparent and try_transparent attributesuse byteable::Byteable;
#[derive(Byteable, Clone, Copy)]
struct Point {
x: i32,
y: i32,
}
use byteable::Byteable;
#[derive(Byteable, Clone, Copy)]
struct NetworkPacket {
version: u8,
#[byteable(big_endian)]
length: u16,
#[byteable(little_endian)]
checksum: u32,
}
#[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 nested Byteable types)#[byteable(try_transparent)] - Use field's raw representation with fallible conversionThe #[derive(Byteable)] macro supports C-like enums with explicit discriminants.
use byteable::Byteable;
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
enum Status {
Idle = 0,
Running = 1,
Completed = 2,
Failed = 3,
}
Enums support type-level endianness attributes:
use byteable::Byteable;
// Little-endian (for file formats)
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u16)]
#[byteable(little_endian)]
enum FileType {
Text = 0x1000,
Binary = 0x2000,
Archive = 0x3000,
}
// Big-endian (for network protocols)
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u32)]
#[byteable(big_endian)]
enum HttpStatus {
Ok = 200,
NotFound = 404,
InternalError = 500,
}
When deriving Byteable for enums, you must:
#[repr(u8)], #[repr(u16)], #[repr(u32)], #[repr(u64)],
#[repr(i8)], #[repr(i16)], #[repr(i32)], or #[repr(i64)]TryFromByteArray for deserialization (returns EnumFromBytesError for invalid discriminants)#[byteable(big_endian)] - Store enum discriminant in big-endian byte order#[byteable(little_endian)] - Store enum discriminant in little-endian byte orderEnums use fallible conversion because not all byte patterns represent valid enum variants:
use byteable::{Byteable, TryFromByteArray};
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
enum Command {
Start = 1,
Stop = 2,
}
fn example() -> Result<(), byteable::EnumFromBytesError> {
// Valid conversion
let bytes = [1];
let cmd = Command::try_from_byte_array(bytes)?;
assert_eq!(cmd, Command::Start);
// Invalid discriminant returns error
let invalid = [255];
let result = Command::try_from_byte_array(invalid);
assert!(result.is_err());
Ok(())
}
use byteable::Byteable;
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
enum MessageType {
Data = 1,
Control = 2,
Error = 3,
}
#[derive(Byteable, Clone, Copy)]
struct Message {
#[byteable(try_transparent)]
msg_type: MessageType,
#[byteable(big_endian)]
sequence: u32,
payload: [u8; 16],
}
Enums with non-sequential discriminants work perfectly:
use byteable::Byteable;
#[derive(Byteable, Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
enum Priority {
Low = 1,
Medium = 5,
High = 10,
Critical = 100,
}
// Only defined discriminants (1, 5, 10, 100) are valid
// All other values return errors during conversion
For comprehensive documentation and examples, see the main byteable crate documentation.