sysfunc_byteorder/
lib.rs

1//! Simple crate providing compile-time byte order constants.
2
3#![cfg_attr(feature = "no-core", feature(no_core))]
4#![cfg_attr(feature = "no-core", no_core)]
5#![cfg_attr(not(feature = "no-core"), no_std)]
6#![deny(missing_docs)]
7#![warn(unused)]
8
9//-----------------------------------------------------------------------------
10
11#[cfg(any(all(not(target_endian = "big"), not(target_endian = "little")), feature = "force-conversion"))]
12mod endian_values {
13    #[cfg(feature = "enable-128")]
14    pub (crate) const O_U128: u128 = u128::from_be(0x0F0E0D0C0B0A09080706050403020100);
15
16    pub (crate) const O_U64: u64 = u64::from_be(0x0706050403020100);
17
18    pub (crate) const O_U32: u32 = u32::from_be(0x03020100);
19
20    pub (crate) const O_U16: u16 = u16::from_be(0x0100);
21}
22
23#[cfg(all(target_endian = "little", not(feature = "force-conversion")))]
24mod endian_values {
25    #[cfg(feature = "enable-128")]
26    pub (crate) const O_U128: u128 = 0x000102030405060708090A0B0C0D0E0F;
27
28    pub (crate) const O_U64: u64 = 0x0001020304050607;
29
30    pub (crate) const O_U32: u32 = 0x00010203;
31
32    pub (crate) const O_U16: u16 = 0x0001;
33}
34
35#[cfg(all(target_endian = "big", not(feature = "force-conversion")))]
36mod endian_values {
37    #[cfg(feature = "enable-128")]
38    pub (crate) const O_U128: u128 = 0x0F0E0D0C0B0A09080706050403020100;
39
40    pub (crate) const O_U64: u64 = 0x0706050403020100;
41
42    pub (crate) const O_U32: u32 = 0x03020100;
43
44    pub (crate) const O_U16: u16 = 0x0100;
45}
46
47use endian_values::*;
48
49//-----------------------------------------------------------------------------
50
51/// Provides byte ordering for u128.
52#[cfg(feature = "enable-128")]
53pub const fn get_byte_order_128() -> *const [u8; 16] {
54    &O_U128 as *const u128 as *const [u8; 16]
55}
56
57/// Provides byte ordering for u64.
58pub const fn get_byte_order_64() -> *const [u8; 8] {
59    &O_U64 as *const u64 as *const [u8; 8]
60}
61
62/// Provides byte ordering for u32.
63pub const fn get_byte_order_32() -> *const [u8; 4] {
64    &O_U32 as *const u32 as *const [u8; 4]
65}
66
67/// Provides byte ordering for u16.
68pub const fn get_byte_order_16() -> *const [u8; 2] {
69    &O_U16 as *const u16 as *const [u8; 2]
70}
71
72//-----------------------------------------------------------------------------
73
74#[cfg(test)]
75mod test {
76    #[cfg(all(target_endian = "big", not(feature = "force-conversion")))]
77    #[test]
78    fn test_big_endian() {
79        use super::*;
80
81        #[cfg(feature = "enable-128")]
82        fn test_128() {
83            // Any value will do.
84            let y: u128 = 0x87BF436137FBCD76453896215AB983DF; // in
85            let mut z: u128 = 0; // out
86            let order = unsafe { *get_byte_order_128() };
87            for i in 0..16 {
88                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
89            }
90            assert_eq!(u128::to_le(y), z);
91        }
92
93        #[cfg(not(feature = "enable-128"))]
94        fn test_128() {
95        }
96
97        test_128();
98
99        // u64
100        {
101            // Any value will do.
102            let y: u64 = 0xAB5A34BBF98356DF; // in
103            let mut z: u64 = 0; // out
104            let order = unsafe { *get_byte_order_64() };
105            for i in 0..8 {
106                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
107            }
108            assert_eq!(u64::to_le(y), z);
109        }
110
111        // u32
112        {
113            // Any value will do.
114            let y: u32 = 0x5AB983DF; // in
115            let mut z: u32 = 0; // out
116            let order = unsafe { *get_byte_order_32() };
117            for i in 0..4 {
118                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
119            }
120            assert_eq!(u32::to_le(y), z);
121        }
122
123        // u16
124        {
125            // Any value will do.
126            let y: u16 = 0xBF59; // in
127            let mut z: u16 = 0; // out
128            let order = unsafe { *get_byte_order_16() };
129            for i in 0..2 {
130                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
131            }
132            assert_eq!(u16::to_le(y), z);
133        }
134    }
135
136    #[cfg(all(target_endian = "little", not(feature = "force-conversion")))]
137    #[test]
138    fn test_little_endian() {
139        use super::*;
140
141        #[cfg(feature = "enable-128")]
142        fn test_128() {
143            // Any value will do.
144            let y: u128 = 0x87BF436137FBCD76453896215AB983DF; // in
145            let mut z: u128 = 0; // out
146            let order = unsafe { *get_byte_order_128() };
147            for i in 0..16 {
148                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
149            }
150            assert_eq!(u128::to_be(y), z);
151        }
152
153        #[cfg(not(feature = "enable-128"))]
154        fn test_128() {
155        }
156
157        test_128();
158
159        // u64
160        {
161            // Any value will do.
162            let y: u64 = 0xAB5A34BBF98356DF; // in
163            let mut z: u64 = 0; // out
164            let order = unsafe { *get_byte_order_64() };
165            for i in 0..8 {
166                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
167            }
168            assert_eq!(u64::to_be(y), z);
169        }
170
171        // u32
172        {
173            // Any value will do.
174            let y: u32 = 0x5AB983DF; // in
175            let mut z: u32 = 0; // out
176            let order = unsafe { *get_byte_order_32() };
177            for i in 0..4 {
178                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
179            }
180            assert_eq!(u32::to_be(y), z);
181        }
182
183        // u16
184        {
185            // Any value will do.
186            let y: u16 = 0xBF59; // in
187            let mut z: u16 = 0; // out
188            let order = unsafe { *get_byte_order_16() };
189            for i in 0..2 {
190                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
191            }
192            assert_eq!(u16::to_be(y), z);
193        }
194    }
195
196    #[cfg(all(target_endian = "big", feature = "force-conversion"))]
197    #[test]
198    fn test_other() {
199        use super::*;
200
201        #[cfg(feature = "enable-128")]
202        fn test_128() {
203            // Any value will do.
204            let y: u128 = 0x87BF436137FBCD76453896215AB983DF; // in
205            let mut z: u128 = 0; // out
206            let order = unsafe { *get_byte_order_128() };
207            for i in 0..16 {
208                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
209            }
210            assert_eq!(u128::to_le(y), z);
211        }
212
213        #[cfg(not(feature = "enable-128"))]
214        fn test_128() {
215        }
216
217        test_128();
218
219        // u64
220        {
221            // Any value will do.
222            let y: u64 = 0xAB5A34BBF98356DF; // in
223            let mut z: u64 = 0; // out
224            let order = unsafe { *get_byte_order_64() };
225            for i in 0..8 {
226                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
227            }
228            assert_eq!(u64::to_le(y), z);
229        }
230
231        // u32
232        {
233            // Any value will do.
234            let y: u32 = 0x5AB983DF; // in
235            let mut z: u32 = 0; // out
236            let order = unsafe { *get_byte_order_32() };
237            for i in 0..4 {
238                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
239            }
240            assert_eq!(u32::to_le(y), z);
241        }
242
243        // u16
244        {
245            // Any value will do.
246            let y: u16 = 0xBF59; // in
247            let mut z: u16 = 0; // out
248            let order = unsafe { *get_byte_order_16() };
249            for i in 0..2 {
250                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
251            }
252            assert_eq!(u16::to_le(y), z);
253        }
254    }
255
256    #[cfg(all(target_endian = "little", feature = "force-conversion"))]
257    #[test]
258    fn test_other() {
259        use super::*;
260
261        #[cfg(feature = "enable-128")]
262        fn test_128() {
263            // Any value will do.
264            let y: u128 = 0x87BF436137FBCD76453896215AB983DF; // in
265            let mut z: u128 = 0; // out
266            let order = unsafe { *get_byte_order_128() };
267            for i in 0..16 {
268                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
269            }
270            assert_eq!(u128::to_be(y), z);
271        }
272
273        #[cfg(not(feature = "enable-128"))]
274        fn test_128() {
275        }
276
277        test_128();
278
279        // u64
280        {
281            // Any value will do.
282            let y: u64 = 0xAB5A34BBF98356DF; // in
283            let mut z: u64 = 0; // out
284            let order = unsafe { *get_byte_order_64() };
285            for i in 0..8 {
286                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
287            }
288            assert_eq!(u64::to_be(y), z);
289        }
290
291        // u32
292        {
293            // Any value will do.
294            let y: u32 = 0x5AB983DF; // in
295            let mut z: u32 = 0; // out
296            let order = unsafe { *get_byte_order_32() };
297            for i in 0..4 {
298                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
299            }
300            assert_eq!(u32::to_be(y), z);
301        }
302
303        // u16
304        {
305            // Any value will do.
306            let y: u16 = 0xBF59; // in
307            let mut z: u16 = 0; // out
308            let order = unsafe { *get_byte_order_16() };
309            for i in 0..2 {
310                z |= ((y >> i * 8) & 0xFF) << (order[i] * 8);
311            }
312            assert_eq!(u16::to_be(y), z);
313        }
314    }
315}