15 releases
new 0.4.1 | Sep 24, 2023 |
---|---|
0.4.0 | Aug 30, 2022 |
0.3.6 | Dec 18, 2021 |
0.3.4 | Nov 24, 2021 |
0.3.1 | Jul 6, 2021 |
#142 in Encoding
644,734 downloads per month
Used in 220 crates
(2 directly)
46KB
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 throughbytecheck
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
~180KB