about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-01-24 23:25:44 +0100
committerGitHub <noreply@github.com>2025-01-24 23:25:44 +0100
commit884ec6b4abc516c262a952ca2b5a509e0f6540f2 (patch)
tree978f85e3a4d381035bc41ba3eb1adb5f0d97c557
parentdcd43d3896ba6065563cbf87455d10fc9c07d4be (diff)
parent4a8de9ac41d6010ef3f4e930894be3628ccc3fc6 (diff)
downloadrust-884ec6b4abc516c262a952ca2b5a509e0f6540f2.tar.gz
rust-884ec6b4abc516c262a952ca2b5a509e0f6540f2.zip
Rollup merge of #135938 - carlsverre:master, r=joboet
Add memory layout documentation to generic NonZero<T>

The documentation I've added is based on the same Layout documentation that appears on the other `NonZero*` types. For example see [the Layout docs on `NonZeroI8`](https://doc.rust-lang.org/std/num/type.NonZeroI8.html#layout-1).
-rw-r--r--library/core/src/num/nonzero.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs
index dbce64420ac..8089d616409 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::<Option<NonZero<u32>>>(), size_of::<u32>());
 /// ```
+///
+/// # Layout
+///
+/// `NonZero<T>` is guaranteed to have the same layout and bit validity as `T`
+/// with the exception that the all-zero bit pattern is invalid.
+/// `Option<NonZero<T>>` is guaranteed to be compatible with `T`, including in
+/// FFI.
+///
+/// Thanks to the [null pointer optimization], `NonZero<T>` and
+/// `Option<NonZero<T>>` are guaranteed to have the same size and alignment:
+///
+/// ```
+/// # use std::mem::{size_of, align_of};
+/// use std::num::NonZero;
+///
+/// assert_eq!(size_of::<NonZero<u32>>(), size_of::<Option<NonZero<u32>>>());
+/// assert_eq!(align_of::<NonZero<u32>>(), align_of::<Option<NonZero<u32>>>());
+/// ```
+///
+/// [null pointer optimization]: crate::option#representation
 #[stable(feature = "generic_nonzero", since = "1.79.0")]
 #[repr(transparent)]
 #[rustc_nonnull_optimization_guaranteed]