Crate endian_cast

Source
Expand description

This crate provides a convenient EndianCast type and a trait Endianness for converting between little-endian and big-endian byte representations of various data types, including a way to cast to the opposite endianness.

§EndianCast

The EndianCast type is a wrapper around a value of type T allowing you to convert it into little-endian, big-endian, native-endian, or opposite-endian byte arrays, even within const contexts. When instantiating EndianCast, you must specify the size of the type T as a const generic. Providing an incorrect size will result in a panic at compile time.

When the system endianness matches the requested endianness, the conversion is essentially a no-op, and the bytes are returned as-is. When the system endianness does not match the requested endianness, the bytes are reversed.

The EndianCast interface is provided as a way to offer the same functionality as the Endianness trait within const contexts, as the trait methods cannot be used in const contexts.

§Example

use endian_cast::EndianCast;

let x = 0x12345678u32;
let le_bytes = EndianCast::<4, u32>(x).le_bytes();
let be_bytes = EndianCast::<4, u32>(x).be_bytes();
let op_bytes = EndianCast::<4, u32>(x).op_bytes();
let ne_bytes = *EndianCast::<4, u32>(x).ne_bytes();

assert_ne!(le_bytes, be_bytes);
assert_ne!(ne_bytes, op_bytes);
assert_eq!(le_bytes, [0x78, 0x56, 0x34, 0x12]);
assert_eq!(be_bytes, [0x12, 0x34, 0x56, 0x78]);
assert_eq!(ne_bytes, [0x78, 0x56, 0x34, 0x12]);

// usable in const contexts
const LE: [u8; 4] = EndianCast::<4, u32>(0x12345678).le_bytes();

§Endianness

The Endianness trait provides methods for converting to and from the byte representation of the type implementing the trait. It is automatically implemented for all primitive types and arrays of bytes and can easily be implemented on arbitrary types that implement Sized and Copy by manually implementing the trait. As with EndianCast, you must specify the size of the type as a const generic, and providing an incorrect size will result in a panic at compile time.

When the system endianness matches the requested endianness, the conversion is essentially a no-op, and the bytes are returned as-is. When the system endianness does not match the requested endianness, the bytes are reversed.

§Example

use endian_cast::Endianness;

let x = 0x12345678u32;
let le_bytes = x.le_bytes();
let be_bytes = x.be_bytes();
let op_bytes = x.op_bytes();
let ne_bytes = *x.ne_bytes();
assert_ne!(le_bytes, be_bytes);
assert_ne!(ne_bytes, op_bytes);
assert_eq!(le_bytes, [0x78, 0x56, 0x34, 0x12]);
assert_eq!(be_bytes, [0x12, 0x34, 0x56, 0x78]);
assert_eq!(ne_bytes, [0x78, 0x56, 0x34, 0x12]);

Structs§

EndianCast
The EndianCast type is a wrapper around a value of type T allowing you to convert it into little-endian, big-endian, native-endian, or opposite-endian byte arrays, even within const contexts. When instantiating EndianCast, you must specify the size of the type T as a const generic. Providing an incorrect size will result in a panic at compile time.

Traits§

Endianness
The Endianness trait provides methods for converting to and from the byte representation of the type implementing the trait. It is automatically implemented for all primitive types and arrays of bytes and can easily be implemented on arbitrary types that implement Sized and Copy by manually implementing the trait. As with EndianCast, you must specify the size of the type as a const generic, and providing an incorrect size will result in a panic at compile time.

Functions§

reverse