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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::ops::{BitAnd, BitOr, BitXor, Not};
use std::cmp::{PartialEq, Eq};

/// Integer byte order
pub trait ByteOrder {}

/// Big‐endian byte order
///
/// Most significant byte first
pub struct BigEndian;
impl ByteOrder for BigEndian {}

/// Little‐endian byte order
///
/// Least significant byte first
pub struct LittleEndian;
impl ByteOrder for LittleEndian {}

/// Byte order‐limited integer
#[derive(Copy, Clone)]
pub struct Endian<T: BitAnd + BitOr + BitXor + Not + Eq, O: ByteOrder>(T, O);

macro_rules! endian {
	($int:ty, $order:ty) => (
		impl BitAnd for Endian<$int, $order> {
			type Output = Endian<<$int as BitAnd>::Output, $order>;

			fn bitand(self, rhs: Self) -> Self::Output {
				let Endian(lhs_value, lhs_order) = self;
				let Endian(rhs_value, _) = rhs;
				Endian(lhs_value & rhs_value, lhs_order)
			}
		}

		impl BitOr for Endian<$int, $order> {
			type Output = Endian<<$int as BitOr>::Output, $order>;

			fn bitor(self, rhs: Self) -> Self::Output {
				let Endian(lhs_value, lhs_order) = self;
				let Endian(rhs_value, _) = rhs;
				Endian(lhs_value | rhs_value, lhs_order)
			}
		}

		impl BitXor for Endian<$int, $order> {
			type Output = Endian<<$int as BitXor>::Output, $order>;

			fn bitxor(self, rhs: Self) -> Self::Output {
				let Endian(lhs_value, lhs_order) = self;
				let Endian(rhs_value, _) = rhs;
				Endian(lhs_value ^ rhs_value, lhs_order)
			}
		}

		impl Not for Endian<$int, $order> {
			type Output = Endian<<$int as Not>::Output, $order>;

			fn not(self) -> Self::Output {
				let Endian(lhs_value, lhs_order) = self;
				Endian(!lhs_value, lhs_order)
			}
		}

		impl PartialEq for Endian<$int, $order> {
			fn eq(&self, rhs: &Self) -> bool {
				let Endian(lhs_value, _) = *self;
				let Endian(rhs_value, _) = *rhs;
				lhs_value == rhs_value
			}

			fn ne(&self, rhs: &Self) -> bool {
				let Endian(lhs_value, _) = *self;
				let Endian(rhs_value, _) = *rhs;
				lhs_value != rhs_value
			}
		}

		impl Eq for Endian<$int, $order> {}
	);
}

macro_rules! endian_be {
	($int:ident) => (
		impl Endian<$int, BigEndian> {
			/// Constructs new byte order‐limited integer
			pub fn new(value: $int) -> Self {
				Endian(value.to_be(), BigEndian)
			}

			/// Converts byte‐order limited integer to native integer
			pub fn to_native(self) -> $int {
				let Endian(value, _) = self;
				$int::from_be(value)
			}
		}

		endian!($int, BigEndian);
	);
}

macro_rules! endian_le {
	($int:ident) => (
		impl Endian<$int, LittleEndian> {
			/// Constructs new byte order‐limited integer
			pub fn new(value: $int) -> Self {
				Endian(value.to_le(), LittleEndian)
			}

			/// Converts byte‐order limited integer to native integer
			pub fn to_native(self) -> $int {
				let Endian(value, _) = self;
				$int::from_le(value)
			}
		}

		endian!($int, LittleEndian);
	);
}

endian_be!(u16);
endian_be!(u32);
endian_be!(u64);

endian_le!(u16);
endian_le!(u32);
endian_le!(u64);

/// Short type aliases
pub mod types {
	use {Endian, BigEndian, LittleEndian};

	#[allow(non_camel_case_types)]
	/// Big‐endian unsigned 16 bit integer
	pub type be16 = Endian<u16, BigEndian>;

	#[allow(non_camel_case_types)]
	/// Big‐endian unsigned 32 bit integer
	pub type be32 = Endian<u32, BigEndian>;

	#[allow(non_camel_case_types)]
	/// Big‐endian unsigned 64 bit integer
	pub type be64 = Endian<u64, BigEndian>;

	#[allow(non_camel_case_types)]
	/// Little‐endian unsigned 16 bit integer
	pub type le16 = Endian<u16, LittleEndian>;

	#[allow(non_camel_case_types)]
	/// Little‐endian unsigned 32 bit integer
	pub type le32 = Endian<u32, LittleEndian>;

	#[allow(non_camel_case_types)]
	/// Little‐endian unsigned 64 bit integer
	pub type le64 = Endian<u64, LittleEndian>;
}

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

	#[test]
	fn be16_invert_static() {
		let int = 0xc0de_u16;
		let be = be16::new(int);
		assert_eq!(int, be.to_native());
	}

	#[test]
	fn be32_invert_static() {
		let int = 0xdeadbeef_u32;
		let be = be32::new(int);
		assert_eq!(int, be.to_native());
	}

	#[test]
	fn be64_invert_static() {
		let int = 0xcafec0dedeadbeef_u64;
		let be = be64::new(int);
		assert_eq!(int, be.to_native());
	}

	#[test]
	fn le16_invert_static() {
		let int = 0xc0de_u16;
		let le = le16::new(int);
		assert_eq!(int, le.to_native());
	}

	#[test]
	fn le32_invert_static() {
		let int = 0xdeadbeef_u32;
		let le = le32::new(int);
		assert_eq!(int, le.to_native());
	}

	#[test]
	fn le64_invert_static() {
		let int = 0xcafec0dedeadbeef_u64;
		let le = le64::new(int);
		assert_eq!(int, le.to_native());
	}
}