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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use std::mem::{size_of, transmute, uninitialized};
use std::slice::{from_raw_parts, from_raw_parts_mut};
use packed::{Unaligned, Aligned};
use uninitialized;

use self::unstable::{box_from, box_into};

/// A marker trait indicating that a type is Plain Old Data.
///
/// It is unsafe to `impl` this manually, use `#[derive(Pod)]` instead.
pub unsafe trait Pod: Sized {
    #[doc(hidden)]
    fn __assert_pod() { }

    /// Safely borrows the aligned value mutably
    ///
    /// See also: `Aligned::as_aligned_mut`
    #[inline]
    fn mut_aligned<T: Pod + Aligned<Unaligned=Self>>(&mut self) -> Option<&mut T> where Self: Copy + Unaligned {
        unsafe { T::as_aligned_mut(self) }
    }

    /// Safely borrows the unaligned value mutably
    ///
    /// See also: `Aligned::as_unaligned_mut`
    #[inline]
    fn mut_unaligned<T: Copy + Unaligned>(s: &mut T) -> Option<&mut Self> where Self: Aligned<Unaligned=T> {
        unsafe { Self::as_aligned_mut(s) }
    }

    /// Safely converts an unaligned value to its aligned equivalent
    ///
    /// See also: `Aligned::from_unaligned`
    #[inline]
    fn aligned<T: Copy + Unaligned>(s: T) -> Self where Self: Aligned<Unaligned=T> {
        unsafe { Self::from_unaligned(s) }
    }

    /// Borrows the POD as a byte slice
    #[inline]
    fn as_slice<'a>(&'a self) -> &'a [u8] {
        unsafe { from_raw_parts(self as *const Self as *const u8, size_of::<Self>()) }
    }

    /// Borrows the POD as a mutable byte slice
    #[inline]
    fn mut_slice<'a>(&'a mut self) -> &'a mut [u8] {
        unsafe { from_raw_parts_mut(self as *mut Self as *mut u8, size_of::<Self>()) }
    }

    /// Borrows a new instance of the POD from a byte slice
    ///
    /// # Panics
    ///
    /// Panics if `slice.len()` is not the same as the type's size
    #[inline]
    fn from_slice<'a>(slice: &'a [u8]) -> &'a Self where Self: Unaligned {
        assert_eq!(slice.len(), size_of::<Self>());
        unsafe { &*(slice.as_ptr() as *const _) }
    }

    /// Borrows a mutable instance of the POD from a mutable byte slice
    ///
    /// # Panics
    ///
    /// Panics if `slice.len()` is not the same as the type's size
    #[inline]
    fn from_mut_slice<'a>(slice: &'a mut [u8]) -> &'a mut Self where Self: Unaligned {
        assert_eq!(slice.len(), size_of::<Self>());
        unsafe { &mut *(slice.as_mut_ptr() as *mut _) }
    }

    /// Converts a byte vector to a boxed instance of the POD type
    ///
    /// # Panics
    ///
    /// Panics if `vec.len()` is not the same as the type's size
    #[inline]
    fn from_vec(vec: Vec<u8>) -> Box<Self> where Self: Unaligned {
        Self::from_box(vec.into_boxed_slice())
    }

    /// Converts a boxed slice to a boxed instance of the POD type
    ///
    /// # Panics
    ///
    /// Panics if `slice.len()` is not the same as the type's size
    #[inline]
    fn from_box(slice: Box<[u8]>) -> Box<Self> where Self: Unaligned {
        assert!(slice.len() == size_of::<Self>());
        unsafe {
            box_from((&mut *box_into(slice)).as_mut_ptr() as *mut _)
        }
    }

    /// Converts a boxed POD to a byte vector
    #[inline]
    fn to_vec(self: Box<Self>) -> Vec<u8> {
        self.to_boxed_slice().into_vec()
    }

    /// Converts a boxed POD to a boxed slice
    #[inline]
    fn to_boxed_slice(self: Box<Self>) -> Box<[u8]> {
        let ptr = box_into(self);
        unsafe {
            let ptr = from_raw_parts_mut(ptr as *mut u8, size_of::<Self>());
            box_from(ptr)
        }
    }

    /// Converts a POD type from one to another of the same size.
    ///
    /// # Panics
    ///
    /// Panics if the two types are not the same size
    #[inline]
    fn map<'a, T: Pod + Unaligned>(&'a self) -> &'a T where Self: Unaligned {
        assert_eq!(size_of::<Self>(), size_of::<T>());
        unsafe {
            transmute(self)
        }
    }

    /// Converts a POD type from one to another of the same size.
    ///
    /// # Panics
    ///
    /// Panics if the two types are not the same size
    #[inline]
    fn map_mut<'a, T: Pod + Unaligned>(&'a mut self) -> &'a mut T where Self: Unaligned {
        assert_eq!(size_of::<Self>(), size_of::<T>());
        unsafe {
            transmute(self)
        }
    }

    /// Generates a new uninitialized instance of a POD type.
    #[inline]
    unsafe fn uninitialized() -> Self {
        uninitialized()
    }

    /// Creates a new zeroed instance of a POD type.
    #[inline]
    fn zeroed() -> Self {
        unsafe { uninitialized::uninitialized() }
    }

    /// Maps a POD slice from one type to another
    ///
    /// # Panics
    ///
    /// Will panic if the output type does not perfectly fit into the slice.
    #[inline]
    fn map_slice<'a, T: Pod + Unaligned>(s: &'a [Self]) -> &'a [T] where Self: Unaligned {
        let len = s.len() * size_of::<Self>();
        assert_eq!(len % size_of::<T>(), 0);
        unsafe { from_raw_parts(s.as_ptr() as *const T, len / size_of::<T>()) }
    }

    /// Maps a mutable POD slice from one type to another
    ///
    /// # Panics
    ///
    /// Will panic if the output type does not perfectly fit into the slice.
    #[inline]
    fn map_mut_slice<'a, T: Pod + Unaligned>(s: &'a mut [Self]) -> &'a mut [T] where Self: Unaligned {
        let len = s.len() * size_of::<Self>();
        assert_eq!(len % size_of::<T>(), 0);
        unsafe { from_raw_parts_mut(s.as_mut_ptr() as *mut T, len / size_of::<T>()) }
    }
}

