1 use super::*; 2 3 /// Marker trait for "plain old data". 4 /// 5 /// The point of this trait is that once something is marked "plain old data" 6 /// you can really go to town with the bit fiddling and bit casting. Therefore, 7 /// it's a relatively strong claim to make about a type. Do not add this to your 8 /// type casually. 9 /// 10 /// **Reminder:** The results of casting around bytes between data types are 11 /// _endian dependant_. Little-endian machines are the most common, but 12 /// big-endian machines do exist (and big-endian is also used for "network 13 /// order" bytes). 14 /// 15 /// ## Safety 16 /// 17 /// * The type must be inhabited (eg: no 18 /// [Infallible](core::convert::Infallible)). 19 /// * The type must allow any bit pattern (eg: no `bool` or `char`, which have 20 /// illegal bit patterns). 21 /// * The type must not contain any padding bytes, either in the middle or on 22 /// the end (eg: no `#[repr(C)] struct Foo(u8, u16)`, which has padding in the 23 /// middle, and also no `#[repr(C)] struct Foo(u16, u8)`, which has padding on 24 /// the end). 25 /// * The type needs to have all fields also be `Pod`. 26 /// * The type needs to be `repr(C)` or `repr(transparent)`. In the case of 27 /// `repr(C)`, the `packed` and `align` repr modifiers can be used as long as 28 /// all other rules end up being followed. 29 pub unsafe trait Pod: Zeroable + Copy + 'static {} 30 31 unsafe impl Pod for () {} 32 unsafe impl Pod for u8 {} 33 unsafe impl Pod for i8 {} 34 unsafe impl Pod for u16 {} 35 unsafe impl Pod for i16 {} 36 unsafe impl Pod for u32 {} 37 unsafe impl Pod for i32 {} 38 unsafe impl Pod for u64 {} 39 unsafe impl Pod for i64 {} 40 unsafe impl Pod for usize {} 41 unsafe impl Pod for isize {} 42 unsafe impl Pod for u128 {} 43 unsafe impl Pod for i128 {} 44 unsafe impl Pod for f32 {} 45 unsafe impl Pod for f64 {} 46 unsafe impl<T: Pod> Pod for Wrapping<T> {} 47 48 unsafe impl Pod for Option<NonZeroI8> {} 49 unsafe impl Pod for Option<NonZeroI16> {} 50 unsafe impl Pod for Option<NonZeroI32> {} 51 unsafe impl Pod for Option<NonZeroI64> {} 52 unsafe impl Pod for Option<NonZeroI128> {} 53 unsafe impl Pod for Option<NonZeroIsize> {} 54 unsafe impl Pod for Option<NonZeroU8> {} 55 unsafe impl Pod for Option<NonZeroU16> {} 56 unsafe impl Pod for Option<NonZeroU32> {} 57 unsafe impl Pod for Option<NonZeroU64> {} 58 unsafe impl Pod for Option<NonZeroU128> {} 59 unsafe impl Pod for Option<NonZeroUsize> {} 60 61 unsafe impl<T: 'static> Pod for *mut T {} 62 unsafe impl<T: 'static> Pod for *const T {} 63 unsafe impl<T: 'static> Pod for Option<NonNull<T>> {} 64 unsafe impl<T: Pod> Pod for PhantomData<T> {} 65 unsafe impl<T: Pod> Pod for ManuallyDrop<T> {} 66 67 // Note(Lokathor): MaybeUninit can NEVER be Pod. 68 69 impl_unsafe_marker_for_array!( 70 Pod, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 71 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256, 72 512, 1024, 2048, 4096 73 ); 74 75 #[cfg(target_arch = "x86")] 76 unsafe impl Pod for x86::__m128i {} 77 #[cfg(target_arch = "x86")] 78 unsafe impl Pod for x86::__m128 {} 79 #[cfg(target_arch = "x86")] 80 unsafe impl Pod for x86::__m128d {} 81 #[cfg(target_arch = "x86")] 82 unsafe impl Pod for x86::__m256i {} 83 #[cfg(target_arch = "x86")] 84 unsafe impl Pod for x86::__m256 {} 85 #[cfg(target_arch = "x86")] 86 unsafe impl Pod for x86::__m256d {} 87 88 #[cfg(target_arch = "x86_64")] 89 unsafe impl Pod for x86_64::__m128i {} 90 #[cfg(target_arch = "x86_64")] 91 unsafe impl Pod for x86_64::__m128 {} 92 #[cfg(target_arch = "x86_64")] 93 unsafe impl Pod for x86_64::__m128d {} 94 #[cfg(target_arch = "x86_64")] 95 unsafe impl Pod for x86_64::__m256i {} 96 #[cfg(target_arch = "x86_64")] 97 unsafe impl Pod for x86_64::__m256 {} 98 #[cfg(target_arch = "x86_64")] 99 unsafe impl Pod for x86_64::__m256d {} 100