#[repr(C)]
pub struct WireValue<T> {
value : T
}
impl<T:Endian> WireValue<T> {
#[inline]
pub fn get(&self) -> T { self.value.get() }
#[inline]
pub fn set(&mut self, value : T) { self.value.set(value) }
}
pub trait Endian : Sized {
fn get(&self) -> Self;
fn set(&mut self, value : Self);
}
macro_rules! endian_impl(
($typ:ty) => (
impl Endian for $typ {
#[inline]
fn get(&self) -> $typ { *self }
#[inline]
fn set(&mut self, value : $typ) {*self = value;}
}
);
($typ:ty, $swapper:ident) => (
impl Endian for $typ {
#[inline]
fn get(&self) -> $typ { self.$swapper() }
#[inline]
fn set(&mut self, value : $typ) {
*self = value.$swapper();
}
}
);
);
endian_impl!(());
endian_impl!(bool);
endian_impl!(u8);
endian_impl!(i8);
endian_impl!(u16, to_le);
endian_impl!(i16, to_le);
endian_impl!(u32, to_le);
endian_impl!(i32, to_le);
endian_impl!(u64, to_le);
endian_impl!(i64, to_le);
impl Endian for f32 {
fn get(&self) -> f32 {
unsafe { ::std::mem::transmute(::std::mem::transmute::<f32, u32>(*self).to_le()) }
}
fn set(&mut self, value : f32) {
*self = unsafe { ::std::mem::transmute(::std::mem::transmute::<f32, u32>(value).to_le()) };
}
}
impl Endian for f64 {
fn get(&self) -> f64 {
unsafe { ::std::mem::transmute(::std::mem::transmute::<f64, u64>(*self).to_le()) }
}
fn set(&mut self, value : f64) {
*self = unsafe { ::std::mem::transmute(::std::mem::transmute::<f64, u64>(value).to_le()) };
}
}