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
use std::fmt::{Debug, Display, Formatter, Error}; pub trait EndianBufFor<Out> { #[inline(always)] fn reverse(&mut self); #[inline(always)] fn native(self) -> Out; } pub trait FromBuf: Sized + Clone { type Buf: EndianBufFor<Self>; #[inline(always)] fn from_buf(buf: Self::Buf) -> Self { buf.native() } #[inline(always)] fn into_buf(self) -> Self::Buf; } pub trait IntoNativeEndian { type Out; #[inline(always)] fn native(self) -> Self::Out; } #[derive(Clone)] pub struct Big<T: FromBuf>(pub T::Buf); #[derive(Clone)] pub struct Little<T: FromBuf>(pub T::Buf); impl<T: FromBuf + Debug> Debug for Big<T> where Self: Clone { #[inline(always)] fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { write!(f, "Big({:?})", self.clone().native()) } } impl<T: FromBuf + Debug> Debug for Little<T> where Self: Clone { #[inline(always)] fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { write!(f, "Big({:?})", self.clone().native()) } } impl<T: FromBuf + Display> Display for Big<T> where Self: Clone { #[inline(always)] fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { self.clone().native().fmt(f) } } impl<T: FromBuf + Debug> Display for Little<T> where Self: Clone { #[inline(always)] fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { self.clone().native().fmt(f) } } macro_rules! impl_buf_traits { ($type:ty, $count:expr) => { impl EndianBufFor<$type> for [u8; $count] { #[inline(always)] fn reverse(&mut self) { self.as_mut().reverse() } #[inline(always)] fn native(self) -> $type { unsafe { ::std::mem::transmute(self) } } } impl FromBuf for $type { type Buf = [u8; $count]; #[inline(always)] fn into_buf(self) -> Self::Buf { unsafe { ::std::mem::transmute(self) } } } impl Copy for Big<$type> {} impl Copy for Little<$type> {} }; } impl_buf_traits!(u8, 1); impl_buf_traits!(u16, 2); impl_buf_traits!(u32, 4); impl_buf_traits!(u64, 8); impl_buf_traits!(i8, 1); impl_buf_traits!(i16, 2); impl_buf_traits!(i32, 4); impl_buf_traits!(i64, 8); impl_buf_traits!(f32, 4); impl_buf_traits!(f64, 8); #[cfg(target_endian = "big")] mod trait_impls { use super::{FromBuf, EndianBufFor, IntoNativeEndian, Big, Little}; impl<T: FromBuf> Big<T> { #[inline(always)] fn new(inner: T) -> Self { Big(inner.into_buf()) } } impl<T: FromBuf> Little<T> { #[inline(always)] fn new(inner: T) -> Self { let mut buf = inner.into_buf(); buf.reverse(); Little(buf) } } impl<T: FromBuf> IntoNativeEndian for Big<T> { type Out = T; #[inline(always)] fn native(self) -> Self::Out { T::from_buf(self.0) } } impl<T: FromBuf> IntoNativeEndian for Little<T> { type Out = T; #[inline(always)] fn native(mut self) -> Self::Out { self.0.reverse(); T::from_buf(self.0) } } } #[cfg(target_endian = "little")] mod trait_impls { use super::{FromBuf, EndianBufFor, IntoNativeEndian, Big, Little}; impl<T: FromBuf> Big<T> { #[inline(always)] pub fn new(inner: T) -> Self { let mut buf = inner.into_buf(); buf.reverse(); Big(buf) } } impl<T: FromBuf> Little<T> { #[inline(always)] pub fn new(inner: T) -> Self { Little(inner.into_buf()) } } impl<T: FromBuf> IntoNativeEndian for Big<T> { type Out = T; #[inline(always)] fn native(mut self) -> Self::Out { self.0.reverse(); T::from_buf(self.0) } } impl<T: FromBuf> IntoNativeEndian for Little<T> { type Out = T; #[inline(always)] fn native(self) -> Self::Out { T::from_buf(self.0) } } } #[cfg(test)] mod tests { #[test] fn it_works() {} }