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
use std::marker::PhantomData;
use std::fmt;
use std::hash::{Hash, Hasher};
use byteorder::{ByteOrder, LittleEndian, BigEndian};
use uninitialized::uninitialized;
use pod::{Pod, PodType};

/// A type alias for little endian primitives
pub type Le<T> = EndianPrimitive<LittleEndian, T>;

/// A type alias for big endian primitives
pub type Be<T> = EndianPrimitive<BigEndian, T>;

/// A POD container for a primitive that stores a value in the specified endianness
/// in memory, and transforms on `get`/`set`
pub struct EndianPrimitive<B, T> {
    value: T,
    _phantom: PhantomData<*const B>,
}

impl<B: ByteOrder, T: EndianConvert> EndianPrimitive<B, T> {
    /// Creates a new value
    pub fn new(v: T) -> Self {
        EndianPrimitive {
            value: EndianConvert::to::<B>(v),
            _phantom: PhantomData,
        }
    }

    /// Transforms to the native value
    pub fn get(&self) -> T {
        EndianConvert::from::<B>(&self.value)
    }

    /// Transforms from a native value
    pub fn set(&mut self, v: T) {
        self.value = EndianConvert::to::<B>(v)
    }
}

impl<B, T: Pod> Pod for EndianPrimitive<B, T> { }
unsafe impl<B, T: PodType> PodType for EndianPrimitive<B, T> { }

impl<B: ByteOrder, T: Default + EndianConvert> Default for EndianPrimitive<B, T> {
    fn default() -> Self {
        Self::new(Default::default())
    }
}

impl<B: ByteOrder, T: EndianConvert> From<T> for EndianPrimitive<B, T> {
    fn from(v: T) -> Self {
        Self::new(v)
    }
}

impl<B, T: fmt::Debug> fmt::Debug for EndianPrimitive<B, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        <T as fmt::Debug>::fmt(&self.value, f)
    }
}

impl<BRHS: ByteOrder, RHS: EndianConvert, B: ByteOrder, T: EndianConvert + PartialEq<RHS>> PartialEq<EndianPrimitive<BRHS, RHS>> for EndianPrimitive<B, T> {
    fn eq(&self, other: &EndianPrimitive<BRHS, RHS>) -> bool {
        self.get().eq(&other.get())
    }
}

impl<B, T: Hash> Hash for EndianPrimitive<B, T> {
    fn hash<H: Hasher>(&self, h: &mut H) {
        self.value.hash(h)
    }
}

impl<B, T: Clone> Clone for EndianPrimitive<B, T> {
    fn clone(&self) -> Self {
        EndianPrimitive {
            value: self.value.clone(),
            _phantom: PhantomData,
        }
    }
}

impl<B, T: Copy> Copy for EndianPrimitive<B, T> { }

/// Describes a value that can be converted to and from a specified byte order.
pub trait EndianConvert {
    /// Converts a value from `B`
    fn from<B: ByteOrder>(&self) -> Self;

    /// Converts a value to `B`
    fn to<B: ByteOrder>(self) -> Self;
}

macro_rules! endian_impl {
    ($t:ty: $s:expr => $r:ident, $w:ident) => {
        impl EndianConvert for $t {
            fn from<B: ByteOrder>(&self) -> Self {
                use std::mem::transmute;
                B::$r(unsafe { transmute::<_, &[u8; $s]>(self) })
            }

            fn to<B: ByteOrder>(self) -> Self {
                use std::mem::transmute;

                unsafe {
                    let mut s = uninitialized();
                    B::$w(transmute::<_, &mut [u8; $s]>(&mut s), self);
                    s
                }
            }
        }
    };
}

endian_impl!(u16: 2 => read_u16, write_u16);
endian_impl!(i16: 2 => read_i16, write_i16);
endian_impl!(i32: 4 => read_i32, write_i32);
endian_impl!(u32: 4 => read_u32, write_u32);
endian_impl!(i64: 8 => read_i64, write_i64);
endian_impl!(u64: 8 => read_u64, write_u64);
endian_impl!(f32: 4 => read_f32, write_f32);
endian_impl!(f64: 8 => read_f64, write_f64);

#[test]
fn endian_size() {
    use std::mem::size_of;
    type B = BigEndian;

    assert_eq!(size_of::<EndianPrimitive<B, i16>>(), 2);
    assert_eq!(size_of::<EndianPrimitive<B, i32>>(), 4);
    assert_eq!(size_of::<EndianPrimitive<B, i64>>(), 8);
    assert_eq!(size_of::<EndianPrimitive<B, f32>>(), 4);
    assert_eq!(size_of::<EndianPrimitive<B, f64>>(), 8);
}