endian-type

crates.io Documentation License

Type safe wrappers for types with a defined byte order.

Example

```rust use endian_type::{types, BigEndian, LittleEndian, NetworkOrder};

// The endianness reflects the type-safety of the declaration: let foo = 0xbeefu32; let foobe = BigEndian::from(foo); asserteq!(foobe.tobytes(), foo.tobebytes()); asserteq!(foobe.asbyteslice(), &foo.tobe_bytes());

// One can convert back to the native representation using the From/Into traits: asserteq!(foo, foobe.into());

// To operate on the wrapped types as if they are regular numbers, one has to // be explicit and switch between the byte representations: // Note: Internally, these are just transmutations and should not affect performance. let foo = u128::MAX; let foole = LittleEndian::from(foo); asserteq!( LittleEndian::::frombytes(u128::fromnebytes(foole.tobytes()).wrappingadd(1).tonebytes()), LittleEndian::::from(0) );

// We also have a couple of aliases to be used as helper. // // This will assert our NetworkOrder type is in accordance with the IETF RFC1700. let foo = -0xdeadi32; let foono = NetworkOrder::from(foo); let foobe = types::i32be::from(foo); asserteq!(foo.tobebytes(), foono.tobytes()); asserteq!(foo.tobebytes(), foobe.tobytes()); ```