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 @@ -1053,6 +1053,28 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
1053
1053
/// to its fields.
1054
1054
///
1055
1055
/// [ub]: ../../reference/behavior-considered-undefined.html
1056
+ ///
1057
+ /// # Layout
1058
+ ///
1059
+ /// `MaybeUninit<T>` is guaranteed to have the same size and alignment as `T`:
1060
+ ///
1061
+ /// ```rust
1062
+ /// use std::mem::{MaybeUninit, size_of, align_of};
1063
+ /// assert_eq!(size_of::<MaybeUninit<u64>>(), size_of::<u64>());
1064
+ /// assert_eq!(align_of::<MaybeUninit<u64>>(), align_of::<u64>());
1065
+ /// ```
1066
+ ///
1067
+ /// However remember that a type *containing* a `MaybeUninit<T>` is not necessarily the same
1068
+ /// layout; Rust does not in general guarantee that the fields of a `Foo<T>` have the same order as
1069
+ /// a `Foo<U>` even if `T` and `U` have the same size and alignment. Furthermore because any bit
1070
+ /// value is valid for a `MaybeUninit<T>` the compiler can't apply non-zero/niche-filling
1071
+ /// optimizations, potentially resulting in a larger size:
1072
+ ///
1073
+ /// ```rust
1074
+ /// # use std::mem::{MaybeUninit, size_of, align_of};
1075
+ /// assert_eq!(size_of::<Option<bool>>(), 1);
1076
+ /// assert_eq!(size_of::<Option<MaybeUninit<bool>>>(), 2);
1077
+ /// ```
1056
1078
#[ allow( missing_debug_implementations) ]
1057
1079
#[ stable( feature = "maybe_uninit" , since = "1.36.0" ) ]
1058
1080
#[ derive( Copy ) ]
You can’t perform that action at this time.
0 commit comments