EndianConvert

Trait EndianConvert 

Source
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§

Source

type ByteArray: ByteArray

The byte array type used to represent this type.

Required Methods§

Source

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

Creates a value from its little-endian byte representation.

Source

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

Creates a value from its big-endian byte representation.

Source

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

Creates a value from its native-endian byte representation.

Source

fn to_le_bytes(self) -> Self::ByteArray

Returns the little-endian byte representation of this value.

Source

fn to_be_bytes(self) -> Self::ByteArray

Returns the big-endian byte representation of this value.

Source

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.

Implementations on Foreign Types§

Source§

impl EndianConvert for f32

Source§

type ByteArray = [u8; 4]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for f64

Source§

type ByteArray = [u8; 8]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for i8

Source§

type ByteArray = [u8; 1]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for i16

Source§

type ByteArray = [u8; 2]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for i32

Source§

type ByteArray = [u8; 4]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for i64

Source§

type ByteArray = [u8; 8]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for i128

Source§

type ByteArray = [u8; 16]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for u8

Source§

type ByteArray = [u8; 1]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for u16

Source§

type ByteArray = [u8; 2]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for u32

Source§

type ByteArray = [u8; 4]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for u64

Source§

type ByteArray = [u8; 8]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Source§

impl EndianConvert for u128

Source§

type ByteArray = [u8; 16]

Source§

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

Source§

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

Source§

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

Source§

fn to_ne_bytes(self) -> Self::ByteArray

Source§

fn to_le_bytes(self) -> Self::ByteArray

Source§

fn to_be_bytes(self) -> Self::ByteArray

Implementors§