ts_rust_helper/
integer.rs

1//! Common functions for integers.
2//!
3
4/// Common functions for integers.
5pub trait Integer<const BYTES: usize> {
6    /// Creates a native endian integer value from its representation as a byte array in big endian.
7    fn from_be_bytes(bytes: [u8; BYTES]) -> Self;
8    /// Creates a native endian integer value from its representation as a byte array in little endian.
9    fn from_le_bytes(bytes: [u8; BYTES]) -> Self;
10    /// Creates a native endian integer value from its representation as a byte array in native endian.
11    fn from_ne_bytes(bytes: [u8; BYTES]) -> Self;
12}
13
14impl Integer<1> for u8 {
15    fn from_be_bytes(bytes: [u8; 1]) -> Self {
16        Self::from_be_bytes(bytes)
17    }
18
19    fn from_le_bytes(bytes: [u8; 1]) -> Self {
20        Self::from_le_bytes(bytes)
21    }
22
23    fn from_ne_bytes(bytes: [u8; 1]) -> Self {
24        Self::from_ne_bytes(bytes)
25    }
26}
27impl Integer<2> for u16 {
28    fn from_be_bytes(bytes: [u8; 2]) -> Self {
29        Self::from_be_bytes(bytes)
30    }
31
32    fn from_le_bytes(bytes: [u8; 2]) -> Self {
33        Self::from_le_bytes(bytes)
34    }
35
36    fn from_ne_bytes(bytes: [u8; 2]) -> Self {
37        Self::from_ne_bytes(bytes)
38    }
39}
40impl Integer<4> for u32 {
41    fn from_be_bytes(bytes: [u8; 4]) -> Self {
42        Self::from_be_bytes(bytes)
43    }
44
45    fn from_le_bytes(bytes: [u8; 4]) -> Self {
46        Self::from_le_bytes(bytes)
47    }
48
49    fn from_ne_bytes(bytes: [u8; 4]) -> Self {
50        Self::from_ne_bytes(bytes)
51    }
52}
53impl Integer<8> for u64 {
54    fn from_be_bytes(bytes: [u8; 8]) -> Self {
55        Self::from_be_bytes(bytes)
56    }
57
58    fn from_le_bytes(bytes: [u8; 8]) -> Self {
59        Self::from_le_bytes(bytes)
60    }
61
62    fn from_ne_bytes(bytes: [u8; 8]) -> Self {
63        Self::from_ne_bytes(bytes)
64    }
65}
66impl Integer<16> for u128 {
67    fn from_be_bytes(bytes: [u8; 16]) -> Self {
68        Self::from_be_bytes(bytes)
69    }
70
71    fn from_le_bytes(bytes: [u8; 16]) -> Self {
72        Self::from_le_bytes(bytes)
73    }
74
75    fn from_ne_bytes(bytes: [u8; 16]) -> Self {
76        Self::from_ne_bytes(bytes)
77    }
78}
79impl Integer<1> for i8 {
80    fn from_be_bytes(bytes: [u8; 1]) -> Self {
81        Self::from_be_bytes(bytes)
82    }
83
84    fn from_le_bytes(bytes: [u8; 1]) -> Self {
85        Self::from_le_bytes(bytes)
86    }
87
88    fn from_ne_bytes(bytes: [u8; 1]) -> Self {
89        Self::from_ne_bytes(bytes)
90    }
91}
92impl Integer<2> for i16 {
93    fn from_be_bytes(bytes: [u8; 2]) -> Self {
94        Self::from_be_bytes(bytes)
95    }
96
97    fn from_le_bytes(bytes: [u8; 2]) -> Self {
98        Self::from_le_bytes(bytes)
99    }
100
101    fn from_ne_bytes(bytes: [u8; 2]) -> Self {
102        Self::from_ne_bytes(bytes)
103    }
104}
105impl Integer<4> for i32 {
106    fn from_be_bytes(bytes: [u8; 4]) -> Self {
107        Self::from_be_bytes(bytes)
108    }
109
110    fn from_le_bytes(bytes: [u8; 4]) -> Self {
111        Self::from_le_bytes(bytes)
112    }
113
114    fn from_ne_bytes(bytes: [u8; 4]) -> Self {
115        Self::from_ne_bytes(bytes)
116    }
117}
118impl Integer<8> for i64 {
119    fn from_be_bytes(bytes: [u8; 8]) -> Self {
120        Self::from_be_bytes(bytes)
121    }
122
123    fn from_le_bytes(bytes: [u8; 8]) -> Self {
124        Self::from_le_bytes(bytes)
125    }
126
127    fn from_ne_bytes(bytes: [u8; 8]) -> Self {
128        Self::from_ne_bytes(bytes)
129    }
130}
131impl Integer<16> for i128 {
132    fn from_be_bytes(bytes: [u8; 16]) -> Self {
133        Self::from_be_bytes(bytes)
134    }
135
136    fn from_le_bytes(bytes: [u8; 16]) -> Self {
137        Self::from_le_bytes(bytes)
138    }
139
140    fn from_ne_bytes(bytes: [u8; 16]) -> Self {
141        Self::from_ne_bytes(bytes)
142    }
143}