1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright 2023 Alberto Ruiz <aruiz@gnome.org>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#![feature(core_intrinsics)]

use std::mem::transmute;

pub trait Swappable {}

impl Swappable for u128 {}
impl Swappable for u64 {}
impl Swappable for u32 {}
impl Swappable for u16 {}

impl Swappable for i128 {}
impl Swappable for i64 {}
impl Swappable for i32 {}
impl Swappable for i16 {}

impl Swappable for usize {}
impl Swappable for isize {}

impl Swappable for f32 {}
impl Swappable for f64 {}

pub struct LittleEndian<T: Swappable> {
    data: T,
}

pub struct BigEndian<T: Swappable> {
    data: T,
}

macro_rules! into_from {
    ($type: ident, $end: ident, $a: tt, $b: tt, $swap: tt) => {
        impl Into<$type> for $end<$type> {
            fn into(self) -> $type {
                #[cfg(target_endian = $a)]
                return self.data;
                #[cfg(target_endian = $b)]
                return unsafe { transmute(self.data.$swap()) };
            }
        }

        impl From<$type> for $end<$type> {
            fn from(value: $type) -> Self {
                #[cfg(target_endian = $a)]
                return Self { data: value };
                #[cfg(target_endian = $b)]
                return Self {
                    data: unsafe { transmute(value.$swap()) },
                };
            }
        }
    };
}

macro_rules! le_into_from {
    ($type: ident) => {
        into_from! {$type, LittleEndian, "little", "big", to_le_bytes}
    };
}

macro_rules! be_into_from {
    ($type: ident) => {
        into_from! {$type, BigEndian, "big", "little", to_be_bytes}
    };
}

le_into_from! {u16}
le_into_from! {u32}
le_into_from! {u64}
le_into_from! {u128}
le_into_from! {usize}

le_into_from! {i16}
le_into_from! {i32}
le_into_from! {i64}
le_into_from! {i128}
le_into_from! {isize}

le_into_from! {f32}
le_into_from! {f64}

be_into_from! {u16}
be_into_from! {u32}
be_into_from! {u64}
be_into_from! {u128}
be_into_from! {usize}

be_into_from! {i16}
be_into_from! {i32}
be_into_from! {i64}
be_into_from! {i128}
be_into_from! {isize}

be_into_from! {f32}
be_into_from! {f64}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! test_type {
        ($fun: ident, $type: ident, $val: expr, $rev: expr) => {
            #[test]
            fn $fun() {
                let l: LittleEndian<$type> = $val.into();
                #[cfg(target_endian = "little")]
                assert_eq!(l.data, $val);
                #[cfg(target_endian = "big")]
                assert_eq!(l.data, $rev);

                let b: BigEndian<$type> = $val.into();
                #[cfg(target_endian = "little")]
                assert_eq!(b.data, $rev);
                #[cfg(target_endian = "big")]
                assert_eq!(b.data, $val);
            }
        };
    }

    test_type! {test_u128, u128, 0xaa000000000000000000000000000000, 0x000000000000000000000000000000aa}
    test_type! {test_u64, u64, 0xaa00000000000000, 0x00000000000000aa}
    test_type! {test_u32, u32, 0xaa000000, 0x000000aa}
    test_type! {test_u16, u16, 0xaa00, 0x00aa}

    #[cfg(target_pointer_width = "32")]
    test_type! {test_usize, usize, 0xaa000000, 0x000000aa}
    #[cfg(target_pointer_width = "64")]
    test_type! {test_usize, usize, 0xaa00000000000000, 0x00000000000000aa}

    #[cfg(target_pointer_width = "32")]
    test_type! {test_isize, isize, -2isize, -16777217isize}
    #[cfg(target_pointer_width = "64")]
    test_type! {test_isize, isize, -2isize, -72057594037927937isize}

    test_type! {test_i16, i16, -2i16, -257i16}
    test_type! {test_i32, i32, -2i32, -16777217i32}
    test_type! {test_i64, i64, -2i64, -72057594037927937i64}
    test_type! {test_i128, i128, -2i128, -1329227995784915872903807060280344577i128}

    #[test]
    fn test_floats() {
        const F32_VAL: f32 = 1.3_f32;
        const F32_REV: f32 = 272302750000000000000000_f32;
        const F64_VAL: f64 = 1.3_f64;
        const F64_REV: f64 = -6065988000116450000000000000000000000000000000000000000000000000000_f64;

        let l: LittleEndian<f32> = F32_VAL.into();
        #[cfg(target_endian = "little")]
        assert_eq!(l.data, F32_VAL);
        #[cfg(target_endian = "big")]
        assert_eq!(l.data, F32_REV);

        let b: BigEndian<f32> = F32_VAL.into();
        #[cfg(target_endian = "little")]
        assert_eq!(b.data, F32_REV);
        #[cfg(target_endian = "big")]
        assert_eq!(b.data, F32_VAL);

        let l: LittleEndian<f64> = F64_VAL.into();
        #[cfg(target_endian = "little")]
        assert_eq!(l.data, F64_VAL);
        #[cfg(target_endian = "big")]
        assert_eq!(l.data, F64_REV);

        let b: BigEndian<f64> = F64_VAL.into();
        #[cfg(target_endian = "little")]
        assert_eq!(b.data, F64_REV);
        #[cfg(target_endian = "big")]
        assert_eq!(b.data, F64_VAL);
    }
}