Skip to content

Commit

Permalink
Explicitly document the size guarantees that Option makes.
Browse files Browse the repository at this point in the history
Triggered by a discussion on wg-unsafe-code-guidelines about which layouts of
`Option<T>` one can guarantee are optimised to a single pointer.
  • Loading branch information
ltratt committed Aug 12, 2020
1 parent 5989bf4 commit 73ada2d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions library/core/src/option.rs
Expand Up @@ -70,10 +70,18 @@
//! }
//! ```
//!
//! 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 optimise the following inner types such that an [`Option`] which contains
//! 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
//!
Expand Down

0 comments on commit 73ada2d

Please sign in to comment.