From 107cd8e2678a3e3c5058f37e16ff998bbeda36f5 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Tue, 15 Aug 2023 02:11:10 -0700 Subject: 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. --- library/core/src/ptr/non_null.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'library/core/src/ptr') diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 9582ca9e0be..8c8d4b55a4a 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -43,9 +43,27 @@ use crate::slice::{self, SliceIndex}; /// it is your responsibility to ensure that `as_mut` is never called, and `as_ptr` /// is never used for mutation. /// +/// # Representation +/// +/// Thanks to the [null pointer optimization], +/// `NonNull` and `Option>` +/// are guaranteed to have the same size and alignment: +/// +/// ``` +/// # use std::mem::{size_of, align_of}; +/// use std::ptr::NonNull; +/// +/// assert_eq!(size_of::>(), size_of::>>()); +/// assert_eq!(align_of::>(), align_of::>>()); +/// +/// assert_eq!(size_of::>(), size_of::>>()); +/// assert_eq!(align_of::>(), align_of::>>()); +/// ``` +/// /// [covariant]: https://doc.rust-lang.org/reference/subtyping.html /// [`PhantomData`]: crate::marker::PhantomData /// [`UnsafeCell`]: crate::cell::UnsafeCell +/// [null pointer optimization]: crate::option#representation #[stable(feature = "nonnull", since = "1.25.0")] #[repr(transparent)] #[rustc_layout_scalar_valid_range_start(1)] -- cgit 1.4.1-3-g733a5