Crate yael

Crate yael 

Source
Expand description

“Yet another endianness library”

This library is intended for uses where the data is not deserialized but already in ROM/RAM.

The library is no_std, has no features and no dependencies.

The conversion is made explicit by calling new, get and set.

Default is provided because 0 is identical for both byte orders.

Debug is provided because it can be useful and is expected to not be fast.

Operations which do not depend on the byte order can be performed on integers: eq, and, or, xor, not.

But please note that a operation with a constant may be more efficient with getting the value instead of wrapping the constant with new, though depending on the constant and cpu.

There are several similar libraries, but they all differ in a some points.

Some alternatives:

§Examples

#[repr(C)]
struct Chunk {
    size: u32be,
    checksum: u32be,
}
let chunk = Chunk { size: u32be::new(55), checksum: u32be::new(0x52f0e743) };
let chunk_size = chunk.size.get();
#[repr(C)]
struct Data {
    mode: u16be,
    flags: u16be,
}

fn fix_flags1(data: &mut Data){
    // compare with a constant big-endian number, no conversion performed, can also be written as `data.mode.is_zero()`
    if data.mode == u16be::new(0) {
        // bit operations: also no conversion performed
        data.flags |= u16be::new(1);
    }
}

fn fix_flags2(data: &mut Data, flags_on_error: u16){
    // while these get and set do perform conversion
    if data.mode.get() > 80 {
        data.flags.set(flags_on_error);
    }
}

The #[repr(C)] is not required by the examples, but probably in many use cases.

§Floating Point

The floating point types do not support bit operations because a float does not either.

Comparison is not supported because it would differ from how a float behaves.

(+0 == -0 float: equal, bits: different; nan == nan float: different, bits: equal)

Structs§

f32be
The 32-bit big endian floating-point type.
f32le
The 32-bit little endian floating-point type.
f64be
The 64-bit big endian floating-point type.
f64le
The 64-bit little endian floating-point type.
i16be
The 16-bit big endian signed integer type.
i16le
The 16-bit little endian signed integer type.
i32be
The 32-bit big endian signed integer type.
i32le
The 32-bit little endian signed integer type.
i64be
The 64-bit big endian signed integer type.
i64le
The 64-bit little endian signed integer type.
i128be
The 128-bit big endian signed integer type.
i128le
The 128-bit little endian signed integer type.
u16be
The 16-bit big endian unsigned integer type.
u16le
The 16-bit little endian unsigned integer type.
u32be
The 32-bit big endian unsigned integer type.
u32le
The 32-bit little endian unsigned integer type.
u64be
The 64-bit big endian unsigned integer type.
u64le
The 64-bit little endian unsigned integer type.
u128be
The 128-bit big endian unsigned integer type.
u128le
The 128-bit little endian unsigned integer type.