Skip to content

Commit 94f00d7

Browse files
committed
FromZeroes for Option<T> where T is subject to NPO
For thin pointer types, `P` (`Box<T>`, `&T`, `&mut T`, and `NonNull<T>` where `T: Sized`), implement `FromZeroes` for `Option<P>`. Release 0.7.25. Closes #293
1 parent c9772d9 commit 94f00d7

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
[package]
1616
edition = "2018"
1717
name = "zerocopy"
18-
version = "0.7.24"
18+
version = "0.7.25"
1919
authors = ["Joshua Liebow-Feeser <[email protected]>"]
2020
description = "Utilities for zero-copy parsing and serialization"
2121
license = "BSD-2-Clause OR Apache-2.0 OR MIT"
@@ -49,7 +49,7 @@ simd-nightly = ["simd"]
4949
__internal_use_only_features_that_work_on_stable = ["alloc", "derive", "simd"]
5050

5151
[dependencies]
52-
zerocopy-derive = { version = "=0.7.24", path = "zerocopy-derive", optional = true }
52+
zerocopy-derive = { version = "=0.7.25", path = "zerocopy-derive", optional = true }
5353

5454
[dependencies.byteorder]
5555
version = "1.3"
@@ -60,7 +60,7 @@ optional = true
6060
# zerocopy-derive remain equal, even if the 'derive' feature isn't used.
6161
# See: https://github.com/matklad/macro-dep-test
6262
[target.'cfg(any())'.dependencies]
63-
zerocopy-derive = { version = "=0.7.24", path = "zerocopy-derive" }
63+
zerocopy-derive = { version = "=0.7.25", path = "zerocopy-derive" }
6464

6565
[dev-dependencies]
6666
assert_matches = "1.5"
@@ -75,6 +75,6 @@ testutil = { path = "testutil" }
7575
# CI test failures.
7676
trybuild = { version = "=1.0.85", features = ["diff"] }
7777
# In tests, unlike in production, zerocopy-derive is not optional
78-
zerocopy-derive = { version = "=0.7.24", path = "zerocopy-derive" }
78+
zerocopy-derive = { version = "=0.7.25", path = "zerocopy-derive" }
7979
# TODO(#381) Remove this dependency once we have our own layout gadgets.
8080
elain = "0.3.0"

src/lib.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,37 @@ safety_comment! {
18801880
unsafe_impl!(Option<NonZeroIsize>: FromZeroes, FromBytes, AsBytes);
18811881
}
18821882

1883+
safety_comment! {
1884+
/// SAFETY:
1885+
/// The following types can be transmuted from `[0u8; size_of::<T>()]`. [1]
1886+
/// None of them contain `UnsafeCell`s, and so they all soundly implement
1887+
/// `FromZeroes`.
1888+
///
1889+
/// [1] Per
1890+
/// https://doc.rust-lang.org/nightly/core/option/index.html#representation:
1891+
///
1892+
/// Rust guarantees to optimize the following types `T` such that
1893+
/// [`Option<T>`] has the same size and alignment as `T`. In some of these
1894+
/// cases, Rust further guarantees that `transmute::<_, Option<T>>([0u8;
1895+
/// size_of::<T>()])` is sound and produces `Option::<T>::None`. These
1896+
/// cases are identified by the second column:
1897+
///
1898+
/// | `T` | `transmute::<_, Option<T>>([0u8; size_of::<T>()])` sound? |
1899+
/// |---------------------|-----------------------------------------------------------|
1900+
/// | [`Box<U>`] | when `U: Sized` |
1901+
/// | `&U` | when `U: Sized` |
1902+
/// | `&mut U` | when `U: Sized` |
1903+
/// | [`ptr::NonNull<U>`] | when `U: Sized` |
1904+
///
1905+
/// TODO(#429), TODO(https://github.com/rust-lang/rust/pull/115333): Cite
1906+
/// the Stable docs once they're available.
1907+
#[cfg(feature = "alloc")]
1908+
unsafe_impl!(T => FromZeroes for Option<Box<T>>);
1909+
unsafe_impl!(T => FromZeroes for Option<&'_ T>);
1910+
unsafe_impl!(T => FromZeroes for Option<&'_ mut T>);
1911+
unsafe_impl!(T => FromZeroes for Option<NonNull<T>>);
1912+
}
1913+
18831914
safety_comment! {
18841915
/// SAFETY:
18851916
/// For all `T`, `PhantomData<T>` has size 0 and alignment 1. [1]
@@ -3820,7 +3851,7 @@ pub use alloc_support::*;
38203851
mod tests {
38213852
#![allow(clippy::unreadable_literal)]
38223853

3823-
use core::{convert::TryInto as _, ops::Deref};
3854+
use core::{cell::UnsafeCell, convert::TryInto as _, ops::Deref};
38243855

38253856
use static_assertions::assert_impl_all;
38263857

@@ -5515,6 +5546,16 @@ mod tests {
55155546
// Implements none of the ZC traits.
55165547
struct NotZerocopy;
55175548

5549+
#[cfg(feature = "alloc")]
5550+
assert_impls!(Option<Box<UnsafeCell<NotZerocopy>>>: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5551+
assert_impls!(Option<Box<[UnsafeCell<NotZerocopy>]>>: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5552+
assert_impls!(Option<&'static UnsafeCell<NotZerocopy>>: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5553+
assert_impls!(Option<&'static [UnsafeCell<NotZerocopy>]>: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5554+
assert_impls!(Option<&'static mut UnsafeCell<NotZerocopy>>: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5555+
assert_impls!(Option<&'static mut [UnsafeCell<NotZerocopy>]>: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5556+
assert_impls!(Option<NonNull<UnsafeCell<NotZerocopy>>>: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5557+
assert_impls!(Option<NonNull<[UnsafeCell<NotZerocopy>]>>: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5558+
55185559
assert_impls!(PhantomData<NotZerocopy>: KnownLayout, FromZeroes, FromBytes, AsBytes, Unaligned);
55195560
assert_impls!(PhantomData<[u8]>: KnownLayout, FromZeroes, FromBytes, AsBytes, Unaligned);
55205561

src/macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
/// The macro invocations are emitted, each decorated with the following
2727
/// attribute: `#[allow(clippy::undocumented_unsafe_blocks)]`.
2828
macro_rules! safety_comment {
29-
(#[doc = r" SAFETY:"] $($(#[doc = $_doc:literal])* $macro:ident!$args:tt;)*) => {
30-
#[allow(clippy::undocumented_unsafe_blocks)]
31-
const _: () = { $($macro!$args;)* };
29+
(#[doc = r" SAFETY:"] $($(#[$attr:meta])* $macro:ident!$args:tt;)*) => {
30+
#[allow(clippy::undocumented_unsafe_blocks, unused_attributes)]
31+
const _: () = { $($(#[$attr])* $macro!$args;)* };
3232
}
3333
}
3434

zerocopy-derive/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[package]
1010
edition = "2018"
1111
name = "zerocopy-derive"
12-
version = "0.7.24"
12+
version = "0.7.25"
1313
authors = ["Joshua Liebow-Feeser <[email protected]>"]
1414
description = "Custom derive for traits from the zerocopy crate"
1515
license = "BSD-2-Clause OR Apache-2.0 OR MIT"

0 commit comments

Comments
 (0)