diff options
| author | Peter Todd <pete@petertodd.org> | 2019-05-06 17:39:39 -0400 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2019-05-20 10:44:02 +0200 |
| commit | 2a15dec5a2f7f713ffe176a9c1d766114f85aeef (patch) | |
| tree | 064a0bb925ce9dd31593d141c4dffbdb062fccd1 | |
| parent | 4bf500fea7b9c57d0fb20c9bdade3590b80358f0 (diff) | |
| download | rust-2a15dec5a2f7f713ffe176a9c1d766114f85aeef.tar.gz rust-2a15dec5a2f7f713ffe176a9c1d766114f85aeef.zip | |
Document layout guarantee of MaybeUninit
| -rw-r--r-- | src/libcore/mem.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index b7af9c0cef9..24bee6355a7 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1053,6 +1053,28 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> { /// to its fields. /// /// [ub]: ../../reference/behavior-considered-undefined.html +/// +/// # Layout +/// +/// `MaybeUninit<T>` is guaranteed to have the same size and alignment as `T`: +/// +/// ```rust +/// use std::mem::{MaybeUninit, size_of, align_of}; +/// assert_eq!(size_of::<MaybeUninit<u64>>(), size_of::<u64>()); +/// assert_eq!(align_of::<MaybeUninit<u64>>(), align_of::<u64>()); +/// ``` +/// +/// However remember that a type *containing* a `MaybeUninit<T>` is not necessarily the same +/// layout; Rust does not in general guarantee that the fields of a `Foo<T>` have the same order as +/// a `Foo<U>` even if `T` and `U` have the same size and alignment. Furthermore because any bit +/// value is valid for a `MaybeUninit<T>` the compiler can't apply non-zero/niche-filling +/// optimizations, potentially resulting in a larger size: +/// +/// ```rust +/// # use std::mem::{MaybeUninit, size_of, align_of}; +/// assert_eq!(size_of::<Option<bool>>(), 1); +/// assert_eq!(size_of::<Option<MaybeUninit<bool>>>(), 2); +/// ``` #[allow(missing_debug_implementations)] #[stable(feature = "maybe_uninit", since = "1.36.0")] #[derive(Copy)] |
