Skip to content

Commit 107cd8e

Browse files
committed
Add alignment to the NPO guarantee
As far as I know, this is always true already, but it's not in the text of the Option module docs, so I figured I'd bring this up to FCP it.
1 parent a32978a commit 107cd8e

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

library/core/src/num/nonzero.rs

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ macro_rules! nonzero_integers {
4141
/// with the exception that `0` is not a valid instance.
4242
#[doc = concat!("`Option<", stringify!($Ty), ">` is guaranteed to be compatible with `", stringify!($Int), "`,")]
4343
/// including in FFI.
44+
///
45+
/// Thanks to the [null pointer optimization],
46+
#[doc = concat!("`", stringify!($Ty), "` and `Option<", stringify!($Ty), ">`")]
47+
/// are guaranteed to have the same size and alignment:
48+
///
49+
/// ```
50+
/// # use std::mem::{size_of, align_of};
51+
#[doc = concat!("use std::num::", stringify!($Ty), ";")]
52+
///
53+
#[doc = concat!("assert_eq!(size_of::<", stringify!($Ty), ">(), size_of::<Option<", stringify!($Ty), ">>());")]
54+
#[doc = concat!("assert_eq!(align_of::<", stringify!($Ty), ">(), align_of::<Option<", stringify!($Ty), ">>());")]
55+
/// ```
56+
///
57+
/// [null pointer optimization]: crate::option#representation
4458
#[$stability]
4559
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
4660
#[repr(transparent)]

library/core/src/option.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
//! # Representation
120120
//!
121121
//! Rust guarantees to optimize the following types `T` such that
122-
//! [`Option<T>`] has the same size as `T`:
122+
//! [`Option<T>`] has the same size and alignment as `T`:
123123
//!
124124
//! * [`Box<U>`]
125125
//! * `&U`

library/core/src/ptr/non_null.rs

+18
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,27 @@ use crate::slice::{self, SliceIndex};
4343
/// it is your responsibility to ensure that `as_mut` is never called, and `as_ptr`
4444
/// is never used for mutation.
4545
///
46+
/// # Representation
47+
///
48+
/// Thanks to the [null pointer optimization],
49+
/// `NonNull<T>` and `Option<NonNull<T>>`
50+
/// are guaranteed to have the same size and alignment:
51+
///
52+
/// ```
53+
/// # use std::mem::{size_of, align_of};
54+
/// use std::ptr::NonNull;
55+
///
56+
/// assert_eq!(size_of::<NonNull<i16>>(), size_of::<Option<NonNull<i16>>>());
57+
/// assert_eq!(align_of::<NonNull<i16>>(), align_of::<Option<NonNull<i16>>>());
58+
///
59+
/// assert_eq!(size_of::<NonNull<str>>(), size_of::<Option<NonNull<str>>>());
60+
/// assert_eq!(align_of::<NonNull<str>>(), align_of::<Option<NonNull<str>>>());
61+
/// ```
62+
///
4663
/// [covariant]: https://doc.rust-lang.org/reference/subtyping.html
4764
/// [`PhantomData`]: crate::marker::PhantomData
4865
/// [`UnsafeCell<T>`]: crate::cell::UnsafeCell
66+
/// [null pointer optimization]: crate::option#representation
4967
#[stable(feature = "nonnull", since = "1.25.0")]
5068
#[repr(transparent)]
5169
#[rustc_layout_scalar_valid_range_start(1)]

0 commit comments

Comments
 (0)