@@ -14,6 +14,7 @@ pub(crate) use ::alloc::boxed::Box;
1414pub ( crate ) use core:: fmt:: Debug ;
1515pub ( crate ) use core:: marker:: PhantomData ;
1616pub ( crate ) use core:: sync:: atomic:: { AtomicI32 , AtomicU32 , AtomicU64 } ;
17+ pub ( crate ) use kernel:: init:: Zeroable ;
1718pub ( crate ) use kernel:: macros:: versions;
1819
1920// Make the trait visible
@@ -53,7 +54,7 @@ pub(crate) struct FwStamp(pub(crate) AtomicU32);
5354#[ repr( C , packed( 1 ) ) ]
5455pub ( crate ) struct U64 ( pub ( crate ) u64 ) ;
5556
56- unsafe impl Zeroed for U64 { }
57+ unsafe impl Zeroable for U64 { }
5758
5859impl fmt:: Debug for U64 {
5960 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -70,7 +71,7 @@ impl fmt::Debug for U64 {
7071#[ repr( C , packed( 1 ) ) ]
7172pub ( crate ) struct U32 ( pub ( crate ) u32 ) ;
7273
73- unsafe impl Zeroed for U32 { }
74+ unsafe impl Zeroable for U32 { }
7475
7576impl fmt:: Debug for U32 {
7677 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -79,16 +80,6 @@ impl fmt::Debug for U32 {
7980 }
8081}
8182
82- unsafe impl Zeroed for u8 { }
83- unsafe impl Zeroed for u16 { }
84- unsafe impl Zeroed for u32 { }
85- unsafe impl Zeroed for u64 { }
86- unsafe impl Zeroed for i8 { }
87- unsafe impl Zeroed for i16 { }
88- unsafe impl Zeroed for i32 { }
89- unsafe impl Zeroed for i64 { }
90- unsafe impl Zeroed for F32 { }
91-
9283/// Create a dummy `Debug` implementation, for when we need it but it's too painful to write by
9384/// hand or not very useful.
9485#[ macro_export]
@@ -102,46 +93,32 @@ macro_rules! no_debug {
10293 } ;
10394}
10495
105- /// Types which can be safely initialized with an all-zero bit pattern.
106- ///
107- /// See: https://github.com/rust-lang/rfcs/issues/2626
108- ///
109- /// # Safety
110- ///
111- /// This trait must only be implemented if a type only contains primitive types which can be
112- /// zero-initialized, FFI structs intended to be zero-initialized, or other types which impl Zeroed.
113- pub ( crate ) unsafe trait Zeroed : Default {
114- fn zeroed ( ) -> Self {
115- // SAFETY: The user is responsible for ensuring this is safe.
116- unsafe { core:: mem:: zeroed ( ) }
117- }
118- }
119-
120- /// Implement Zeroed for a given type (and Default along with it).
96+ /// Implement Zeroable for a given type (and Default along with it).
12197///
12298/// # Safety
12399///
124100/// This macro must only be used if a type only contains primitive types which can be
125- /// zero-initialized, FFI structs intended to be zero-initialized, or other types which impl Zeroed.
101+ /// zero-initialized, FFI structs intended to be zero-initialized, or other types which
102+ /// impl Zeroable.
126103#[ macro_export]
127104macro_rules! default_zeroed {
128105 ( <$( $lt: lifetime) ,* >, $type: ty) => {
129106 impl <$( $lt) ,* > Default for $type {
130107 fn default ( ) -> $type {
131- Zeroed :: zeroed( )
108+ :: kernel :: init :: Zeroable :: zeroed( )
132109 }
133110 }
134111 // SAFETY: The user is responsible for ensuring this is safe.
135- unsafe impl <$( $lt) ,* > Zeroed for $type { }
112+ unsafe impl <$( $lt) ,* > :: kernel :: init :: Zeroable for $type { }
136113 } ;
137114 ( $type: ty) => {
138115 impl Default for $type {
139116 fn default ( ) -> $type {
140- Zeroed :: zeroed( )
117+ :: kernel :: init :: Zeroable :: zeroed( )
141118 }
142119 }
143120 // SAFETY: The user is responsible for ensuring this is safe.
144- unsafe impl Zeroed for $type { }
121+ unsafe impl :: kernel :: init :: Zeroable for $type { }
145122 } ;
146123}
147124
@@ -151,11 +128,11 @@ macro_rules! default_zeroed {
151128pub ( crate ) struct Pad < const N : usize > ( [ u8 ; N ] ) ;
152129
153130/// SAFETY: Primitive type, safe to zero-init.
154- unsafe impl < const N : usize > Zeroed for Pad < N > { }
131+ unsafe impl < const N : usize > Zeroable for Pad < N > { }
155132
156133impl < const N : usize > Default for Pad < N > {
157134 fn default ( ) -> Self {
158- Zeroed :: zeroed ( )
135+ Zeroable :: zeroed ( )
159136 }
160137}
161138
@@ -165,7 +142,7 @@ impl<const N: usize> fmt::Debug for Pad<N> {
165142 }
166143}
167144
168- /// A convenience type for a fixed-sized array with Default/Zeroed impls.
145+ /// A convenience type for a fixed-sized array with Default/Zeroable impls.
169146#[ derive( Copy , Clone ) ]
170147#[ repr( C ) ]
171148pub ( crate ) struct Array < const N : usize , T > ( [ T ; N ] ) ;
@@ -176,12 +153,12 @@ impl<const N: usize, T> Array<N, T> {
176153 }
177154}
178155
179- // SAFETY: Arrays of Zeroed values can be safely Zeroed .
180- unsafe impl < const N : usize , T : Zeroed > Zeroed for Array < N , T > { }
156+ // SAFETY: Arrays of Zeroable values can be safely Zeroable .
157+ unsafe impl < const N : usize , T : Zeroable > Zeroable for Array < N , T > { }
181158
182- impl < const N : usize , T : Zeroed > Default for Array < N , T > {
159+ impl < const N : usize , T : Zeroable > Default for Array < N , T > {
183160 fn default ( ) -> Self {
184- Zeroed :: zeroed ( )
161+ Zeroable :: zeroed ( )
185162 }
186163}
187164
@@ -224,11 +201,12 @@ impl<const N: usize, T: Sized + fmt::Debug> fmt::Debug for Array<N, T> {
224201#[ macro_export]
225202macro_rules! trivial_gpustruct {
226203 ( $type: ident) => {
227- #[ derive( Debug , Default ) ]
204+ #[ derive( Debug ) ]
228205 pub ( crate ) struct $type { }
229206
230207 impl GpuStruct for $type {
231208 type Raw <' a> = raw:: $type;
232209 }
210+ $crate:: default_zeroed!( $type) ;
233211 } ;
234212}
0 commit comments