pub trait Representable {
type Representation: ByteArray;
// Required methods
fn from_le_bytes(bytes: &Self::Representation) -> Self;
fn from_be_bytes(bytes: &Self::Representation) -> Self;
fn to_le_bytes(&self) -> Self::Representation;
fn to_be_bytes(&self) -> Self::Representation;
}Expand description
A trait for types that can be represented as a fixed-size byte array.
This trait is implemented for all primitive integer types (u8-u128, i8-i128)
and floating-point types (f32, f64). It provides methods for converting between
the type and its byte representation in both little-endian and big-endian formats.
§Examples
use futures_byteorder::Representable;
let value: u16 = 0x1234;
let le_bytes = value.to_le_bytes();
let be_bytes = value.to_be_bytes();
assert_eq!(le_bytes, [0x34, 0x12]);
assert_eq!(be_bytes, [0x12, 0x34]);
assert_eq!(u16::from_le_bytes(le_bytes), value);
assert_eq!(u16::from_be_bytes(be_bytes), value);Required Associated Types§
Sourcetype Representation: ByteArray
type Representation: ByteArray
The byte array type that represents this type.
For example, u16::Representation is [u8; 2], and u64::Representation is [u8; 8].
Required Methods§
Sourcefn from_le_bytes(bytes: &Self::Representation) -> Self
fn from_le_bytes(bytes: &Self::Representation) -> Self
Creates a value from its little-endian byte representation.
Sourcefn from_be_bytes(bytes: &Self::Representation) -> Self
fn from_be_bytes(bytes: &Self::Representation) -> Self
Creates a value from its big-endian byte representation
Sourcefn to_le_bytes(&self) -> Self::Representation
fn to_le_bytes(&self) -> Self::Representation
Returns the little-endian byte representation of this value.
Sourcefn to_be_bytes(&self) -> Self::Representation
fn to_be_bytes(&self) -> Self::Representation
Returns the big-endian byte representation of this value.
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.