use std::{mem,slice}; use std::convert::{From,Into}; use std::ops::{BitAnd,BitOr,BitXor}; ///Type with a specified byte order pub trait Endian{} macro_rules! impl_Endian{ ( for $e:ident) => { impl BitAnd for $e where T: BitAnd { type Output = $e<::Output>; #[inline] fn bitand(self,other: Self) -> Self::Output{ $e(self.0 & other.0) } } impl BitOr for $e where T: BitOr { type Output = $e<::Output>; #[inline] fn bitor(self,other: Self) -> Self::Output{ $e(self.0 | other.0) } } impl BitXor for $e where T: BitXor { type Output = $e<::Output>; #[inline] fn bitxor(self,other: Self) -> Self::Output{ $e(self.0 ^ other.0) } } impl $e where T: Sized + Copy { #[inline] pub fn from_bytes(bytes: &[u8]) -> Self{ debug_assert!(bytes.len() >= mem::size_of::()); $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::() )} } /*pub fn write_bytes(self,buffer: &mut [u8]){ debug_assert!(buffer.len() >= mem::size_of::()); let bytes = mem::transmute::<_,[u8; mem::size_of::()]>(); unsafe{ptr::copy_nonoverlapping(bytes.as_ptr(),buffer.as_mut_ptr(),mem::size_of::())}; }*/ } } } ///Big endian byte order /// ///Most significant byte first #[derive(Copy,Clone,Debug,Eq,PartialEq,Hash,Ord,PartialOrd)] pub struct BigEndian(T); impl Endian for BigEndian{} 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> 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); impl Endian for LittleEndian{} 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> 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 = BigEndian; ///Type aliases for primitive types pub mod types{ #![allow(non_camel_case_types)] use super::*; pub type i16_be = BigEndian; pub type i32_be = BigEndian; pub type i64_be = BigEndian; pub type isize_be = BigEndian; pub type u16_be = BigEndian; pub type u32_be = BigEndian; pub type u64_be = BigEndian; pub type usize_be = BigEndian; pub type i16_le = LittleEndian; pub type i32_le = LittleEndian; pub type i64_le = LittleEndian; pub type isize_le = LittleEndian; pub type u16_le = LittleEndian; pub type u32_le = LittleEndian; pub type u64_le = LittleEndian; pub type usize_le = LittleEndian; pub type i16_net = NetworkOrder; pub type i32_net = NetworkOrder; pub type i64_net = NetworkOrder; pub type isize_net = NetworkOrder; pub type u16_net = NetworkOrder; pub type u32_net = NetworkOrder; pub type u64_net = NetworkOrder; pub type usize_net = NetworkOrder; } /*#[cfg(test)] mod tests{ use super::*; use super::types::*; #[test] fn construct_big(){ //#[cfg(target_endian = "big")]{} } }*/