mirror of
https://github.com/torvalds/linux.git
synced 2026-06-02 19:43:40 +02:00
rust: pin-init: alloc: restrict impl ZeroableOption for Box to T: Sized
Similar to what was done for `Zeroable<NonNull<T>>` in commitdf27cef153("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:a6007cf555Fixes:9b2299af3b("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>
This commit is contained in:
parent
05a2b0011c
commit
3c75fff196
|
|
@ -17,11 +17,9 @@
|
|||
|
||||
pub extern crate alloc;
|
||||
|
||||
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee).
|
||||
//
|
||||
// In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant and there
|
||||
// is no problem with a VTABLE pointer being null.
|
||||
unsafe impl<T: ?Sized> ZeroableOption for Box<T> {}
|
||||
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
|
||||
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
|
||||
unsafe impl<T> ZeroableOption for Box<T> {}
|
||||
|
||||
/// Smart pointer that can initialize memory in-place.
|
||||
pub trait InPlaceInit<T>: Sized {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user