Crates.io | intsplit |
lib.rs | intsplit |
version | 0.1.0 |
source | src |
created_at | 2024-05-21 21:36:47.22581 |
updated_at | 2024-05-21 21:36:47.22581 |
description | Rust library for splitting numeric types into their binary component arrays |
homepage | |
repository | https://github.com/AlexanderSchuetz97/intsplit |
max_upload_size | |
id | 1247233 |
size | 17,234 |
Rust library for splitting numeric types into their binary component arrays.
use intsplit::*;
#[test]
fn example() {
let number : u32 = 0x0A0B0C0Du32;
let u16_components: [u16; 2] = number.as_u16_array();
assert_eq!([0x0C0Du16, 0x0A0Bu16], u16_components);
}
This library uses no unsafe code (only in tests) to implement the transmutes. The rust compiler will recognize that all these operations are essentially transmutes and optimize similarly (on optimized builds)
as_i8_array -> [i8; 8]
as_i16_array -> [i16; 4]
as_u16_array -> [u16; 4]
as_i32_array -> [i32; 2]
as_u32_array -> [u32; 2]
as_f32_array -> [f32; 2]
Please note that the result is always equivalent to transmuting the to_ne_bytes() result to the respective array. The example provided in this readme will not work on big endian targets as that would swap the 2 u16 values in the assert statement, just like mem::transmute - ing the result of to_ne_bytes() would not yield the same result on little and big endian.