1 use super::*;
2 
3 /// Trait for types that can be safely created with
4 /// [`zeroed`](core::mem::zeroed).
5 ///
6 /// An all-zeroes value may or may not be the same value as the
7 /// [Default](core::default::Default) value of the type.
8 ///
9 /// ## Safety
10 ///
11 /// * Your type must be inhabited (eg: no
12 ///   [Infallible](core::convert::Infallible)).
13 /// * Your type must be allowed to be an "all zeroes" bit pattern (eg: no
14 ///   [`NonNull<T>`](core::ptr::NonNull)).
15 pub unsafe trait Zeroable: Sized {
16   /// Calls [`zeroed`](core::mem::zeroed).
17   ///
18   /// This is a trait method so that you can write `MyType::zeroed()` in your
19   /// code. It is a contract of this trait that if you implement it on your type
20   /// you **must not** override this method.
21   #[inline]
zeroed() -> Self22   fn zeroed() -> Self {
23     unsafe { core::mem::zeroed() }
24   }
25 }
26 unsafe impl Zeroable for () {}
27 unsafe impl Zeroable for bool {}
28 unsafe impl Zeroable for char {}
29 unsafe impl Zeroable for u8 {}
30 unsafe impl Zeroable for i8 {}
31 unsafe impl Zeroable for u16 {}
32 unsafe impl Zeroable for i16 {}
33 unsafe impl Zeroable for u32 {}
34 unsafe impl Zeroable for i32 {}
35 unsafe impl Zeroable for u64 {}
36 unsafe impl Zeroable for i64 {}
37 unsafe impl Zeroable for usize {}
38 unsafe impl Zeroable for isize {}
39 unsafe impl Zeroable for u128 {}
40 unsafe impl Zeroable for i128 {}
41 unsafe impl Zeroable for f32 {}
42 unsafe impl Zeroable for f64 {}
43 unsafe impl<T: Zeroable> Zeroable for Wrapping<T> {}
44 
45 unsafe impl Zeroable for Option<NonZeroI8> {}
46 unsafe impl Zeroable for Option<NonZeroI16> {}
47 unsafe impl Zeroable for Option<NonZeroI32> {}
48 unsafe impl Zeroable for Option<NonZeroI64> {}
49 unsafe impl Zeroable for Option<NonZeroI128> {}
50 unsafe impl Zeroable for Option<NonZeroIsize> {}
51 unsafe impl Zeroable for Option<NonZeroU8> {}
52 unsafe impl Zeroable for Option<NonZeroU16> {}
53 unsafe impl Zeroable for Option<NonZeroU32> {}
54 unsafe impl Zeroable for Option<NonZeroU64> {}
55 unsafe impl Zeroable for Option<NonZeroU128> {}
56 unsafe impl Zeroable for Option<NonZeroUsize> {}
57 
58 unsafe impl<T> Zeroable for *mut T {}
59 unsafe impl<T> Zeroable for *const T {}
60 unsafe impl<T> Zeroable for Option<NonNull<T>> {}
61 unsafe impl<T: Zeroable> Zeroable for PhantomData<T> {}
62 unsafe impl<T: Zeroable> Zeroable for ManuallyDrop<T> {}
63 
64 #[cfg(feature = "zeroable_maybe_uninit")]
65 unsafe impl<T> Zeroable for core::mem::MaybeUninit<T> {}
66 
67 unsafe impl<A: Zeroable> Zeroable for (A,) {}
68 unsafe impl<A: Zeroable, B: Zeroable> Zeroable for (A, B) {}
69 unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable> Zeroable for (A, B, C) {}
70 unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable, D: Zeroable> Zeroable
71   for (A, B, C, D)
72 {
73 }
74 unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable, D: Zeroable, E: Zeroable>
75   Zeroable for (A, B, C, D, E)
76 {
77 }
78 unsafe impl<
79     A: Zeroable,
80     B: Zeroable,
81     C: Zeroable,
82     D: Zeroable,
83     E: Zeroable,
84     F: Zeroable,
85   > Zeroable for (A, B, C, D, E, F)
86 {
87 }
88 unsafe impl<
89     A: Zeroable,
90     B: Zeroable,
91     C: Zeroable,
92     D: Zeroable,
93     E: Zeroable,
94     F: Zeroable,
95     G: Zeroable,
96   > Zeroable for (A, B, C, D, E, F, G)
97 {
98 }
99 unsafe impl<
100     A: Zeroable,
101     B: Zeroable,
102     C: Zeroable,
103     D: Zeroable,
104     E: Zeroable,
105     F: Zeroable,
106     G: Zeroable,
107     H: Zeroable,
108   > Zeroable for (A, B, C, D, E, F, G, H)
109 {
110 }
111 
112 impl_unsafe_marker_for_array!(
113   Zeroable, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
114   19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256,
115   512, 1024, 2048, 4096
116 );
117 
118 #[cfg(target_arch = "x86")]
119 unsafe impl Zeroable for x86::__m128i {}
120 #[cfg(target_arch = "x86")]
121 unsafe impl Zeroable for x86::__m128 {}
122 #[cfg(target_arch = "x86")]
123 unsafe impl Zeroable for x86::__m128d {}
124 #[cfg(target_arch = "x86")]
125 unsafe impl Zeroable for x86::__m256i {}
126 #[cfg(target_arch = "x86")]
127 unsafe impl Zeroable for x86::__m256 {}
128 #[cfg(target_arch = "x86")]
129 unsafe impl Zeroable for x86::__m256d {}
130 
131 #[cfg(target_arch = "x86_64")]
132 unsafe impl Zeroable for x86_64::__m128i {}
133 #[cfg(target_arch = "x86_64")]
134 unsafe impl Zeroable for x86_64::__m128 {}
135 #[cfg(target_arch = "x86_64")]
136 unsafe impl Zeroable for x86_64::__m128d {}
137 #[cfg(target_arch = "x86_64")]
138 unsafe impl Zeroable for x86_64::__m256i {}
139 #[cfg(target_arch = "x86_64")]
140 unsafe impl Zeroable for x86_64::__m256 {}
141 #[cfg(target_arch = "x86_64")]
142 unsafe impl Zeroable for x86_64::__m256d {}
143