Skip to content

Commit b41aad0

Browse files
committed
alloc: restrict impl ZeroableOption for Box to T: Sized
Similar to what was done for `Zeroable<NonNull<T>>` in commit 9caa350 ("last docs changes and zeroable unsized pointer fixes"), 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]>
1 parent 1dbc383 commit b41aad0

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

Diff for: src/alloc.rs

+3-5
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)