Skip to content

Commit f7dfded

Browse files
ojeday86-dev
authored andcommitted
rust: pin-init: alloc: restrict impl ZeroableOption for Box to T: Sized
Similar to what was done for `Zeroable<NonNull<T>>` in commit df27cef ("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 <[email protected]> Link: Rust-for-Linux/pin-init@a6007cf Fixes: 9b2299a ("rust: pin-init: add `std` and `alloc` support from the user-space version") Cc: [email protected] [ Adjust mentioned commit to the one from the kernel. - Benno ] Signed-off-by: Benno Lossin <[email protected]>
1 parent a2cc6ff commit f7dfded

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

rust/pin-init/src/alloc.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ use crate::{
1717

1818
pub extern crate alloc;
1919

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> {}
2523

2624
/// Smart pointer that can initialize memory in-place.
2725
pub trait InPlaceInit<T>: Sized {

0 commit comments

Comments
 (0)