Skip to content

Commit 2e704f1

Browse files
y86-devojeda
authored andcommitted
rust: init: implement Zeroable for UnsafeCell<T> and Opaque<T>
`UnsafeCell<T>` and `T` have the same layout so if `T` is `Zeroable` then so should `UnsafeCell<T>` be. This allows using the derive macro for `Zeroable` on types that contain an `UnsafeCell<T>`. Since `Opaque<T>` contains a `MaybeUninit<T>`, all bytes zero is a valid bit pattern for that type. Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Signed-off-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 674b1c7 commit 2e704f1

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Diff for: rust/kernel/init.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@
202202
use crate::{
203203
error::{self, Error},
204204
sync::UniqueArc,
205-
types::ScopeGuard,
205+
types::{Opaque, ScopeGuard},
206206
};
207207
use alloc::boxed::Box;
208208
use core::{
209209
alloc::AllocError,
210+
cell::UnsafeCell,
210211
convert::Infallible,
211212
marker::PhantomData,
212213
mem::MaybeUninit,
@@ -1151,6 +1152,11 @@ impl_zeroable! {
11511152

11521153
// SAFETY: Type is allowed to take any value, including all zeros.
11531154
{<T>} MaybeUninit<T>,
1155+
// SAFETY: Type is allowed to take any value, including all zeros.
1156+
{<T>} Opaque<T>,
1157+
1158+
// SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
1159+
{<T: ?Sized + Zeroable>} UnsafeCell<T>,
11541160

11551161
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee).
11561162
Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,

0 commit comments

Comments
 (0)