Skip to content

Commit 3989fe9

Browse files
authored
Merge pull request #2393 from DanielEScherzer/MaybeUninit
Add documentation for `MaybeUninit<T>`
2 parents 2dafab9 + 7b2bb6d commit 3989fe9

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎src/behavior-considered-undefined.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ r[undefined.validity.struct]
144144
r[undefined.validity.union]
145145
* For a `union`, the exact validity requirements are not decided yet. Obviously, all values that can be created entirely in safe code are valid. If the union has a [zero-sized] field, then every possible value is valid. Further details are [still being debated](https://github.com/rust-lang/unsafe-code-guidelines/issues/438).
146146

147+
r[undefined.validity.maybe-uninit]
148+
* A [`MaybeUninit<T>`] is always valid (see [lang-types.maybe-uninit.validity]).
149+
147150
r[undefined.validity.reference-box]
148151
* A reference or [`Box<T>`] must be aligned and non-null, it cannot be [dangling], and it must point to a valid value (in case of dynamically sized types, using the actual dynamic type of the pointee as determined by the [metadata]). Note that the last point (about pointing to a valid value) remains a subject of some debate.
149152

@@ -203,6 +206,7 @@ r[undefined.validity.undef]
203206
[`target_feature`]: attributes/codegen.md#the-target_feature-attribute
204207
[`UnsafeCell<U>`]: std::cell::UnsafeCell
205208
[Rustonomicon]: ../nomicon/index.html
209+
[`MaybeUninit<T>`]: core::mem::MaybeUninit
206210
[metadata]: dynamic-sized.pointer-types
207211
[`NonNull<T>`]: core::ptr::NonNull
208212
[`NonZero<T>`]: core::num::NonZero

‎src/glossary.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ Examples of 1-ZSTs include:
124124
- `repr(transparent)` [structs] with no fields or where all fields are 1-ZSTs (see [layout.repr.transparent.layout-abi]).
125125
- [Arrays] of 1-ZSTs (see [layout.array]).
126126
- [`PhantomData<T>`] for any type `T` (see [lang-types.phantom-data]).
127+
- [`MaybeUninit<T>`] for a type `T` that is itself a 1-ZST (see [lang-types.maybe-uninit.layout]).
127128

128129
```rust
129130
# use core::marker::PhantomData;
130-
# use core::mem::{align_of, size_of};
131+
# use core::mem::{MaybeUninit, align_of, size_of};
131132
#[repr(C)]
132133
struct C1 {}
133134
#[repr(C)]
@@ -157,6 +158,7 @@ assert!(is_1_zst::<T1>());
157158
assert!(is_1_zst::<T2>());
158159
assert!(is_1_zst::<PhantomData<()>>());
159160
assert!(is_1_zst::<PhantomData<u16>>());
161+
assert!(is_1_zst::<MaybeUninit<[(); 2]>>());
160162
```
161163

162164
### Name
@@ -281,10 +283,11 @@ A type is zero sized (a ZST) if its size is 0. Such types have at most one possi
281283
- [Arrays] of length zero (see [layout.array]).
282284
- [Unions] of zero-sized types (see [items.union.common-storage]).
283285
- [`PhantomData<T>`] for any type `T` (see [lang-types.phantom-data]).
286+
- [`MaybeUninit<T>`] for a type `T` that is itself a zero-sized type (see [lang-types.maybe-uninit.layout]).
284287

285288
```rust
286289
# use core::marker::PhantomData;
287-
# use core::mem::{size_of, size_of_val};
290+
# use core::mem::{MaybeUninit, size_of, size_of_val};
288291
fn f() {}
289292
struct S(u8);
290293
enum E { V(u8) }
@@ -367,10 +370,12 @@ assert_eq!(0, size_of::<E7>());
367370
assert_eq!(0, size_of::<PhantomData<()>>());
368371
# // `PhantomData` for a type that is not a ZST.
369372
assert_eq!(0, size_of::<PhantomData<u16>>());
373+
assert_eq!(0, size_of::<MaybeUninit<[(); 2]>>());
370374
```
371375

372376
[`extern` blocks]: items.extern
373377
[`extern fn`]: items.fn.extern
378+
[`MaybeUninit<T>`]: lang-types.maybe-uninit
374379
[`PhantomData<T>`]: lang-types.phantom-data
375380
[alignment]: type-layout.md#size-and-alignment
376381
[arrays]: type.array

‎src/special-types-and-traits.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ r[lang-types.unsafe-cell.interior-mut]
4848
r[lang-types.unsafe-cell.read-only-alloc]
4949
It also ensures that [`static` items] which have a type with interior mutability aren't placed in memory marked as read only.
5050

51+
r[lang-types.maybe-uninit]
52+
## `MaybeUninit<T>`
53+
54+
r[lang-types.maybe-uninit.intro]
55+
[`MaybeUninit<T>`] is a type for values that are laid out like a `T` but need not be a valid `T`. This is useful when allocating and writing to memory intended for `T` values before that memory holds a valid `T`, as when initializing such values in place.
56+
57+
r[lang-types.maybe-uninit.validity]
58+
A value of type [`MaybeUninit<T>`] is always [valid], regardless of any validity requirements for a value of type `T`.
59+
60+
r[lang-types.maybe-uninit.layout]
61+
[`MaybeUninit<T>`] is guaranteed to have the same [size], [alignment], and [ABI] as `T`.
62+
5163
r[lang-types.phantom-data]
5264
## `PhantomData<T>`
5365

@@ -188,6 +200,7 @@ These implicit `Sized` bounds may be relaxed by using the special `?Sized` bound
188200
[`Arc<Self>`]: std::sync::Arc
189201
[`Deref`]: std::ops::Deref
190202
[`DerefMut`]: std::ops::DerefMut
203+
[`MaybeUninit<T>`]: core::mem::MaybeUninit
191204
[`Pin<P>`]: std::pin::Pin
192205
[`Rc<Self>`]: std::rc::Rc
193206
[`RefUnwindSafe`]: std::panic::RefUnwindSafe
@@ -196,6 +209,8 @@ These implicit `Sized` bounds may be relaxed by using the special `?Sized` bound
196209
[`Unpin`]: std::marker::Unpin
197210

198211
[1-aligned]: layout.properties.align
212+
[ABI]: glossary.abi
213+
[alignment]: layout.properties.align
199214
[Arrays]: types/array.md
200215
[associated types]: items/associated-items.md#associated-types
201216
[call expressions]: expressions/call-expr.md
@@ -216,12 +231,14 @@ These implicit `Sized` bounds may be relaxed by using the special `?Sized` bound
216231
[moved from]: expr.move.movable-place
217232
[operators]: expressions/operator-expr.md
218233
[orphan rules]: items/implementations.md#trait-implementation-coherence
234+
[size]: layout.properties.size
219235
[`static` items]: items/static-items.md
220236
[test functions]: attributes/testing.md#the-test-attribute
221237
[the standard library]: std
222238
[trait object]: types/trait-object.md
223239
[Tuples]: types/tuple.md
224240
[Type parameters]: types/parameters.md
241+
[valid]: undefined.validity
225242
[`VaList`]: core::ffi::VaList
226243
[variance]: subtyping.md#variance
227244
[zero-sized]: glossary.zst

0 commit comments

Comments
 (0)