From f4166487ff37e6fce9e5558f4aea5d8b034bcdc8 Mon Sep 17 00:00:00 2001 From: Carl Sverre <82591+carlsverre@users.noreply.github.com> Date: Thu, 23 Jan 2025 08:51:21 -0800 Subject: [PATCH 1/2] Add memory layout documentation to generic NonZero --- library/core/src/num/nonzero.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index dbce64420ac45..bf7ba3222d0e2 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -90,6 +90,26 @@ impl_zeroable_primitive!( /// /// assert_eq!(size_of::>>(), size_of::()); /// ``` +/// +/// # Layout +/// +/// `NonZero` is guaranteed to have the same layout and bit validity as `T` +/// with the exception that the all-zero bit pattern is not a valid instance. +/// `Option>` is guaranteed to be compatible with `T`, including in +/// FFI. +/// +/// Thanks to the [null pointer optimization], `NonZero` and +/// `Option>` are guaranteed to have the same size and alignment: +/// +/// ``` +/// # use std::mem::{size_of, align_of}; +/// use std::num::NonZero; +/// +/// assert_eq!(size_of::>(), size_of::>>()); +/// assert_eq!(align_of::>(), align_of::>>()); +/// ``` +/// +/// [null pointer optimization]: crate::option#representation #[stable(feature = "generic_nonzero", since = "1.79.0")] #[repr(transparent)] #[rustc_nonnull_optimization_guaranteed] From 4a8de9ac41d6010ef3f4e930894be3628ccc3fc6 Mon Sep 17 00:00:00 2001 From: Carl Sverre <82591+carlsverre@users.noreply.github.com> Date: Fri, 24 Jan 2025 09:15:19 -0800 Subject: [PATCH 2/2] Update library/core/src/num/nonzero.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tweak language Co-authored-by: Jonas Böttiger --- library/core/src/num/nonzero.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index bf7ba3222d0e2..8089d6164090d 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -94,7 +94,7 @@ impl_zeroable_primitive!( /// # Layout /// /// `NonZero` is guaranteed to have the same layout and bit validity as `T` -/// with the exception that the all-zero bit pattern is not a valid instance. +/// with the exception that the all-zero bit pattern is invalid. /// `Option>` is guaranteed to be compatible with `T`, including in /// FFI. ///