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: pin-init: alloc: restrict `impl ZeroableOption` for `Box` to `T: Sized`

Similar to what was done for `Zeroable<NonNull<T>>` in commit
df27cef15360 ("rust: init: fix `Zeroable` implementation for
`Option<NonNull<T>>` and `Option<KBox<T>>`"), the latest Rust
documentation [1] says it guarantees that `transmute::<_,
Option<T>>([0u8; size_of::<T>()])` is sound and produces
`Option::<T>::None` only in some cases. In particular, it says:

`Box<U>` (specifically, only `Box<U, Global>`) when `U: Sized`

Thus restrict the `impl` to `Sized`, and use similar wording as in that
commit too.

Link: https://doc.rust-lang.org/stable/std/option/index.html#representation [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/pin-init/pull/32/commits/a6007cf555e5946bcbfafe93a6468c329078acd8
Fixes: 9b2299af3b92 ("rust: pin-init: add `std` and `alloc` support from the user-space version")
Cc: stable@vger.kernel.org
[ Adjust mentioned commit to the one from the kernel. - Benno ]
Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Link: https://lore.kernel.org/r/20250407201755.649153-2-benno.lossin@proton.me
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

+3 -5
+3 -5
rust/pin-init/src/alloc.rs
··· 17 17 18 18 pub extern crate alloc; 19 19 20 - // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee). 21 - // 22 - // In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant and there 23 - // is no problem with a VTABLE pointer being null. 24 - unsafe impl<T: ?Sized> ZeroableOption for Box<T> {} 20 + // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee: 21 + // <https://doc.rust-lang.org/stable/std/option/index.html#representation>). 22 + unsafe impl<T> ZeroableOption for Box<T> {} 25 23 26 24 /// Smart pointer that can initialize memory in-place. 27 25 pub trait InPlaceInit<T>: Sized {