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 #[cfg(feature = "unsound_ptr_pod_impl")]
62 unsafe impl<T: 'static> Pod for *mut T {}
63 #[cfg(feature = "unsound_ptr_pod_impl")]
64 unsafe impl<T: 'static> Pod for *const T {}
65 #[cfg(feature = "unsound_ptr_pod_impl")]
66 unsafe impl<T: 'static> Pod for Option<NonNull<T>> {}
67 
68 unsafe impl<T: Pod> Pod for PhantomData<T> {}
69 unsafe impl Pod for PhantomPinned {}
70 unsafe impl<T: Pod> Pod for ManuallyDrop<T> {}
71 
72 // Note(Lokathor): MaybeUninit can NEVER be Pod.
73 
74 #[cfg(feature = "min_const_generics")]
75 unsafe impl<T, const N: usize> Pod for [T; N] where T: Pod {}
76 
77 #[cfg(not(feature = "min_const_generics"))]
78 impl_unsafe_marker_for_array!(
79   Pod, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
80   20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256,
81   512, 1024, 2048, 4096
82 );
83 
84 #[cfg(all(target_arch = "wasm32", feature = "wasm_simd"))]
85 unsafe impl Pod for wasm32::v128 {}
86 
87 #[cfg(target_arch = "x86")]
88 unsafe impl Pod for x86::__m128i {}
89 #[cfg(target_arch = "x86")]
90 unsafe impl Pod for x86::__m128 {}
91 #[cfg(target_arch = "x86")]
92 unsafe impl Pod for x86::__m128d {}
93 #[cfg(target_arch = "x86")]
94 unsafe impl Pod for x86::__m256i {}
95 #[cfg(target_arch = "x86")]
96 unsafe impl Pod for x86::__m256 {}
97 #[cfg(target_arch = "x86")]
98 unsafe impl Pod for x86::__m256d {}
99 
100 #[cfg(target_arch = "x86_64")]
101 unsafe impl Pod for x86_64::__m128i {}
102 #[cfg(target_arch = "x86_64")]
103 unsafe impl Pod for x86_64::__m128 {}
104 #[cfg(target_arch = "x86_64")]
105 unsafe impl Pod for x86_64::__m128d {}
106 #[cfg(target_arch = "x86_64")]
107 unsafe impl Pod for x86_64::__m256i {}
108 #[cfg(target_arch = "x86_64")]
109 unsafe impl Pod for x86_64::__m256 {}
110 #[cfg(target_arch = "x86_64")]
111 unsafe impl Pod for x86_64::__m256d {}
112 
113 #[cfg(feature = "nightly_portable_simd")]
114 unsafe impl<T, const N: usize> Pod for core::simd::Simd<T, N>
115 where
116   T: core::simd::SimdElement + Pod,
117   core::simd::LaneCount<N>: core::simd::SupportedLaneCount,
118 {
119 }
120