unsafe impl Pod for () { }
unsafe impl Pod for f32 { }
unsafe impl Pod for f64 { }
unsafe impl Pod for i8 { }
unsafe impl Pod for u8 { }
unsafe impl Pod for i16 { }
unsafe impl Pod for u16 { }
unsafe impl Pod for i32 { }
unsafe impl Pod for u32 { }
unsafe impl Pod for i64 { }
unsafe impl Pod for u64 { }
unsafe impl Pod for isize { }
unsafe impl Pod for usize { }
unsafe impl<T> Pod for *const T { }
unsafe impl<T> Pod for *mut T { }

macro_rules! pod_def {
    ($($x:expr),*) => {
        $(
            unsafe impl<T: Pod> Pod for [T; $x] { }
        )*
    };
}

unsafe impl<T: Pod> Pod for (T,) { }
pod_def! { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }
pod_def! { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }
pod_def! { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f }
pod_def! { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f }
pod_def! { 0x40 }

#[cfg(feature = "unstable")]
mod unstable {
    pub unsafe fn box_from<T: ?Sized>(raw: *mut T) -> Box<T> { Box::from_raw(raw) }
    pub fn box_into<T: ?Sized>(b: Box<T>) -> *mut T { Box::into_raw(b) }
}

#[cfg(not(feature = "unstable"))]
mod unstable {
    use std::mem::transmute;

    pub unsafe fn box_from<T: ?Sized>(raw: *mut T) -> Box<T> { transmute(raw) }
    pub fn box_into<T: ?Sized>(b: Box<T>) -> *mut T { unsafe { transmute(b) } }
}