pub trait EndianConvert: Copy {
type ByteArray: ByteArray;
// Required methods
fn from_le_bytes(byte_array: Self::ByteArray) -> Self;
fn from_be_bytes(byte_array: Self::ByteArray) -> Self;
fn from_ne_bytes(byte_array: Self::ByteArray) -> Self;
fn to_le_bytes(self) -> Self::ByteArray;
fn to_be_bytes(self) -> Self::ByteArray;
fn to_ne_bytes(self) -> Self::ByteArray;
}Expand description
A trait for types that can be converted between different byte orders (endianness).
This trait provides methods for converting between little-endian, big-endian, and native-endian byte representations. It is implemented for all primitive numeric types.
§Implementations
This trait is automatically implemented for:
- Unsigned integers:
u8,u16,u32,u64,u128 - Signed integers:
i8,i16,i32,i64,i128 - Floating point:
f32,f64
§Examples
use byteable::EndianConvert;
let value = 0x12345678u32;
let le_bytes = value.to_le_bytes();
assert_eq!(le_bytes, [0x78, 0x56, 0x34, 0x12]);
let be_bytes = value.to_be_bytes();
assert_eq!(be_bytes, [0x12, 0x34, 0x56, 0x78]);
// Convert back
assert_eq!(u32::from_le_bytes(le_bytes), value);
assert_eq!(u32::from_be_bytes(be_bytes), value);Required Associated Types§
Required Methods§
Sourcefn from_le_bytes(byte_array: Self::ByteArray) -> Self
fn from_le_bytes(byte_array: Self::ByteArray) -> Self
Creates a value from its little-endian byte representation.
Sourcefn from_be_bytes(byte_array: Self::ByteArray) -> Self
fn from_be_bytes(byte_array: Self::ByteArray) -> Self
Creates a value from its big-endian byte representation.
Sourcefn from_ne_bytes(byte_array: Self::ByteArray) -> Self
fn from_ne_bytes(byte_array: Self::ByteArray) -> Self
Creates a value from its native-endian byte representation.
Sourcefn to_le_bytes(self) -> Self::ByteArray
fn to_le_bytes(self) -> Self::ByteArray
Returns the little-endian byte representation of this value.
Sourcefn to_be_bytes(self) -> Self::ByteArray
fn to_be_bytes(self) -> Self::ByteArray
Returns the big-endian byte representation of this value.
Sourcefn to_ne_bytes(self) -> Self::ByteArray
fn to_ne_bytes(self) -> Self::ByteArray
Returns the native-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.