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.
This commit is contained in:
parent
5989bf4872
commit
73ada2d404
1 changed files with 12 additions and 4 deletions
|
@ -70,10 +70,18 @@
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! This usage of [`Option`] to create safe nullable pointers is so
|
//! # Representation
|
||||||
//! common that Rust does special optimizations to make the
|
//!
|
||||||
//! representation of [`Option`]`<`[`Box<T>`]`>` a single pointer. Optional pointers
|
//! Rust guarantees to optimise the following inner types such that an [`Option`] which contains
|
||||||
//! in Rust are stored as efficiently as any other pointer type.
|
//! them has the same size as a pointer:
|
||||||
|
//!
|
||||||
|
//! * `&T`
|
||||||
|
//! * `&mut T`
|
||||||
|
//! * `extern "C" fn`
|
||||||
|
//! * [`num::NonZero*`]
|
||||||
|
//! * [`ptr::NonNull<T>`]
|
||||||
|
//! * `#[repr(transparent)]` struct around one of the types in this list.
|
||||||
|
//! * [`Box<T>`]
|
||||||
//!
|
//!
|
||||||
//! # Examples
|
//! # Examples
|
||||||
//!
|
//!
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue