Skip to content

Commit 5657c3a

Browse files
BennoLossinojeda
authored andcommitted
rust: add ZeroableOption and implement it instead of Zeroable for Option<Box<T, A>>
When making pin-init its own crate, `Zeroable` will no longer be defined by the kernel crate and thus implementing it for `Option<Box<T, A>>` is no longer possible due to the orphan rule. For this reason introduce a new `ZeroableOption` trait that circumvents this problem. Signed-off-by: Benno Lossin <[email protected]> Reviewed-by: Fiona Behrens <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Tested-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 9d29c68 commit 5657c3a

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

rust/kernel/alloc/kbox.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use core::pin::Pin;
1515
use core::ptr::NonNull;
1616
use core::result::Result;
1717

18-
use crate::init::{InPlaceWrite, Init, PinInit, Zeroable};
18+
use crate::init::{InPlaceWrite, Init, PinInit, ZeroableOption};
1919
use crate::init_ext::InPlaceInit;
2020
use crate::types::ForeignOwnable;
2121

@@ -104,7 +104,7 @@ pub type KVBox<T> = Box<T, super::allocator::KVmalloc>;
104104
//
105105
// In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant and there
106106
// is no problem with a VTABLE pointer being null.
107-
unsafe impl<T: ?Sized, A: Allocator> Zeroable for Option<Box<T, A>> {}
107+
unsafe impl<T: ?Sized, A: Allocator> ZeroableOption for Box<T, A> {}
108108

109109
// SAFETY: `Box` is `Send` if `T` is `Send` because the `Box` owns a `T`.
110110
unsafe impl<T, A> Send for Box<T, A>

rust/pin-init/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,17 @@ pub unsafe trait PinnedDrop: __internal::HasPinData {
12971297
/// ```
12981298
pub unsafe trait Zeroable {}
12991299

1300+
/// Marker trait for types that allow `Option<Self>` to be set to all zeroes in order to write
1301+
/// `None` to that location.
1302+
///
1303+
/// # Safety
1304+
///
1305+
/// The implementer needs to ensure that `unsafe impl Zeroable for Option<Self> {}` is sound.
1306+
pub unsafe trait ZeroableOption {}
1307+
1308+
// SAFETY: by the safety requirement of `ZeroableOption`, this is valid.
1309+
unsafe impl<T: ZeroableOption> Zeroable for Option<T> {}
1310+
13001311
/// Create a new zeroed T.
13011312
///
13021313
/// The returned initializer will write `0x00` to every byte of the given `slot`.

0 commit comments

Comments
 (0)