Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 73ada2d

Browse files
committedAug 12, 2020
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.
1 parent 5989bf4 commit 73ada2d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed
 

‎library/core/src/option.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,18 @@
7070
//! }
7171
//! ```
7272
//!
73-
//! This usage of [`Option`] to create safe nullable pointers is so
74-
//! common that Rust does special optimizations to make the
75-
//! representation of [`Option`]`<`[`Box<T>`]`>` a single pointer. Optional pointers
76-
//! in Rust are stored as efficiently as any other pointer type.
73+
//! # Representation
74+
//!
75+
//! Rust guarantees to optimise the following inner types such that an [`Option`] which contains
76+
//! them has the same size as a pointer:
77+
//!
78+
//! * `&T`
79+
//! * `&mut T`
80+
//! * `extern "C" fn`
81+
//! * [`num::NonZero*`]
82+
//! * [`ptr::NonNull<T>`]
83+
//! * `#[repr(transparent)]` struct around one of the types in this list.
84+
//! * [`Box<T>`]
7785
//!
7886
//! # Examples
7987
//!

0 commit comments

Comments
 (0)
Please sign in to comment.