Skip to content

A Rust crate that provides generic functions and traits for accessing big-endian and little-endian versions of data types with a cheap abstraction that is zero-cost when the platform endianness matches the desired endianness.

License

Notifications You must be signed in to change notification settings

sam0x17/endian-cast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

endian-cast

Crates.io docs.rs Build Status MIT License

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].into());
assert_eq!(be_bytes, [0x12, 0x34, 0x56, 0x78].into());
assert_eq!(ne_bytes, [0x78, 0x56, 0x34, 0x12].into());

About

A Rust crate that provides generic functions and traits for accessing big-endian and little-endian versions of data types with a cheap abstraction that is zero-cost when the platform endianness matches the desired endianness.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages