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
180
181
182
183
/*
 * 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/.
 */

//! # Explicit endian conversion no_std crate
//!
//! A lightweight, no_std crate for convenient data conversion between different endianness formats. Simplifies managing binary data on systems with varying endianness.
//!
//! ## Example
//!
//! ```rust
//! extern crate explicit_endian as ee;
//!
//! use ee::{LittleEndian, BigEndian, Swappable};
//!
//! fn main() {
//!     let value = 42u32;
//!
//!     // Convert to little-endian
//!     let le_value: LittleEndian<u32> = value.into();
//!
//!     // Convert to big-endian
//!     let be_value: BigEndian<u32> = value.into();
//!
//!     // You can now work with le_value and be_value in their respective endianness formats.
//! }
//! ```
//!
//! ## Supported Data Types
//!
//! - `u16`, `u32`, `u64`, `u128`
//! - `i16`, `i32`, `i64`, `i128`
//! - `usize`, `isize`
//! - `f32`, `f64`

#![no_std]
#![feature(core_intrinsics)]

use core::intrinsics::transmute;

/// A trait representing data types that can be swapped between endianness formats.
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 {}

/// A wrapper to store data in little endian format
#[derive(Debug, Clone, Copy)]
pub struct LittleEndian<T: Swappable>(T);

/// A wrapper to store data in big endian format
#[derive(Debug, Clone, Copy)]
pub struct BigEndian<T: Swappable>(T);

/// A macro to implement Into<T: Swappable> and From<T: Swappable> for either LittleEndian<T> or BigEndian<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.0;
                #[cfg(target_endian = $b)]
                return unsafe { transmute(self.0.$swap()) };
            }
        }

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

/// Wrapper macro to implement From/Into<T: Swappable> for LittleEndian<T: Swappable>
macro_rules! le_into_from {
    ($type: ident) => {
        into_from! {$type, LittleEndian, "little", "big", to_le_bytes}
    };
}

/// Wrapper macro to implement From/Into<T: Swappable> for BigEndian<T: Swappable>
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.0, $val);
                #[cfg(target_endian = "big")]
                assert_eq!(l.0, $rev);

                let b: BigEndian<$type> = $val.into();
                #[cfg(target_endian = "little")]
                assert_eq!(b.0, $rev);
                #[cfg(target_endian = "big")]
                assert_eq!(b.0, $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_type! {test_f32, f32, 1.3_f32, 272302750000000000000000_f32}
    test_type! {test_f64, f64, 1.3_f64, -6065988000116450000000000000000000000000000000000000000000000000000_f64}
}