File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -90,6 +90,26 @@ impl_zeroable_primitive!(
9090///
9191/// assert_eq!(size_of::<Option<NonZero<u32>>>(), size_of::<u32>());
9292/// ```
93+ ///
94+ /// # Layout
95+ ///
96+ /// `NonZero<T>` is guaranteed to have the same layout and bit validity as `T`
97+ /// with the exception that the all-zero bit pattern is not a valid instance.
98+ /// `Option<NonZero<T>>` is guaranteed to be compatible with `T`, including in
99+ /// FFI.
100+ ///
101+ /// Thanks to the [null pointer optimization], `NonZero<T>` and
102+ /// `Option<NonZero<T>>` are guaranteed to have the same size and alignment:
103+ ///
104+ /// ```
105+ /// # use std::mem::{size_of, align_of};
106+ /// use std::num::NonZero;
107+ ///
108+ /// assert_eq!(size_of::<NonZero<u32>>(), size_of::<Option<NonZero<u32>>>());
109+ /// assert_eq!(align_of::<NonZero<u32>>(), align_of::<Option<NonZero<u32>>>());
110+ /// ```
111+ ///
112+ /// [null pointer optimization]: crate::option#representation
93113#[ stable( feature = "generic_nonzero" , since = "1.79.0" ) ]
94114#[ repr( transparent) ]
95115#[ rustc_nonnull_optimization_guaranteed]
You can’t perform that action at this time.
0 commit comments