Skip to content

Commit

Permalink
Auto merge of #115333 - joshlf:patch-5, r=RalfJung
Browse files Browse the repository at this point in the history
Guarantee representation of None in NPO

This allows users to soundly transmute zeroes into `Option` types subject to the null pointer optimization (NPO). It unblocks google/zerocopy#293.
  • Loading branch information
bors committed Nov 3, 2023
2 parents 9c20ddd + 9703cb2 commit 1bb6553
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,21 @@
//! # Representation
//!
//! Rust guarantees to optimize the following types `T` such that
//! [`Option<T>`] has the same size and alignment as `T`:
//!
//! * [`Box<U>`]
//! * `&U`
//! * `&mut U`
//! * `fn`, `extern "C" fn`[^extern_fn]
//! * [`num::NonZero*`]
//! * [`ptr::NonNull<U>`]
//! * `#[repr(transparent)]` struct around one of the types in this list.
//! [`Option<T>`] has the same size and alignment as `T`. In some
//! of these cases, Rust further guarantees that
//! `transmute::<_, Option<T>>([0u8; size_of::<T>()])` is sound and
//! produces `Option::<T>::None`. These cases are identified by the
//! second column:
//!
//! | `T` | `transmute::<_, Option<T>>([0u8; size_of::<T>()])` sound? |
//! |---------------------------------------------------------------------|----------------------------------------------------------------------|
//! | [`Box<U>`] | when `U: Sized` |
//! | `&U` | when `U: Sized` |
//! | `&mut U` | when `U: Sized` |
//! | `fn`, `extern "C" fn`[^extern_fn] | always |
//! | [`num::NonZero*`] | always |
//! | [`ptr::NonNull<U>`] | when `U: Sized` |
//! | `#[repr(transparent)]` struct around one of the types in this list. | when it holds for the inner type |
//!
//! [^extern_fn]: this remains true for any other ABI: `extern "abi" fn` (_e.g._, `extern "system" fn`)
//!
Expand Down

0 comments on commit 1bb6553

Please sign in to comment.