#endian #byteorder #wrapper #no-std

no-std endian-type

Type safe wrappers for types with a defined byte order

4 releases

0.2.0 Mar 6, 2023
0.1.2 Jul 28, 2015
0.1.1 Jul 27, 2015
0.1.0 Jul 27, 2015

#1657 in Encoding

Download history 195927/week @ 2024-03-14 197846/week @ 2024-03-21 187524/week @ 2024-03-28 189896/week @ 2024-04-04 181904/week @ 2024-04-11 186113/week @ 2024-04-18 156569/week @ 2024-04-25 148602/week @ 2024-05-02 151289/week @ 2024-05-09 158754/week @ 2024-05-16 153207/week @ 2024-05-23 153043/week @ 2024-05-30 151942/week @ 2024-06-06 148791/week @ 2024-06-13 150245/week @ 2024-06-20 118616/week @ 2024-06-27

597,755 downloads per month
Used in 458 crates (2 directly)

MIT license

14KB
273 lines

endian-type

crates.io Documentation License

Type safe wrappers for types with a defined byte order.

Example

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

// The endianness reflects the type-safety of the declaration:
let foo = 0xbeef_u32;
let foo_be = BigEndian::from(foo);
assert_eq!(foo_be.to_bytes(), foo.to_be_bytes());
assert_eq!(foo_be.as_byte_slice(), &foo.to_be_bytes());

// One can convert back to the native representation using the `From`/`Into` traits:
assert_eq!(foo, foo_be.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](https://doc.rust-lang.org/core/mem/fn.transmute.html) and should not affect performance.
let foo = u128::MAX;
let foo_le = LittleEndian::from(foo);
assert_eq!(
    LittleEndian::<u128>::from_bytes(u128::from_ne_bytes(foo_le.to_bytes()).wrapping_add(1).to_ne_bytes()),
    LittleEndian::<u128>::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 = -0xdead_i32;
let foo_no = NetworkOrder::from(foo);
let foo_be = types::i32_be::from(foo);
assert_eq!(foo.to_be_bytes(), foo_no.to_bytes());
assert_eq!(foo.to_be_bytes(), foo_be.to_bytes());

No runtime deps