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]whereNis the size of the type in bytes. This must implement theByteArraytrait.
§Associated Constants
BYTE_SIZE: The size of the type in bytes. This is automatically derived fromByteArray::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§
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.