Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <benno.lossin@proton.me>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Link: https://lore.kernel.org/r/20250308110339.2997091-11-benno.lossin@proton.me
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Benno Lossin and committed by
Miguel Ojeda
5657c3a9 9d29c682

+13 -2
+2 -2
rust/kernel/alloc/kbox.rs
··· 15 15 use core::ptr::NonNull; 16 16 use core::result::Result; 17 17 18 - use crate::init::{InPlaceWrite, Init, PinInit, Zeroable}; 18 + use crate::init::{InPlaceWrite, Init, PinInit, ZeroableOption}; 19 19 use crate::init_ext::InPlaceInit; 20 20 use crate::types::ForeignOwnable; 21 21 ··· 104 104 // 105 105 // In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant and there 106 106 // 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> {} 108 108 109 109 // SAFETY: `Box` is `Send` if `T` is `Send` because the `Box` owns a `T`. 110 110 unsafe impl<T, A> Send for Box<T, A>
+11
rust/pin-init/src/lib.rs
··· 1297 1297 /// ``` 1298 1298 pub unsafe trait Zeroable {} 1299 1299 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 + 1300 1311 /// Create a new zeroed T. 1301 1312 /// 1302 1313 /// The returned initializer will write `0x00` to every byte of the given `slot`.