about summary refs log tree commit diff
path: root/library/core/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-01-24 23:13:48 +0000
committerbors <bors@rust-lang.org>2025-01-24 23:13:48 +0000
commit814ebca2931bd25384ade5018e1cbc403b13ec11 (patch)
tree5974a4a7cc9fa51594108a2b77fe2fb3b2a456a7 /library/core/src
parent1e9b0177da38e3f421a3b9b1942f1777d166e06a (diff)
parentd15cf36f0c2abdc1ebde9d00d8e0f3aef3f1e530 (diff)
Auto merge of #136030 - matthiaskrgr:rollup-cbue0ng, r=matthiaskrgr
Rollup of 9 pull requests

Successful merges:

 - #135415 (Add `File already exists` error doc to `hard_link` function)
 - #135581 (Separate Builder methods from tcx)
 - #135728 (document order of items in iterator from drain)
 - #135749 (Do not assume const params are printed after type params)
 - #135829 (Rustc dev guide subtree update)
 - #135938 (Add memory layout documentation to generic NonZero<T>)
 - #135949 (Use short type string in E0308 secondary span label)
 - #135976 (Don't drop types with no drop glue when building drops for tailcalls)
 - #135998 ([rustdoc] Fix indent of trait items on mobile)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/core/src')
-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]