]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | use crate::any::type_name; |
2 | use crate::fmt; | |
dc9dc135 XL |
3 | use crate::intrinsics; |
4 | use crate::mem::ManuallyDrop; | |
1b1a35ee | 5 | use crate::ptr; |
60c5eb7d | 6 | |
dc9dc135 XL |
7 | /// A wrapper type to construct uninitialized instances of `T`. |
8 | /// | |
9 | /// # Initialization invariant | |
10 | /// | |
e74abb32 XL |
11 | /// The compiler, in general, assumes that a variable is properly initialized |
12 | /// according to the requirements of the variable's type. For example, a variable of | |
13 | /// reference type must be aligned and non-NULL. This is an invariant that must | |
14 | /// *always* be upheld, even in unsafe code. As a consequence, zero-initializing a | |
15 | /// variable of reference type causes instantaneous [undefined behavior][ub], | |
16 | /// no matter whether that reference ever gets used to access memory: | |
dc9dc135 XL |
17 | /// |
18 | /// ```rust,no_run | |
416331ca | 19 | /// # #![allow(invalid_value)] |
dc9dc135 XL |
20 | /// use std::mem::{self, MaybeUninit}; |
21 | /// | |
f9f354fc | 22 | /// let x: &i32 = unsafe { mem::zeroed() }; // undefined behavior! ⚠️ |
dc9dc135 | 23 | /// // The equivalent code with `MaybeUninit<&i32>`: |
f9f354fc | 24 | /// let x: &i32 = unsafe { MaybeUninit::zeroed().assume_init() }; // undefined behavior! ⚠️ |
dc9dc135 XL |
25 | /// ``` |
26 | /// | |
27 | /// This is exploited by the compiler for various optimizations, such as eliding | |
28 | /// run-time checks and optimizing `enum` layout. | |
29 | /// | |
30 | /// Similarly, entirely uninitialized memory may have any content, while a `bool` must | |
31 | /// always be `true` or `false`. Hence, creating an uninitialized `bool` is undefined behavior: | |
32 | /// | |
33 | /// ```rust,no_run | |
416331ca | 34 | /// # #![allow(invalid_value)] |
dc9dc135 XL |
35 | /// use std::mem::{self, MaybeUninit}; |
36 | /// | |
f9f354fc | 37 | /// let b: bool = unsafe { mem::uninitialized() }; // undefined behavior! ⚠️ |
dc9dc135 | 38 | /// // The equivalent code with `MaybeUninit<bool>`: |
f9f354fc | 39 | /// let b: bool = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior! ⚠️ |
dc9dc135 XL |
40 | /// ``` |
41 | /// | |
42 | /// Moreover, uninitialized memory is special in that the compiler knows that | |
43 | /// it does not have a fixed value. This makes it undefined behavior to have | |
44 | /// uninitialized data in a variable even if that variable has an integer type, | |
45 | /// which otherwise can hold any *fixed* bit pattern: | |
46 | /// | |
47 | /// ```rust,no_run | |
416331ca | 48 | /// # #![allow(invalid_value)] |
dc9dc135 XL |
49 | /// use std::mem::{self, MaybeUninit}; |
50 | /// | |
f9f354fc | 51 | /// let x: i32 = unsafe { mem::uninitialized() }; // undefined behavior! ⚠️ |
dc9dc135 | 52 | /// // The equivalent code with `MaybeUninit<i32>`: |
f9f354fc | 53 | /// let x: i32 = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior! ⚠️ |
dc9dc135 XL |
54 | /// ``` |
55 | /// (Notice that the rules around uninitialized integers are not finalized yet, but | |
56 | /// until they are, it is advisable to avoid them.) | |
57 | /// | |
58 | /// On top of that, remember that most types have additional invariants beyond merely | |
59 | /// being considered initialized at the type level. For example, a `1`-initialized [`Vec<T>`] | |
416331ca XL |
60 | /// is considered initialized (under the current implementation; this does not constitute |
61 | /// a stable guarantee) because the only requirement the compiler knows about it | |
dc9dc135 XL |
62 | /// is that the data pointer must be non-null. Creating such a `Vec<T>` does not cause |
63 | /// *immediate* undefined behavior, but will cause undefined behavior with most | |
64 | /// safe operations (including dropping it). | |
65 | /// | |
66 | /// [`Vec<T>`]: ../../std/vec/struct.Vec.html | |
67 | /// | |
68 | /// # Examples | |
69 | /// | |
70 | /// `MaybeUninit<T>` serves to enable unsafe code to deal with uninitialized data. | |
71 | /// It is a signal to the compiler indicating that the data here might *not* | |
72 | /// be initialized: | |
73 | /// | |
74 | /// ```rust | |
75 | /// use std::mem::MaybeUninit; | |
76 | /// | |
77 | /// // Create an explicitly uninitialized reference. The compiler knows that data inside | |
78 | /// // a `MaybeUninit<T>` may be invalid, and hence this is not UB: | |
79 | /// let mut x = MaybeUninit::<&i32>::uninit(); | |
80 | /// // Set it to a valid value. | |
81 | /// unsafe { x.as_mut_ptr().write(&0); } | |
82 | /// // Extract the initialized data -- this is only allowed *after* properly | |
83 | /// // initializing `x`! | |
84 | /// let x = unsafe { x.assume_init() }; | |
85 | /// ``` | |
86 | /// | |
87 | /// The compiler then knows to not make any incorrect assumptions or optimizations on this code. | |
88 | /// | |
89 | /// You can think of `MaybeUninit<T>` as being a bit like `Option<T>` but without | |
90 | /// any of the run-time tracking and without any of the safety checks. | |
91 | /// | |
92 | /// ## out-pointers | |
93 | /// | |
94 | /// You can use `MaybeUninit<T>` to implement "out-pointers": instead of returning data | |
95 | /// from a function, pass it a pointer to some (uninitialized) memory to put the | |
96 | /// result into. This can be useful when it is important for the caller to control | |
97 | /// how the memory the result is stored in gets allocated, and you want to avoid | |
98 | /// unnecessary moves. | |
99 | /// | |
100 | /// ``` | |
101 | /// use std::mem::MaybeUninit; | |
102 | /// | |
103 | /// unsafe fn make_vec(out: *mut Vec<i32>) { | |
104 | /// // `write` does not drop the old contents, which is important. | |
105 | /// out.write(vec![1, 2, 3]); | |
106 | /// } | |
107 | /// | |
108 | /// let mut v = MaybeUninit::uninit(); | |
109 | /// unsafe { make_vec(v.as_mut_ptr()); } | |
110 | /// // Now we know `v` is initialized! This also makes sure the vector gets | |
111 | /// // properly dropped. | |
112 | /// let v = unsafe { v.assume_init() }; | |
113 | /// assert_eq!(&v, &[1, 2, 3]); | |
114 | /// ``` | |
115 | /// | |
116 | /// ## Initializing an array element-by-element | |
117 | /// | |
118 | /// `MaybeUninit<T>` can be used to initialize a large array element-by-element: | |
119 | /// | |
120 | /// ``` | |
121 | /// use std::mem::{self, MaybeUninit}; | |
dc9dc135 XL |
122 | /// |
123 | /// let data = { | |
124 | /// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is | |
125 | /// // safe because the type we are claiming to have initialized here is a | |
126 | /// // bunch of `MaybeUninit`s, which do not require initialization. | |
127 | /// let mut data: [MaybeUninit<Vec<u32>>; 1000] = unsafe { | |
128 | /// MaybeUninit::uninit().assume_init() | |
129 | /// }; | |
130 | /// | |
416331ca XL |
131 | /// // Dropping a `MaybeUninit` does nothing. Thus using raw pointer |
132 | /// // assignment instead of `ptr::write` does not cause the old | |
133 | /// // uninitialized value to be dropped. Also if there is a panic during | |
134 | /// // this loop, we have a memory leak, but there is no memory safety | |
135 | /// // issue. | |
dc9dc135 | 136 | /// for elem in &mut data[..] { |
416331ca | 137 | /// *elem = MaybeUninit::new(vec![42]); |
dc9dc135 XL |
138 | /// } |
139 | /// | |
140 | /// // Everything is initialized. Transmute the array to the | |
141 | /// // initialized type. | |
142 | /// unsafe { mem::transmute::<_, [Vec<u32>; 1000]>(data) } | |
143 | /// }; | |
144 | /// | |
145 | /// assert_eq!(&data[0], &[42]); | |
146 | /// ``` | |
147 | /// | |
148 | /// You can also work with partially initialized arrays, which could | |
149 | /// be found in low-level datastructures. | |
150 | /// | |
151 | /// ``` | |
152 | /// use std::mem::MaybeUninit; | |
153 | /// use std::ptr; | |
154 | /// | |
155 | /// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is | |
156 | /// // safe because the type we are claiming to have initialized here is a | |
157 | /// // bunch of `MaybeUninit`s, which do not require initialization. | |
158 | /// let mut data: [MaybeUninit<String>; 1000] = unsafe { MaybeUninit::uninit().assume_init() }; | |
159 | /// // Count the number of elements we have assigned. | |
160 | /// let mut data_len: usize = 0; | |
161 | /// | |
162 | /// for elem in &mut data[0..500] { | |
416331ca | 163 | /// *elem = MaybeUninit::new(String::from("hello")); |
dc9dc135 XL |
164 | /// data_len += 1; |
165 | /// } | |
166 | /// | |
167 | /// // For each item in the array, drop if we allocated it. | |
168 | /// for elem in &mut data[0..data_len] { | |
169 | /// unsafe { ptr::drop_in_place(elem.as_mut_ptr()); } | |
170 | /// } | |
171 | /// ``` | |
172 | /// | |
173 | /// ## Initializing a struct field-by-field | |
174 | /// | |
175 | /// There is currently no supported way to create a raw pointer or reference | |
176 | /// to a field of a struct inside `MaybeUninit<Struct>`. That means it is not possible | |
177 | /// to create a struct by calling `MaybeUninit::uninit::<Struct>()` and then writing | |
178 | /// to its fields. | |
179 | /// | |
180 | /// [ub]: ../../reference/behavior-considered-undefined.html | |
181 | /// | |
182 | /// # Layout | |
183 | /// | |
184 | /// `MaybeUninit<T>` is guaranteed to have the same size, alignment, and ABI as `T`: | |
185 | /// | |
186 | /// ```rust | |
187 | /// use std::mem::{MaybeUninit, size_of, align_of}; | |
188 | /// assert_eq!(size_of::<MaybeUninit<u64>>(), size_of::<u64>()); | |
189 | /// assert_eq!(align_of::<MaybeUninit<u64>>(), align_of::<u64>()); | |
190 | /// ``` | |
191 | /// | |
192 | /// However remember that a type *containing* a `MaybeUninit<T>` is not necessarily the same | |
193 | /// layout; Rust does not in general guarantee that the fields of a `Foo<T>` have the same order as | |
194 | /// a `Foo<U>` even if `T` and `U` have the same size and alignment. Furthermore because any bit | |
195 | /// value is valid for a `MaybeUninit<T>` the compiler can't apply non-zero/niche-filling | |
196 | /// optimizations, potentially resulting in a larger size: | |
197 | /// | |
198 | /// ```rust | |
199 | /// # use std::mem::{MaybeUninit, size_of}; | |
200 | /// assert_eq!(size_of::<Option<bool>>(), 1); | |
201 | /// assert_eq!(size_of::<Option<MaybeUninit<bool>>>(), 2); | |
202 | /// ``` | |
203 | /// | |
204 | /// If `T` is FFI-safe, then so is `MaybeUninit<T>`. | |
205 | /// | |
206 | /// While `MaybeUninit` is `#[repr(transparent)]` (indicating it guarantees the same size, | |
207 | /// alignment, and ABI as `T`), this does *not* change any of the previous caveats. `Option<T>` and | |
208 | /// `Option<MaybeUninit<T>>` may still have different sizes, and types containing a field of type | |
209 | /// `T` may be laid out (and sized) differently than if that field were `MaybeUninit<T>`. | |
210 | /// `MaybeUninit` is a union type, and `#[repr(transparent)]` on unions is unstable (see [the | |
211 | /// tracking issue](https://github.com/rust-lang/rust/issues/60405)). Over time, the exact | |
212 | /// guarantees of `#[repr(transparent)]` on unions may evolve, and `MaybeUninit` may or may not | |
213 | /// remain `#[repr(transparent)]`. That said, `MaybeUninit<T>` will *always* guarantee that it has | |
214 | /// the same size, alignment, and ABI as `T`; it's just that the way `MaybeUninit` implements that | |
215 | /// guarantee may evolve. | |
dc9dc135 | 216 | #[stable(feature = "maybe_uninit", since = "1.36.0")] |
416331ca | 217 | // Lang item so we can wrap other types in it. This is useful for generators. |
e1599b0c | 218 | #[lang = "maybe_uninit"] |
dc9dc135 | 219 | #[derive(Copy)] |
416331ca | 220 | #[repr(transparent)] |
dc9dc135 XL |
221 | pub union MaybeUninit<T> { |
222 | uninit: (), | |
223 | value: ManuallyDrop<T>, | |
224 | } | |
225 | ||
226 | #[stable(feature = "maybe_uninit", since = "1.36.0")] | |
227 | impl<T: Copy> Clone for MaybeUninit<T> { | |
228 | #[inline(always)] | |
229 | fn clone(&self) -> Self { | |
230 | // Not calling `T::clone()`, we cannot know if we are initialized enough for that. | |
231 | *self | |
232 | } | |
233 | } | |
234 | ||
60c5eb7d XL |
235 | #[stable(feature = "maybe_uninit_debug", since = "1.41.0")] |
236 | impl<T> fmt::Debug for MaybeUninit<T> { | |
237 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
238 | f.pad(type_name::<Self>()) | |
239 | } | |
240 | } | |
241 | ||
dc9dc135 XL |
242 | impl<T> MaybeUninit<T> { |
243 | /// Creates a new `MaybeUninit<T>` initialized with the given value. | |
244 | /// It is safe to call [`assume_init`] on the return value of this function. | |
245 | /// | |
246 | /// Note that dropping a `MaybeUninit<T>` will never call `T`'s drop code. | |
247 | /// It is your responsibility to make sure `T` gets dropped if it got initialized. | |
248 | /// | |
3dfed10e | 249 | /// [`assume_init`]: MaybeUninit::assume_init |
dc9dc135 | 250 | #[stable(feature = "maybe_uninit", since = "1.36.0")] |
dfeec247 | 251 | #[rustc_const_stable(feature = "const_maybe_uninit", since = "1.36.0")] |
dc9dc135 XL |
252 | #[inline(always)] |
253 | pub const fn new(val: T) -> MaybeUninit<T> { | |
254 | MaybeUninit { value: ManuallyDrop::new(val) } | |
255 | } | |
256 | ||
257 | /// Creates a new `MaybeUninit<T>` in an uninitialized state. | |
258 | /// | |
259 | /// Note that dropping a `MaybeUninit<T>` will never call `T`'s drop code. | |
260 | /// It is your responsibility to make sure `T` gets dropped if it got initialized. | |
261 | /// | |
262 | /// See the [type-level documentation][type] for some examples. | |
263 | /// | |
264 | /// [type]: union.MaybeUninit.html | |
265 | #[stable(feature = "maybe_uninit", since = "1.36.0")] | |
dfeec247 | 266 | #[rustc_const_stable(feature = "const_maybe_uninit", since = "1.36.0")] |
dc9dc135 | 267 | #[inline(always)] |
dfeec247 | 268 | #[rustc_diagnostic_item = "maybe_uninit_uninit"] |
dc9dc135 XL |
269 | pub const fn uninit() -> MaybeUninit<T> { |
270 | MaybeUninit { uninit: () } | |
271 | } | |
272 | ||
60c5eb7d XL |
273 | /// Create a new array of `MaybeUninit<T>` items, in an uninitialized state. |
274 | /// | |
275 | /// Note: in a future Rust version this method may become unnecessary | |
276 | /// when array literal syntax allows | |
277 | /// [repeating const expressions](https://github.com/rust-lang/rust/issues/49147). | |
278 | /// The example below could then use `let mut buf = [MaybeUninit::<u8>::uninit(); 32];`. | |
279 | /// | |
280 | /// # Examples | |
281 | /// | |
282 | /// ```no_run | |
1b1a35ee | 283 | /// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice)] |
60c5eb7d XL |
284 | /// |
285 | /// use std::mem::MaybeUninit; | |
286 | /// | |
287 | /// extern "C" { | |
288 | /// fn read_into_buffer(ptr: *mut u8, max_len: usize) -> usize; | |
289 | /// } | |
290 | /// | |
291 | /// /// Returns a (possibly smaller) slice of data that was actually read | |
292 | /// fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] { | |
293 | /// unsafe { | |
294 | /// let len = read_into_buffer(buf.as_mut_ptr() as *mut u8, buf.len()); | |
1b1a35ee | 295 | /// MaybeUninit::slice_assume_init_ref(&buf[..len]) |
60c5eb7d XL |
296 | /// } |
297 | /// } | |
298 | /// | |
299 | /// let mut buf: [MaybeUninit<u8>; 32] = MaybeUninit::uninit_array(); | |
300 | /// let data = read(&mut buf); | |
301 | /// ``` | |
dfeec247 | 302 | #[unstable(feature = "maybe_uninit_uninit_array", issue = "none")] |
60c5eb7d XL |
303 | #[inline(always)] |
304 | pub fn uninit_array<const LEN: usize>() -> [Self; LEN] { | |
1b1a35ee | 305 | // SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid. |
dfeec247 | 306 | unsafe { MaybeUninit::<[MaybeUninit<T>; LEN]>::uninit().assume_init() } |
60c5eb7d XL |
307 | } |
308 | ||
dc9dc135 XL |
309 | /// Creates a new `MaybeUninit<T>` in an uninitialized state, with the memory being |
310 | /// filled with `0` bytes. It depends on `T` whether that already makes for | |
311 | /// proper initialization. For example, `MaybeUninit<usize>::zeroed()` is initialized, | |
312 | /// but `MaybeUninit<&'static i32>::zeroed()` is not because references must not | |
313 | /// be null. | |
314 | /// | |
315 | /// Note that dropping a `MaybeUninit<T>` will never call `T`'s drop code. | |
316 | /// It is your responsibility to make sure `T` gets dropped if it got initialized. | |
317 | /// | |
318 | /// # Example | |
319 | /// | |
320 | /// Correct usage of this function: initializing a struct with zero, where all | |
321 | /// fields of the struct can hold the bit-pattern 0 as a valid value. | |
322 | /// | |
323 | /// ```rust | |
324 | /// use std::mem::MaybeUninit; | |
325 | /// | |
326 | /// let x = MaybeUninit::<(u8, bool)>::zeroed(); | |
327 | /// let x = unsafe { x.assume_init() }; | |
328 | /// assert_eq!(x, (0, false)); | |
329 | /// ``` | |
330 | /// | |
3dfed10e XL |
331 | /// *Incorrect* usage of this function: calling `x.zeroed().assume_init()` |
332 | /// when `0` is not a valid bit-pattern for the type: | |
dc9dc135 XL |
333 | /// |
334 | /// ```rust,no_run | |
335 | /// use std::mem::MaybeUninit; | |
336 | /// | |
337 | /// enum NotZero { One = 1, Two = 2 }; | |
338 | /// | |
339 | /// let x = MaybeUninit::<(u8, NotZero)>::zeroed(); | |
340 | /// let x = unsafe { x.assume_init() }; | |
341 | /// // Inside a pair, we create a `NotZero` that does not have a valid discriminant. | |
f9f354fc | 342 | /// // This is undefined behavior. ⚠️ |
dc9dc135 XL |
343 | /// ``` |
344 | #[stable(feature = "maybe_uninit", since = "1.36.0")] | |
345 | #[inline] | |
dfeec247 | 346 | #[rustc_diagnostic_item = "maybe_uninit_zeroed"] |
dc9dc135 XL |
347 | pub fn zeroed() -> MaybeUninit<T> { |
348 | let mut u = MaybeUninit::<T>::uninit(); | |
1b1a35ee | 349 | // SAFETY: `u.as_mut_ptr()` points to allocated memory. |
dc9dc135 XL |
350 | unsafe { |
351 | u.as_mut_ptr().write_bytes(0u8, 1); | |
352 | } | |
353 | u | |
354 | } | |
355 | ||
356 | /// Sets the value of the `MaybeUninit<T>`. This overwrites any previous value | |
357 | /// without dropping it, so be careful not to use this twice unless you want to | |
358 | /// skip running the destructor. For your convenience, this also returns a mutable | |
359 | /// reference to the (now safely initialized) contents of `self`. | |
e1599b0c | 360 | #[unstable(feature = "maybe_uninit_extra", issue = "63567")] |
dc9dc135 XL |
361 | #[inline(always)] |
362 | pub fn write(&mut self, val: T) -> &mut T { | |
1b1a35ee XL |
363 | *self = MaybeUninit::new(val); |
364 | // SAFETY: We just initialized this value. | |
365 | unsafe { self.assume_init_mut() } | |
dc9dc135 XL |
366 | } |
367 | ||
368 | /// Gets a pointer to the contained value. Reading from this pointer or turning it | |
369 | /// into a reference is undefined behavior unless the `MaybeUninit<T>` is initialized. | |
370 | /// Writing to memory that this pointer (non-transitively) points to is undefined behavior | |
371 | /// (except inside an `UnsafeCell<T>`). | |
372 | /// | |
373 | /// # Examples | |
374 | /// | |
375 | /// Correct usage of this method: | |
376 | /// | |
377 | /// ```rust | |
378 | /// use std::mem::MaybeUninit; | |
379 | /// | |
380 | /// let mut x = MaybeUninit::<Vec<u32>>::uninit(); | |
381 | /// unsafe { x.as_mut_ptr().write(vec![0,1,2]); } | |
382 | /// // Create a reference into the `MaybeUninit<T>`. This is okay because we initialized it. | |
383 | /// let x_vec = unsafe { &*x.as_ptr() }; | |
384 | /// assert_eq!(x_vec.len(), 3); | |
385 | /// ``` | |
386 | /// | |
387 | /// *Incorrect* usage of this method: | |
388 | /// | |
389 | /// ```rust,no_run | |
390 | /// use std::mem::MaybeUninit; | |
391 | /// | |
392 | /// let x = MaybeUninit::<Vec<u32>>::uninit(); | |
393 | /// let x_vec = unsafe { &*x.as_ptr() }; | |
f9f354fc | 394 | /// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️ |
dc9dc135 XL |
395 | /// ``` |
396 | /// | |
397 | /// (Notice that the rules around references to uninitialized data are not finalized yet, but | |
398 | /// until they are, it is advisable to avoid them.) | |
399 | #[stable(feature = "maybe_uninit", since = "1.36.0")] | |
3dfed10e | 400 | #[rustc_const_unstable(feature = "const_maybe_uninit_as_ptr", issue = "75251")] |
dc9dc135 | 401 | #[inline(always)] |
3dfed10e XL |
402 | pub const fn as_ptr(&self) -> *const T { |
403 | // `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer. | |
404 | self as *const _ as *const T | |
dc9dc135 XL |
405 | } |
406 | ||
407 | /// Gets a mutable pointer to the contained value. Reading from this pointer or turning it | |
408 | /// into a reference is undefined behavior unless the `MaybeUninit<T>` is initialized. | |
409 | /// | |
410 | /// # Examples | |
411 | /// | |
412 | /// Correct usage of this method: | |
413 | /// | |
414 | /// ```rust | |
415 | /// use std::mem::MaybeUninit; | |
416 | /// | |
417 | /// let mut x = MaybeUninit::<Vec<u32>>::uninit(); | |
418 | /// unsafe { x.as_mut_ptr().write(vec![0,1,2]); } | |
419 | /// // Create a reference into the `MaybeUninit<Vec<u32>>`. | |
420 | /// // This is okay because we initialized it. | |
421 | /// let x_vec = unsafe { &mut *x.as_mut_ptr() }; | |
422 | /// x_vec.push(3); | |
423 | /// assert_eq!(x_vec.len(), 4); | |
424 | /// ``` | |
425 | /// | |
426 | /// *Incorrect* usage of this method: | |
427 | /// | |
428 | /// ```rust,no_run | |
429 | /// use std::mem::MaybeUninit; | |
430 | /// | |
431 | /// let mut x = MaybeUninit::<Vec<u32>>::uninit(); | |
432 | /// let x_vec = unsafe { &mut *x.as_mut_ptr() }; | |
f9f354fc | 433 | /// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️ |
dc9dc135 XL |
434 | /// ``` |
435 | /// | |
436 | /// (Notice that the rules around references to uninitialized data are not finalized yet, but | |
437 | /// until they are, it is advisable to avoid them.) | |
438 | #[stable(feature = "maybe_uninit", since = "1.36.0")] | |
3dfed10e | 439 | #[rustc_const_unstable(feature = "const_maybe_uninit_as_ptr", issue = "75251")] |
dc9dc135 | 440 | #[inline(always)] |
3dfed10e XL |
441 | pub const fn as_mut_ptr(&mut self) -> *mut T { |
442 | // `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer. | |
443 | self as *mut _ as *mut T | |
dc9dc135 XL |
444 | } |
445 | ||
446 | /// Extracts the value from the `MaybeUninit<T>` container. This is a great way | |
447 | /// to ensure that the data will get dropped, because the resulting `T` is | |
448 | /// subject to the usual drop handling. | |
449 | /// | |
450 | /// # Safety | |
451 | /// | |
452 | /// It is up to the caller to guarantee that the `MaybeUninit<T>` really is in an initialized | |
453 | /// state. Calling this when the content is not yet fully initialized causes immediate undefined | |
454 | /// behavior. The [type-level documentation][inv] contains more information about | |
455 | /// this initialization invariant. | |
456 | /// | |
457 | /// [inv]: #initialization-invariant | |
458 | /// | |
416331ca XL |
459 | /// On top of that, remember that most types have additional invariants beyond merely |
460 | /// being considered initialized at the type level. For example, a `1`-initialized [`Vec<T>`] | |
461 | /// is considered initialized (under the current implementation; this does not constitute | |
462 | /// a stable guarantee) because the only requirement the compiler knows about it | |
463 | /// is that the data pointer must be non-null. Creating such a `Vec<T>` does not cause | |
464 | /// *immediate* undefined behavior, but will cause undefined behavior with most | |
465 | /// safe operations (including dropping it). | |
466 | /// | |
1b1a35ee XL |
467 | /// [`Vec<T>`]: ../../std/vec/struct.Vec.html |
468 | /// | |
dc9dc135 XL |
469 | /// # Examples |
470 | /// | |
471 | /// Correct usage of this method: | |
472 | /// | |
473 | /// ```rust | |
474 | /// use std::mem::MaybeUninit; | |
475 | /// | |
476 | /// let mut x = MaybeUninit::<bool>::uninit(); | |
477 | /// unsafe { x.as_mut_ptr().write(true); } | |
478 | /// let x_init = unsafe { x.assume_init() }; | |
479 | /// assert_eq!(x_init, true); | |
480 | /// ``` | |
481 | /// | |
482 | /// *Incorrect* usage of this method: | |
483 | /// | |
484 | /// ```rust,no_run | |
485 | /// use std::mem::MaybeUninit; | |
486 | /// | |
487 | /// let x = MaybeUninit::<Vec<u32>>::uninit(); | |
488 | /// let x_init = unsafe { x.assume_init() }; | |
f9f354fc | 489 | /// // `x` had not been initialized yet, so this last line caused undefined behavior. ⚠️ |
dc9dc135 XL |
490 | /// ``` |
491 | #[stable(feature = "maybe_uninit", since = "1.36.0")] | |
492 | #[inline(always)] | |
dfeec247 | 493 | #[rustc_diagnostic_item = "assume_init"] |
dc9dc135 | 494 | pub unsafe fn assume_init(self) -> T { |
f035d41b XL |
495 | // SAFETY: the caller must guarantee that `self` is initialized. |
496 | // This also means that `self` must be a `value` variant. | |
497 | unsafe { | |
498 | intrinsics::assert_inhabited::<T>(); | |
499 | ManuallyDrop::into_inner(self.value) | |
500 | } | |
dc9dc135 XL |
501 | } |
502 | ||
503 | /// Reads the value from the `MaybeUninit<T>` container. The resulting `T` is subject | |
504 | /// to the usual drop handling. | |
505 | /// | |
416331ca | 506 | /// Whenever possible, it is preferable to use [`assume_init`] instead, which |
dc9dc135 XL |
507 | /// prevents duplicating the content of the `MaybeUninit<T>`. |
508 | /// | |
509 | /// # Safety | |
510 | /// | |
511 | /// It is up to the caller to guarantee that the `MaybeUninit<T>` really is in an initialized | |
512 | /// state. Calling this when the content is not yet fully initialized causes undefined | |
513 | /// behavior. The [type-level documentation][inv] contains more information about | |
514 | /// this initialization invariant. | |
515 | /// | |
516 | /// Moreover, this leaves a copy of the same data behind in the `MaybeUninit<T>`. When using | |
1b1a35ee XL |
517 | /// multiple copies of the data (by calling `assume_init_read` multiple times, or first |
518 | /// calling `assume_init_read` and then [`assume_init`]), it is your responsibility | |
dc9dc135 XL |
519 | /// to ensure that that data may indeed be duplicated. |
520 | /// | |
521 | /// [inv]: #initialization-invariant | |
3dfed10e | 522 | /// [`assume_init`]: MaybeUninit::assume_init |
dc9dc135 XL |
523 | /// |
524 | /// # Examples | |
525 | /// | |
526 | /// Correct usage of this method: | |
527 | /// | |
528 | /// ```rust | |
529 | /// #![feature(maybe_uninit_extra)] | |
530 | /// use std::mem::MaybeUninit; | |
531 | /// | |
532 | /// let mut x = MaybeUninit::<u32>::uninit(); | |
533 | /// x.write(13); | |
1b1a35ee | 534 | /// let x1 = unsafe { x.assume_init_read() }; |
dc9dc135 | 535 | /// // `u32` is `Copy`, so we may read multiple times. |
1b1a35ee | 536 | /// let x2 = unsafe { x.assume_init_read() }; |
dc9dc135 XL |
537 | /// assert_eq!(x1, x2); |
538 | /// | |
539 | /// let mut x = MaybeUninit::<Option<Vec<u32>>>::uninit(); | |
540 | /// x.write(None); | |
1b1a35ee | 541 | /// let x1 = unsafe { x.assume_init_read() }; |
dc9dc135 | 542 | /// // Duplicating a `None` value is okay, so we may read multiple times. |
1b1a35ee | 543 | /// let x2 = unsafe { x.assume_init_read() }; |
dc9dc135 XL |
544 | /// assert_eq!(x1, x2); |
545 | /// ``` | |
546 | /// | |
547 | /// *Incorrect* usage of this method: | |
548 | /// | |
549 | /// ```rust,no_run | |
550 | /// #![feature(maybe_uninit_extra)] | |
551 | /// use std::mem::MaybeUninit; | |
552 | /// | |
553 | /// let mut x = MaybeUninit::<Option<Vec<u32>>>::uninit(); | |
554 | /// x.write(Some(vec![0,1,2])); | |
1b1a35ee XL |
555 | /// let x1 = unsafe { x.assume_init_read() }; |
556 | /// let x2 = unsafe { x.assume_init_read() }; | |
f9f354fc | 557 | /// // We now created two copies of the same vector, leading to a double-free ⚠️ when |
dc9dc135 XL |
558 | /// // they both get dropped! |
559 | /// ``` | |
e1599b0c | 560 | #[unstable(feature = "maybe_uninit_extra", issue = "63567")] |
dc9dc135 | 561 | #[inline(always)] |
1b1a35ee | 562 | pub unsafe fn assume_init_read(&self) -> T { |
f035d41b XL |
563 | // SAFETY: the caller must guarantee that `self` is initialized. |
564 | // Reading from `self.as_ptr()` is safe since `self` should be initialized. | |
565 | unsafe { | |
566 | intrinsics::assert_inhabited::<T>(); | |
567 | self.as_ptr().read() | |
568 | } | |
dc9dc135 XL |
569 | } |
570 | ||
1b1a35ee XL |
571 | /// Drops the contained value in place. |
572 | /// | |
573 | /// If you have ownership of the `MaybeUninit`, you can use [`assume_init`] instead. | |
574 | /// | |
575 | /// # Safety | |
576 | /// | |
577 | /// It is up to the caller to guarantee that the `MaybeUninit<T>` really is | |
578 | /// in an initialized state. Calling this when the content is not yet fully | |
579 | /// initialized causes undefined behavior. | |
580 | /// | |
581 | /// On top of that, all additional invariants of the type `T` must be | |
582 | /// satisfied, as the `Drop` implementation of `T` (or its members) may | |
583 | /// rely on this. For example, a `1`-initialized [`Vec<T>`] is considered | |
584 | /// initialized (under the current implementation; this does not constitute | |
585 | /// a stable guarantee) because the only requirement the compiler knows | |
586 | /// about it is that the data pointer must be non-null. Dropping such a | |
587 | /// `Vec<T>` however will cause undefined behaviour. | |
588 | /// | |
589 | /// [`assume_init`]: MaybeUninit::assume_init | |
590 | /// [`Vec<T>`]: ../../std/vec/struct.Vec.html | |
591 | #[unstable(feature = "maybe_uninit_extra", issue = "63567")] | |
592 | pub unsafe fn assume_init_drop(&mut self) { | |
593 | // SAFETY: the caller must guarantee that `self` is initialized and | |
594 | // satisfies all invariants of `T`. | |
595 | // Dropping the value in place is safe if that is the case. | |
596 | unsafe { ptr::drop_in_place(self.as_mut_ptr()) } | |
597 | } | |
598 | ||
60c5eb7d XL |
599 | /// Gets a shared reference to the contained value. |
600 | /// | |
601 | /// This can be useful when we want to access a `MaybeUninit` that has been | |
602 | /// initialized but don't have ownership of the `MaybeUninit` (preventing the use | |
603 | /// of `.assume_init()`). | |
dc9dc135 XL |
604 | /// |
605 | /// # Safety | |
606 | /// | |
60c5eb7d XL |
607 | /// Calling this when the content is not yet fully initialized causes undefined |
608 | /// behavior: it is up to the caller to guarantee that the `MaybeUninit<T>` really | |
609 | /// is in an initialized state. | |
610 | /// | |
611 | /// # Examples | |
612 | /// | |
613 | /// ### Correct usage of this method: | |
614 | /// | |
615 | /// ```rust | |
616 | /// #![feature(maybe_uninit_ref)] | |
617 | /// use std::mem::MaybeUninit; | |
618 | /// | |
619 | /// let mut x = MaybeUninit::<Vec<u32>>::uninit(); | |
620 | /// // Initialize `x`: | |
621 | /// unsafe { x.as_mut_ptr().write(vec![1, 2, 3]); } | |
622 | /// // Now that our `MaybeUninit<_>` is known to be initialized, it is okay to | |
623 | /// // create a shared reference to it: | |
624 | /// let x: &Vec<u32> = unsafe { | |
1b1a35ee XL |
625 | /// // SAFETY: `x` has been initialized. |
626 | /// x.assume_init_ref() | |
60c5eb7d XL |
627 | /// }; |
628 | /// assert_eq!(x, &vec![1, 2, 3]); | |
629 | /// ``` | |
630 | /// | |
631 | /// ### *Incorrect* usages of this method: | |
632 | /// | |
633 | /// ```rust,no_run | |
634 | /// #![feature(maybe_uninit_ref)] | |
635 | /// use std::mem::MaybeUninit; | |
636 | /// | |
637 | /// let x = MaybeUninit::<Vec<u32>>::uninit(); | |
1b1a35ee | 638 | /// let x_vec: &Vec<u32> = unsafe { x.assume_init_ref() }; |
f9f354fc | 639 | /// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️ |
60c5eb7d XL |
640 | /// ``` |
641 | /// | |
642 | /// ```rust,no_run | |
643 | /// #![feature(maybe_uninit_ref)] | |
644 | /// use std::{cell::Cell, mem::MaybeUninit}; | |
645 | /// | |
646 | /// let b = MaybeUninit::<Cell<bool>>::uninit(); | |
647 | /// // Initialize the `MaybeUninit` using `Cell::set`: | |
648 | /// unsafe { | |
1b1a35ee XL |
649 | /// b.assume_init_ref().set(true); |
650 | /// // ^^^^^^^^^^^^^^^ | |
651 | /// // Reference to an uninitialized `Cell<bool>`: UB! | |
60c5eb7d XL |
652 | /// } |
653 | /// ``` | |
e1599b0c | 654 | #[unstable(feature = "maybe_uninit_ref", issue = "63568")] |
dc9dc135 | 655 | #[inline(always)] |
1b1a35ee | 656 | pub unsafe fn assume_init_ref(&self) -> &T { |
f035d41b XL |
657 | // SAFETY: the caller must guarantee that `self` is initialized. |
658 | // This also means that `self` must be a `value` variant. | |
659 | unsafe { | |
660 | intrinsics::assert_inhabited::<T>(); | |
661 | &*self.value | |
662 | } | |
dc9dc135 XL |
663 | } |
664 | ||
60c5eb7d XL |
665 | /// Gets a mutable (unique) reference to the contained value. |
666 | /// | |
667 | /// This can be useful when we want to access a `MaybeUninit` that has been | |
668 | /// initialized but don't have ownership of the `MaybeUninit` (preventing the use | |
669 | /// of `.assume_init()`). | |
dc9dc135 XL |
670 | /// |
671 | /// # Safety | |
672 | /// | |
60c5eb7d XL |
673 | /// Calling this when the content is not yet fully initialized causes undefined |
674 | /// behavior: it is up to the caller to guarantee that the `MaybeUninit<T>` really | |
1b1a35ee | 675 | /// is in an initialized state. For instance, `.assume_init_mut()` cannot be used to |
60c5eb7d XL |
676 | /// initialize a `MaybeUninit`. |
677 | /// | |
678 | /// # Examples | |
679 | /// | |
680 | /// ### Correct usage of this method: | |
681 | /// | |
682 | /// ```rust | |
683 | /// #![feature(maybe_uninit_ref)] | |
684 | /// use std::mem::MaybeUninit; | |
685 | /// | |
686 | /// # unsafe extern "C" fn initialize_buffer(buf: *mut [u8; 2048]) { *buf = [0; 2048] } | |
687 | /// # #[cfg(FALSE)] | |
688 | /// extern "C" { | |
689 | /// /// Initializes *all* the bytes of the input buffer. | |
690 | /// fn initialize_buffer(buf: *mut [u8; 2048]); | |
691 | /// } | |
692 | /// | |
693 | /// let mut buf = MaybeUninit::<[u8; 2048]>::uninit(); | |
694 | /// | |
695 | /// // Initialize `buf`: | |
696 | /// unsafe { initialize_buffer(buf.as_mut_ptr()); } | |
697 | /// // Now we know that `buf` has been initialized, so we could `.assume_init()` it. | |
698 | /// // However, using `.assume_init()` may trigger a `memcpy` of the 2048 bytes. | |
699 | /// // To assert our buffer has been initialized without copying it, we upgrade | |
700 | /// // the `&mut MaybeUninit<[u8; 2048]>` to a `&mut [u8; 2048]`: | |
701 | /// let buf: &mut [u8; 2048] = unsafe { | |
1b1a35ee XL |
702 | /// // SAFETY: `buf` has been initialized. |
703 | /// buf.assume_init_mut() | |
60c5eb7d XL |
704 | /// }; |
705 | /// | |
706 | /// // Now we can use `buf` as a normal slice: | |
707 | /// buf.sort_unstable(); | |
708 | /// assert!( | |
74b04a01 | 709 | /// buf.windows(2).all(|pair| pair[0] <= pair[1]), |
60c5eb7d XL |
710 | /// "buffer is sorted", |
711 | /// ); | |
712 | /// ``` | |
713 | /// | |
714 | /// ### *Incorrect* usages of this method: | |
715 | /// | |
1b1a35ee | 716 | /// You cannot use `.assume_init_mut()` to initialize a value: |
60c5eb7d XL |
717 | /// |
718 | /// ```rust,no_run | |
719 | /// #![feature(maybe_uninit_ref)] | |
720 | /// use std::mem::MaybeUninit; | |
721 | /// | |
722 | /// let mut b = MaybeUninit::<bool>::uninit(); | |
723 | /// unsafe { | |
1b1a35ee | 724 | /// *b.assume_init_mut() = true; |
60c5eb7d | 725 | /// // We have created a (mutable) reference to an uninitialized `bool`! |
f9f354fc | 726 | /// // This is undefined behavior. ⚠️ |
60c5eb7d XL |
727 | /// } |
728 | /// ``` | |
729 | /// | |
730 | /// For instance, you cannot [`Read`] into an uninitialized buffer: | |
731 | /// | |
732 | /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html | |
733 | /// | |
734 | /// ```rust,no_run | |
735 | /// #![feature(maybe_uninit_ref)] | |
736 | /// use std::{io, mem::MaybeUninit}; | |
737 | /// | |
738 | /// fn read_chunk (reader: &'_ mut dyn io::Read) -> io::Result<[u8; 64]> | |
739 | /// { | |
740 | /// let mut buffer = MaybeUninit::<[u8; 64]>::uninit(); | |
1b1a35ee XL |
741 | /// reader.read_exact(unsafe { buffer.assume_init_mut() })?; |
742 | /// // ^^^^^^^^^^^^^^^^^^^^^^^^ | |
60c5eb7d XL |
743 | /// // (mutable) reference to uninitialized memory! |
744 | /// // This is undefined behavior. | |
745 | /// Ok(unsafe { buffer.assume_init() }) | |
746 | /// } | |
747 | /// ``` | |
748 | /// | |
749 | /// Nor can you use direct field access to do field-by-field gradual initialization: | |
750 | /// | |
751 | /// ```rust,no_run | |
752 | /// #![feature(maybe_uninit_ref)] | |
753 | /// use std::{mem::MaybeUninit, ptr}; | |
754 | /// | |
755 | /// struct Foo { | |
756 | /// a: u32, | |
757 | /// b: u8, | |
758 | /// } | |
759 | /// | |
760 | /// let foo: Foo = unsafe { | |
761 | /// let mut foo = MaybeUninit::<Foo>::uninit(); | |
1b1a35ee XL |
762 | /// ptr::write(&mut foo.assume_init_mut().a as *mut u32, 1337); |
763 | /// // ^^^^^^^^^^^^^^^^^^^^^ | |
60c5eb7d XL |
764 | /// // (mutable) reference to uninitialized memory! |
765 | /// // This is undefined behavior. | |
1b1a35ee XL |
766 | /// ptr::write(&mut foo.assume_init_mut().b as *mut u8, 42); |
767 | /// // ^^^^^^^^^^^^^^^^^^^^^ | |
60c5eb7d XL |
768 | /// // (mutable) reference to uninitialized memory! |
769 | /// // This is undefined behavior. | |
770 | /// foo.assume_init() | |
771 | /// }; | |
772 | /// ``` | |
1b1a35ee | 773 | // FIXME(#76092): We currently rely on the above being incorrect, i.e., we have references |
dc9dc135 XL |
774 | // to uninitialized data (e.g., in `libcore/fmt/float.rs`). We should make |
775 | // a final decision about the rules before stabilization. | |
e1599b0c | 776 | #[unstable(feature = "maybe_uninit_ref", issue = "63568")] |
dc9dc135 | 777 | #[inline(always)] |
1b1a35ee | 778 | pub unsafe fn assume_init_mut(&mut self) -> &mut T { |
f035d41b XL |
779 | // SAFETY: the caller must guarantee that `self` is initialized. |
780 | // This also means that `self` must be a `value` variant. | |
781 | unsafe { | |
782 | intrinsics::assert_inhabited::<T>(); | |
783 | &mut *self.value | |
784 | } | |
dc9dc135 XL |
785 | } |
786 | ||
60c5eb7d XL |
787 | /// Assuming all the elements are initialized, get a slice to them. |
788 | /// | |
789 | /// # Safety | |
790 | /// | |
791 | /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements | |
792 | /// really are in an initialized state. | |
793 | /// Calling this when the content is not yet fully initialized causes undefined behavior. | |
1b1a35ee XL |
794 | /// |
795 | /// See [`assume_init_ref`] for more details and examples. | |
796 | /// | |
797 | /// [`assume_init_ref`]: MaybeUninit::assume_init_ref | |
798 | #[unstable(feature = "maybe_uninit_slice", issue = "63569")] | |
60c5eb7d | 799 | #[inline(always)] |
1b1a35ee | 800 | pub unsafe fn slice_assume_init_ref(slice: &[Self]) -> &[T] { |
f035d41b XL |
801 | // SAFETY: casting slice to a `*const [T]` is safe since the caller guarantees that |
802 | // `slice` is initialized, and`MaybeUninit` is guaranteed to have the same layout as `T`. | |
803 | // The pointer obtained is valid since it refers to memory owned by `slice` which is a | |
804 | // reference and thus guaranteed to be valid for reads. | |
805 | unsafe { &*(slice as *const [Self] as *const [T]) } | |
60c5eb7d XL |
806 | } |
807 | ||
808 | /// Assuming all the elements are initialized, get a mutable slice to them. | |
809 | /// | |
810 | /// # Safety | |
811 | /// | |
812 | /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements | |
813 | /// really are in an initialized state. | |
814 | /// Calling this when the content is not yet fully initialized causes undefined behavior. | |
1b1a35ee XL |
815 | /// |
816 | /// See [`assume_init_mut`] for more details and examples. | |
817 | /// | |
818 | /// [`assume_init_mut`]: MaybeUninit::assume_init_mut | |
819 | #[unstable(feature = "maybe_uninit_slice", issue = "63569")] | |
60c5eb7d | 820 | #[inline(always)] |
1b1a35ee | 821 | pub unsafe fn slice_assume_init_mut(slice: &mut [Self]) -> &mut [T] { |
f035d41b XL |
822 | // SAFETY: similar to safety notes for `slice_get_ref`, but we have a |
823 | // mutable reference which is also guaranteed to be valid for writes. | |
824 | unsafe { &mut *(slice as *mut [Self] as *mut [T]) } | |
60c5eb7d XL |
825 | } |
826 | ||
dc9dc135 | 827 | /// Gets a pointer to the first element of the array. |
e1599b0c | 828 | #[unstable(feature = "maybe_uninit_slice", issue = "63569")] |
dc9dc135 | 829 | #[inline(always)] |
1b1a35ee | 830 | pub fn slice_as_ptr(this: &[MaybeUninit<T>]) -> *const T { |
dc9dc135 XL |
831 | this as *const [MaybeUninit<T>] as *const T |
832 | } | |
833 | ||
834 | /// Gets a mutable pointer to the first element of the array. | |
e1599b0c | 835 | #[unstable(feature = "maybe_uninit_slice", issue = "63569")] |
dc9dc135 | 836 | #[inline(always)] |
1b1a35ee | 837 | pub fn slice_as_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T { |
dc9dc135 XL |
838 | this as *mut [MaybeUninit<T>] as *mut T |
839 | } | |
840 | } |