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
203
use std::{mem,slice};
use std::convert::{From,Into};
use std::ops::{BitAnd,BitOr,BitXor};

///Type with a specified byte order
pub trait Endian<T>{}

macro_rules! impl_Endian{
	( for $e:ident) => {
		impl<T> BitAnd for $e<T>
			where T: BitAnd
		{
			type Output = $e<<T as BitAnd>::Output>;

			#[inline]
			fn bitand(self,other: Self) -> Self::Output{
				$e(self.0 & other.0)
			}
		}
		impl<T> BitOr for $e<T>
			where T: BitOr
		{
			type Output = $e<<T as BitOr>::Output>;

			#[inline]
			fn bitor(self,other: Self) -> Self::Output{
				$e(self.0 | other.0)
			}
		}
		impl<T> BitXor for $e<T>
			where T: BitXor
		{
			type Output = $e<<T as BitXor>::Output>;

			#[inline]
			fn bitxor(self,other: Self) -> Self::Output{
				$e(self.0 ^ other.0)
			}
		}

		impl<T> $e<T>
			where T: Sized + Copy
		{
			#[inline]
			pub fn from_bytes(bytes: &[u8]) -> Self{
				debug_assert!(bytes.len() >= mem::size_of::<T>());
				$e(unsafe{*(bytes.as_ptr() as *const T)})
			}

			#[inline]
			pub fn as_bytes(&self) -> &[u8]{
				unsafe{slice::from_raw_parts(
					&self.0 as *const T as *const u8,
					mem::size_of::<T>()
				)}
			}

			/*pub fn write_bytes(self,buffer: &mut [u8]){
				debug_assert!(buffer.len() >= mem::size_of::<T>());
				let bytes = mem::transmute::<_,[u8; mem::size_of::<T>()]>();
				unsafe{ptr::copy_nonoverlapping(bytes.as_ptr(),buffer.as_mut_ptr(),mem::size_of::<T>())};
			}*/
		}
	}
}



///Big endian byte order
///
///Most significant byte first
#[derive(Copy,Clone,Debug,Eq,PartialEq,Hash,Ord,PartialOrd)]
pub struct BigEndian<T>(T);
impl<T> Endian<T> for BigEndian<T>{}
macro_rules! impl_for_BigEndian{
	( $t:ident ) => {
		impl Into<$t> for BigEndian<$t>{
			#[inline]
			fn into(self) -> $t{
				$t::from_be(self.0)
			}
		}

		impl From<$t> for BigEndian<$t>{
			#[inline]
			fn from(data: $t) -> Self{
				BigEndian(data.to_be())
			}
		}

		impl From<LittleEndian<$t>> for BigEndian<$t>{
			#[inline]
			fn from(data: LittleEndian<$t>) -> Self{
				BigEndian(data.0.swap_bytes())
			}
		}
	}
}
impl_Endian!(for BigEndian);
impl_for_BigEndian!(u16);
impl_for_BigEndian!(u32);
impl_for_BigEndian!(u64);
impl_for_BigEndian!(usize);
impl_for_BigEndian!(i16);
impl_for_BigEndian!(i32);
impl_for_BigEndian!(i64);
impl_for_BigEndian!(isize);



///Little endian byte order
///
///Least significant byte first
#[derive(Copy,Clone,Debug,Eq,PartialEq,Hash,Ord,PartialOrd)]
pub struct LittleEndian<T>(T);
impl<T> Endian<T> for LittleEndian<T>{}
macro_rules! impl_for_LittleEndian{
	( $t:ident ) => {
		impl Into<$t> for LittleEndian<$t>{
			#[inline]
			fn into(self) -> $t{
				$t::from_le(self.0)
			}
		}

		impl From<$t> for LittleEndian<$t>{
			#[inline]
			fn from(data: $t) -> Self{
				LittleEndian(data.to_le())
			}
		}

		impl From<BigEndian<$t>> for LittleEndian<$t>{
			#[inline]
			fn from(data: BigEndian<$t>) -> Self{
				LittleEndian(data.0.swap_bytes())
			}
		}
	}
}
impl_Endian!(for LittleEndian);
impl_for_LittleEndian!(u16);
impl_for_LittleEndian!(u32);
impl_for_LittleEndian!(u64);
impl_for_LittleEndian!(usize);
impl_for_LittleEndian!(i16);
impl_for_LittleEndian!(i32);
impl_for_LittleEndian!(i64);
impl_for_LittleEndian!(isize);


///Network byte order as defined by IETF RFC1700 [http://tools.ietf.org/html/rfc1700]
pub type NetworkOrder<T> = BigEndian<T>;


///Type aliases for primitive types
pub mod types{
	#![allow(non_camel_case_types)]

	use super::*;

	pub type i16_be   = BigEndian<i16>;
	pub type i32_be   = BigEndian<i32>;
	pub type i64_be   = BigEndian<i64>;
	pub type isize_be = BigEndian<isize>;

	pub type u16_be   = BigEndian<u16>;
	pub type u32_be   = BigEndian<u32>;
	pub type u64_be   = BigEndian<u64>;
	pub type usize_be = BigEndian<usize>;

	pub type i16_le   = LittleEndian<i16>;
	pub type i32_le   = LittleEndian<i32>;
	pub type i64_le   = LittleEndian<i64>;
	pub type isize_le = LittleEndian<isize>;

	pub type u16_le   = LittleEndian<u16>;
	pub type u32_le   = LittleEndian<u32>;
	pub type u64_le   = LittleEndian<u64>;
	pub type usize_le = LittleEndian<usize>;

	pub type i16_net   = NetworkOrder<i16>;
	pub type i32_net   = NetworkOrder<i32>;
	pub type i64_net   = NetworkOrder<i64>;
	pub type isize_net = NetworkOrder<isize>;

	pub type u16_net   = NetworkOrder<u16>;
	pub type u32_net   = NetworkOrder<u32>;
	pub type u64_net   = NetworkOrder<u64>;
	pub type usize_net = NetworkOrder<usize>;
}

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

	#[test]
	fn construct_big(){
		//#[cfg(target_endian = "big")]{}

	}
}*/