AssociatedByteArray

Trait AssociatedByteArray 

Source
pub trait AssociatedByteArray {
    type ByteArray: ByteArray;

    const BYTE_SIZE: usize = <Self::ByteArray>::BYTE_SIZE;
}
Expand description

Associates a type with its byte array representation.

This trait defines the relationship between a Rust type and its corresponding byte array type. It serves as the foundation for byte-oriented serialization, providing the necessary type information for conversions.

§Associated Types

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

§Associated Constants

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

§Usage

This trait is typically not implemented directly. Instead, implement the higher-level traits IntoByteArray and FromByteArray, which require AssociatedByteArray as a supertrait, or use the #[derive(Byteable)] macro which implements all necessary traits automatically.

§Examples

use byteable::{AssociatedByteArray, IntoByteArray, FromByteArray};

// Primitive types implement AssociatedByteArray
assert_eq!(u32::BYTE_SIZE, 4);
assert_eq!(u64::BYTE_SIZE, 8);

// Arrays also implement it
assert_eq!(<[u16; 3]>::BYTE_SIZE, 6);

§With custom types using derive

use byteable::{Byteable, AssociatedByteArray};

#[derive(Byteable, Clone, Copy)]
struct Point {
    x: u8,
    y: u8,
}

// AssociatedByteArray is automatically implemented
assert_eq!(Point::BYTE_SIZE, 2);

Provided Associated Constants§

Source

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

Required Associated Types§

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 AssociatedByteArray for bool

Source§

impl AssociatedByteArray for char

Source§

impl AssociatedByteArray for f32

Source§

impl AssociatedByteArray for f64

Source§

impl AssociatedByteArray for i8

Source§

impl AssociatedByteArray for i16

Source§

impl AssociatedByteArray for i32

Source§

impl AssociatedByteArray for i64

Source§

impl AssociatedByteArray for i128

Source§

impl AssociatedByteArray for u8

Source§

impl AssociatedByteArray for u16

Source§

impl AssociatedByteArray for u32

Source§

impl AssociatedByteArray for u64

Source§

impl AssociatedByteArray for u128

Source§

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

Implementors§