Skip to content

Commit 31a896d

Browse files
carlsverregitbot
authored and
gitbot
committed
Add memory layout documentation to generic NonZero<T>
1 parent fa6e893 commit 31a896d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

core/src/num/nonzero.rs

+20
Original file line numberDiff line numberDiff 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]

0 commit comments

Comments
 (0)