irox_tools/primitives/
u32.rs1use crate::{ToF64, WrappingAdd, WrappingMul, WrappingSub};
6
7pub trait ToU32Array<const T: usize> {
10 fn to_u32_array(&self) -> [u32; T];
13}
14
15impl ToU32Array<2> for u64 {
16 fn to_u32_array(&self) -> [u32; 2] {
17 let a = (self >> 32) as u32;
18 let b = *self as u32;
19 [a, b]
20 }
21}
22
23impl ToU32Array<4> for u128 {
24 fn to_u32_array(&self) -> [u32; 4] {
25 let a = (self >> 96) as u32;
26 let b = (self >> 64) as u32;
27 let c = (self >> 32) as u32;
28 let d = *self as u32;
29 [a, b, c, d]
30 }
31}
32
33pub trait FromU32Array<const L: usize> {
36 fn from_u32_array(arr: &[u32; L]) -> Self;
39}
40
41impl FromU32Array<4> for u128 {
42 fn from_u32_array(arr: &[u32; 4]) -> Self {
43 let [a, b, c, d] = *arr;
44
45 let a: u128 = (a as u128) << 96;
46 let b: u128 = (b as u128) << 64;
47 let c: u128 = (c as u128) << 32;
48 let d: u128 = d as u128;
49
50 a | b | c | d
51 }
52}
53
54impl FromU32Array<2> for u64 {
55 fn from_u32_array(arr: &[u32; 2]) -> Self {
56 let [a, b] = *arr;
57
58 let a: u64 = (a as u64) << 32;
59 let b: u64 = b as u64;
60
61 a | b
62 }
63}
64
65impl WrappingAdd for u32 {
66 fn wrapping_add(&self, rhs: Self) -> Self {
67 u32::wrapping_add(*self, rhs)
68 }
69}
70impl WrappingAdd for i32 {
71 fn wrapping_add(&self, rhs: Self) -> Self {
72 i32::wrapping_add(*self, rhs)
73 }
74}
75impl WrappingSub for u32 {
76 fn wrapping_sub(&self, rhs: Self) -> Self {
77 u32::wrapping_sub(*self, rhs)
78 }
79}
80impl WrappingSub for i32 {
81 fn wrapping_sub(&self, rhs: Self) -> Self {
82 i32::wrapping_sub(*self, rhs)
83 }
84}
85impl WrappingMul for u32 {
86 fn wrapping_mul(&self, rhs: Self) -> Self {
87 u32::wrapping_mul(rhs, *self)
88 }
89}
90impl WrappingMul for i32 {
91 fn wrapping_mul(&self, rhs: Self) -> Self {
92 i32::wrapping_mul(*self, rhs)
93 }
94}
95impl ToF64 for i32 {
96 fn to_f64(&self) -> f64 {
97 *self as f64
98 }
99}
100impl ToF64 for u32 {
101 fn to_f64(&self) -> f64 {
102 *self as f64
103 }
104}