#endian #wrapper #integer #arithmetic #comparison #explicitly #standard

no-std storage_endian

Simple integer wrappers for explicitly defining storage endianess

1 unstable release

0.1.0 Nov 30, 2023

#183 in No standard library

Download history 1/week @ 2023-12-24 4/week @ 2024-02-18 11/week @ 2024-02-25 4/week @ 2024-03-03 5/week @ 2024-03-10 1/week @ 2024-03-17 52/week @ 2024-03-24 18/week @ 2024-03-31

77 downloads per month
Used in ubus

BSD-2-Clause

20KB
294 lines

storage_endian

Simple integer wrappers for explicitly defining storage endianess.

The wrappers provide comparison, arithmetic, and conversion using standard Rust traits.

I've ended up writing bits of this crate several times on various projects, so I decided it's time to make it a crate! :)

Example Usage

use storage_endian::{BEu32, BEu64};

#[repr(C)]
struct Data {
    magic: BEu32,
    version: BEu32,
    size: BEu64,
    thing: BEu64,
}
impl Data {
    pub const SIZE: usize = core::mem::size_of::<Self>();
    pub const MAGIC: u32 = 0x1337_beef;

    fn handle_thing(thing: u64) {
        // ...
    }

    pub fn from_bytes(data: [u8; Self::SIZE]) -> Self {
        let mut data: Self = unsafe { core::mem::transmute(data) };

        assert_eq!(data.magic, Self::MAGIC);
        assert_eq!((data.version >> 16) & 0xff, 0x01);
        assert!(data.size >= Self::SIZE as u64);
        Self::handle_thing(data.thing.into());

        data
    }
}

As you can see, most of the time you don't have to worry what endianess the underlying data is, the operator overloading handles it for you.

To avoid bugs, there is intentionally no easy way to access the data in the underlying representation.

Alternatives

There are various other solutions to manage endian flipping in Rust, you might be interested in using:

For one reason or another these haven't worked perfectly for some of my use cases, but they might work for others!

No runtime deps