EndianConvert

Trait EndianConvert 

Source
pub trait EndianConvert:
    Copy
    + AssociatedByteArray
    + IntoByteArray
    + FromByteArray {
    // Required methods
    fn from_le(value: Self) -> Self;
    fn from_be(value: Self) -> Self;
    fn to_le(self) -> Self;
    fn to_be(self) -> Self;
}
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 Methods§

Source

fn from_le(value: Self) -> Self

Creates a value from its little-endian representation.

Source

fn from_be(value: Self) -> Self

Creates a value from its big-endian representation.

Source

fn to_le(self) -> Self

Converts to little-endian representation.

Source

fn to_be(self) -> Self

Converts to big-endian representation.

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§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for f64

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for i8

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for i16

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for i32

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for i64

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for i128

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for u8

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for u16

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for u32

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for u64

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Source§

impl EndianConvert for u128

Source§

fn from_le(value: Self) -> Self

Source§

fn from_be(value: Self) -> Self

Source§

fn to_le(self) -> Self

Source§

fn to_be(self) -> Self

Implementors§