#[repr(C)]
pub struct WireValue<T> {
value: T,
}
impl<T> WireValue<T> where T: Endian {
#[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 {
f32::from_bits(self.to_bits().to_le())
}
fn set(&mut self, value : f32) {
*self = f32::from_bits(value.to_bits().to_le());
}
}
impl Endian for f64 {
fn get(&self) -> f64 {
f64::from_bits(self.to_bits().to_le())
}
fn set(&mut self, value : f64) {
*self = f64::from_bits(value.to_bits().to_le());
}
}