mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-02 01:52:51 +00:00
Rollup merge of #75454 - ltratt:option_optimisation_guarantees, r=dtolnay
Explicitly document the size guarantees that Option makes. Triggered by a discussion on wg-unsafe-code-guidelines about which layouts of `Option<T>` one can guarantee are optimised to a single pointer. CC @RalfJung
This commit is contained in:
commit
1e62382a4f
@ -70,10 +70,23 @@
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! This usage of [`Option`] to create safe nullable pointers is so
|
||||
//! common that Rust does special optimizations to make the
|
||||
//! representation of [`Option`]`<`[`Box<T>`]`>` a single pointer. Optional pointers
|
||||
//! in Rust are stored as efficiently as any other pointer type.
|
||||
//! # Representation
|
||||
//!
|
||||
//! Rust guarantees to optimize the following types `T` such that
|
||||
//! [`Option<T>`] has the same size as `T`:
|
||||
//!
|
||||
//! * [`Box<U>`]
|
||||
//! * `&U`
|
||||
//! * `&mut U`
|
||||
//! * `fn`, `extern "C" fn`
|
||||
//! * [`num::NonZero*`]
|
||||
//! * [`ptr::NonNull<U>`]
|
||||
//! * `#[repr(transparent)]` struct around one of the types in this list.
|
||||
//!
|
||||
//! It is further guaranteed that, for the cases above, one can
|
||||
//! [`mem::transmute`] from all valid values of `T` to `Option<T>` and
|
||||
//! from `Some::<T>(_)` to `T` (but transmuting `None::<T>` to `T`
|
||||
//! is undefined behaviour).
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
|
Loading…
Reference in New Issue
Block a user