#endian #no-std

no-std rend

Endian-aware primitives for Rust

9 releases

0.3.2 Aug 5, 2021
0.3.1 Jul 6, 2021
0.3.0 Jun 21, 2021
0.2.3 May 17, 2021
0.1.1 May 4, 2021

#486 in Encoding

Download history 4/week @ 2021-05-31 6/week @ 2021-06-07 3/week @ 2021-06-14 47/week @ 2021-06-21 26/week @ 2021-06-28 68/week @ 2021-07-05 541/week @ 2021-07-12 741/week @ 2021-07-19 535/week @ 2021-07-26 460/week @ 2021-08-02 653/week @ 2021-08-09 485/week @ 2021-08-16 603/week @ 2021-08-23 830/week @ 2021-08-30 1030/week @ 2021-09-06 1873/week @ 2021-09-13

805 downloads per month
Used in 6 crates (via rkyv)

MIT license

41KB
1K SLoC

rend is a library that provides endian-aware primitives for Rust.


rend in action

use rend::*;

let little_int = i32_le::new(0x12345678);
// Internal representation is little-endian
assert_eq!([0x78, 0x56, 0x34, 0x12], unsafe { ::core::mem::transmute::<_, [u8; 4]>(little_int) });

// Can also be made with `.into()`
let little_int: i32_le = 0x12345678.into();
// Still formats correctly
assert_eq!("305419896", format!("{}", little_int));
assert_eq!("0x12345678", format!("0x{:x}", little_int));

let big_int = i32_be::new(0x12345678);
// Internal representation is big-endian
assert_eq!([0x12, 0x34, 0x56, 0x78], unsafe { ::core::mem::transmute::<_, [u8; 4]>(big_int) });

// Can also be made with `.into()`
let big_int: i32_be = 0x12345678.into();
// Still formats correctly
assert_eq!("305419896", format!("{}", big_int));
assert_eq!("0x12345678", format!("0x{:x}", big_int));

lib.rs:

rend

rend is a library that provides endian-aware primitives for Rust.

It's similar in design to simple_endian, but has support for more builtin types such as atomics and nonzero integers. It also has support for const functions since it does not rely on traits.

rend does not provide endian-aware types for types that are inherently endian-agnostic, such as bool and u8. It does not provide endian-aware types for types that have an architecture-dependent size, such as isize and usize. It's also not extensible to custom types.

rend is intended to be used to build portable types that can be shared between different architectures, especially with zero-copy deserialization.

Features

  • std: Enables standard library support (enabled by default)
  • validation: Enables validation support through bytecheck

Example:

use rend::*;

let little_int = i32_le::new(0x12345678);
// Internal representation is little-endian
assert_eq!(
    [0x78, 0x56, 0x34, 0x12],
    unsafe { ::core::mem::transmute::<_, [u8; 4]>(little_int) }
);

// Can also be made with `.into()`
let little_int: i32_le = 0x12345678.into();
// Still formats correctly
assert_eq!("305419896", format!("{}", little_int));
assert_eq!("0x12345678", format!("0x{:x}", little_int));

let big_int = i32_be::new(0x12345678);
// Internal representation is big-endian
assert_eq!(
    [0x12, 0x34, 0x56, 0x78],
    unsafe { ::core::mem::transmute::<_, [u8; 4]>(big_int) }
);

// Can also be made with `.into()`
let big_int: i32_be = 0x12345678.into();
// Still formats correctly
assert_eq!("305419896", format!("{}", big_int));
assert_eq!("0x12345678", format!("0x{:x}", big_int));

Dependencies

~125KB