Byteable

Trait Byteable 

Source
pub trait Byteable {
    type ByteArray: ByteArray;

    const BYTE_SIZE: usize = <Self::ByteArray>::BYTE_SIZE;

    // Required methods
    fn to_byte_array(self) -> Self::ByteArray;
    fn from_byte_array(byte_array: Self::ByteArray) -> Self;
}
Expand description

A trait for types that can be converted to and from a byte array.

The Byteable trait provides a zero-overhead, zero-copy conversion between Rust types and their byte representations. This is particularly useful for:

  • Binary file I/O
  • Network protocols
  • Low-level system programming
  • Memory-mapped files
  • Interfacing with C libraries

§Associated Types

  • ByteArray: The type of the byte array representation. Usually [u8; N] where N is the size of the type in bytes.

§Associated Constants

  • BYTE_SIZE: The size of the type in bytes. This is automatically derived from ByteArray::BYTE_SIZE.

§Methods

  • to_byte_array: Converts the value into its byte array representation
  • from_byte_array: Constructs a value from its byte array representation

§Safety

While the trait itself is safe, implementations often use unsafe code internally (particularly via the #[derive(Byteable)] macro). The derive macro automatically handles memory layout and endianness conversion. Users must ensure:

  1. All fields are primitive types or use endianness attributes
  2. All byte patterns are valid for the field types
  3. No types with invalid bit patterns are used (e.g., bool, char, enums)

§Implementations

The trait is implemented for:

  • All primitive numeric types (u8, i32, f64, etc.)
  • Fixed-size arrays of Byteable types
  • BigEndian<T> and LittleEndian<T> wrappers
  • Custom types via #[derive(Byteable)] with automatic endianness handling

§Examples

§Using primitive types

use byteable::Byteable;

let value: u32 = 0x12345678;
let bytes = value.to_byte_array();

// On little-endian systems
#[cfg(target_endian = "little")]
assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);

let restored = u32::from_byte_array(bytes);
assert_eq!(restored, value);

§Using with custom types

use byteable::Byteable;

#[derive(Byteable, Debug, PartialEq, Clone, Copy)]
struct Color {
    r: u8,
    g: u8,
    b: u8,
    a: u8,
}

let color = Color { r: 255, g: 128, b: 64, a: 255 };
let bytes = color.to_byte_array();
assert_eq!(bytes, [255, 128, 64, 255]);

let restored = Color::from_byte_array(bytes);
assert_eq!(restored, color);

§Using with arrays

use byteable::Byteable;

let values: [u16; 3] = [1, 2, 3];
let byte_array = values.to_byte_array();

// byte_array is [[u8; 2]; 3] - array of byte arrays
let restored = <[u16; 3]>::from_byte_array(byte_array);
assert_eq!(restored, values);

Provided Associated Constants§

Source

const BYTE_SIZE: usize = <Self::ByteArray>::BYTE_SIZE

The size of this type in bytes.

This is automatically computed from the associated ByteArray type.

Required Associated Types§

Source

type ByteArray: ByteArray

The byte array type used to represent this type.

Typically this is [u8; N] where N is size_of::<Self>().

Required Methods§

Source

fn to_byte_array(self) -> Self::ByteArray

Converts this value into a byte array.

§Examples
use byteable::Byteable;

let value: u16 = 0x1234;
let bytes = value.to_byte_array();
Source

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Constructs a value from a byte array.

§Examples
use byteable::Byteable;

let bytes = [0x34, 0x12];
let value = u16::from_byte_array(bytes);
#[cfg(target_endian = "little")]
assert_eq!(value, 0x1234);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Byteable for f32

Source§

type ByteArray = [u8; 4]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for f64

Source§

type ByteArray = [u8; 8]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for i8

Source§

type ByteArray = [u8; 1]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for i16

Source§

type ByteArray = [u8; 2]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for i32

Source§

type ByteArray = [u8; 4]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for i64

Source§

type ByteArray = [u8; 8]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for i128

Source§

type ByteArray = [u8; 16]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for u8

Source§

type ByteArray = [u8; 1]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for u16

Source§

type ByteArray = [u8; 2]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for u32

Source§

type ByteArray = [u8; 4]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for u64

Source§

type ByteArray = [u8; 8]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl Byteable for u128

Source§

type ByteArray = [u8; 16]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Source§

impl<T: Byteable, const SIZE: usize> Byteable for [T; SIZE]

Source§

type ByteArray = [<T as Byteable>::ByteArray; SIZE]

Source§

fn to_byte_array(self) -> Self::ByteArray

Source§

fn from_byte_array(byte_array: Self::ByteArray) -> Self

Implementors§