irox_tools/primitives/
u16.rs

1// SPDX-License-Identifier: MIT
2// Copyright ${YEAR} IROX Contributors
3//
4
5use crate::{ToF64, WrappingAdd, WrappingMul, WrappingSub};
6
7///
8/// Converts the specified primitive to a big-endian [`[u16;T]`]
9pub trait ToU16Array<const T: usize> {
10    ///
11    /// Creates an big-endian array of [`u16`]'s from this specified primitive type.
12    fn to_u16_array(&self) -> [u16; T];
13}
14
15impl ToU16Array<2> for u32 {
16    fn to_u16_array(&self) -> [u16; 2] {
17        let a = (self >> 16) as u16;
18        let b = *self as u16;
19        [a, b]
20    }
21}
22
23impl ToU16Array<4> for u64 {
24    fn to_u16_array(&self) -> [u16; 4] {
25        let a = (self >> 48) as u16;
26        let b = (self >> 32) as u16;
27        let c = (self >> 16) as u16;
28        let d = *self as u16;
29        [a, b, c, d]
30    }
31}
32
33impl ToU16Array<8> for u128 {
34    fn to_u16_array(&self) -> [u16; 8] {
35        let a = (self >> 112) as u16;
36        let b = (self >> 96) as u16;
37        let c = (self >> 80) as u16;
38        let d = (self >> 64) as u16;
39        let e = (self >> 48) as u16;
40        let f = (self >> 32) as u16;
41        let g = (self >> 16) as u16;
42        let h = *self as u16;
43        [a, b, c, d, e, f, g, h]
44    }
45}
46
47///
48/// Creates a Self from a constant u16 array.
49pub trait FromU16Array<const L: usize> {
50    ///
51    /// Creates a primitive type from an big-endian array of [`u16`]'s
52    fn from_u16_array(arr: &[u16; L]) -> Self;
53}
54
55impl FromU16Array<8> for u128 {
56    fn from_u16_array(arr: &[u16; 8]) -> Self {
57        let [a, b, c, d, e, f, g, h] = *arr;
58
59        let a: u128 = (a as u128) << 112;
60        let b: u128 = (b as u128) << 96;
61        let c: u128 = (c as u128) << 80;
62        let d: u128 = (d as u128) << 64;
63        let e: u128 = (e as u128) << 48;
64        let f: u128 = (f as u128) << 32;
65        let g: u128 = (g as u128) << 16;
66        let h: u128 = h as u128;
67
68        a | b | c | d | e | f | g | h
69    }
70}
71
72impl FromU16Array<4> for u64 {
73    fn from_u16_array(arr: &[u16; 4]) -> Self {
74        let [a, b, c, d] = *arr;
75
76        let a: u64 = (a as u64) << 48;
77        let b: u64 = (b as u64) << 32;
78        let c: u64 = (c as u64) << 16;
79        let d: u64 = d as u64;
80
81        a | b | c | d
82    }
83}
84
85impl FromU16Array<2> for u32 {
86    fn from_u16_array(arr: &[u16; 2]) -> Self {
87        let [a, b] = *arr;
88
89        let a: u32 = (a as u32) << 16;
90        let b: u32 = b as u32;
91
92        a | b
93    }
94}
95
96impl WrappingAdd for u16 {
97    fn wrapping_add(&self, other: u16) -> u16 {
98        u16::wrapping_add(*self, other)
99    }
100}
101impl WrappingAdd for i16 {
102    fn wrapping_add(&self, other: i16) -> i16 {
103        i16::wrapping_add(*self, other)
104    }
105}
106
107impl WrappingSub for u16 {
108    fn wrapping_sub(&self, rhs: Self) -> Self {
109        u16::wrapping_sub(*self, rhs)
110    }
111}
112impl WrappingSub for i16 {
113    fn wrapping_sub(&self, rhs: Self) -> Self {
114        i16::wrapping_sub(*self, rhs)
115    }
116}
117impl WrappingMul for u16 {
118    fn wrapping_mul(&self, rhs: Self) -> Self {
119        u16::wrapping_mul(*self, rhs)
120    }
121}
122impl WrappingMul for i16 {
123    fn wrapping_mul(&self, rhs: Self) -> Self {
124        i16::wrapping_mul(*self, rhs)
125    }
126}
127
128impl ToF64 for i16 {
129    fn to_f64(&self) -> f64 {
130        *self as f64
131    }
132}
133impl ToF64 for u16 {
134    fn to_f64(&self) -> f64 {
135        *self as f64
136    }
137}