File tree 1 file changed +22
-0
lines changed
1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -1110,6 +1110,28 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
1110
1110
/// to its fields.
1111
1111
///
1112
1112
/// [ub]: ../../reference/behavior-considered-undefined.html
1113
+ ///
1114
+ /// # Layout
1115
+ ///
1116
+ /// `MaybeUninit<T>` is guaranteed to have the same size and alignment as `T`:
1117
+ ///
1118
+ /// ```rust
1119
+ /// use std::mem::{MaybeUninit, size_of, align_of};
1120
+ /// assert_eq!(size_of::<MaybeUninit<u64>>(), size_of::<u64>());
1121
+ /// assert_eq!(align_of::<MaybeUninit<u64>>(), size_of::<u64>());
1122
+ /// ```
1123
+ ///
1124
+ /// However remember that a type *containing* a `MaybeUninit<T>` is not necessarily the same
1125
+ /// layout; Rust does not in general guarantee that the fields of a `Foo<T>` have the same order as
1126
+ /// a `Foo<U>` even if `T` and `U` have the same size and alignment. Furthermore because any bit
1127
+ /// value is valid for a `MaybeUninit<T>` the compiler can't apply non-zero/niche-filling
1128
+ /// optimizations, potentially resulting in a larger size:
1129
+ ///
1130
+ /// ```rust
1131
+ /// # use std::mem::{MaybeUninit, size_of, align_of};
1132
+ /// assert_eq!(size_of::<Option<bool>>(), 1);
1133
+ /// assert_eq!(size_of::<Option<MaybeUninit<bool>>>(), 2);
1134
+ /// ```
1113
1135
#[ allow( missing_debug_implementations) ]
1114
1136
#[ stable( feature = "maybe_uninit" , since = "1.36.0" ) ]
1115
1137
#[ derive( Copy ) ]
You can’t perform that action at this time.
0 commit comments