]> git.proxmox.com Git - rustc.git/blame - library/core/src/mem/maybe_uninit.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / library / core / src / mem / maybe_uninit.rs
CommitLineData
60c5eb7d
XL
1use crate::any::type_name;
2use crate::fmt;
dc9dc135
XL
3use crate::intrinsics;
4use crate::mem::ManuallyDrop;
1b1a35ee 5use 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///
6a06907d
XL
42/// Moreover, uninitialized memory is special in that it does not have a fixed value ("fixed"
43/// meaning "it won't change without being written to"). Reading the same uninitialized byte
44/// multiple times can give different results. This makes it undefined behavior to have
45/// uninitialized data in a variable even if that variable has an integer type, which otherwise can
46/// hold any *fixed* bit pattern:
dc9dc135
XL
47///
48/// ```rust,no_run
416331ca 49/// # #![allow(invalid_value)]
dc9dc135
XL
50/// use std::mem::{self, MaybeUninit};
51///
f9f354fc 52/// let x: i32 = unsafe { mem::uninitialized() }; // undefined behavior! ⚠️
dc9dc135 53/// // The equivalent code with `MaybeUninit<i32>`:
f9f354fc 54/// let x: i32 = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior! ⚠️
dc9dc135
XL
55/// ```
56/// (Notice that the rules around uninitialized integers are not finalized yet, but
57/// until they are, it is advisable to avoid them.)
58///
59/// On top of that, remember that most types have additional invariants beyond merely
60/// being considered initialized at the type level. For example, a `1`-initialized [`Vec<T>`]
416331ca
XL
61/// is considered initialized (under the current implementation; this does not constitute
62/// a stable guarantee) because the only requirement the compiler knows about it
dc9dc135
XL
63/// is that the data pointer must be non-null. Creating such a `Vec<T>` does not cause
64/// *immediate* undefined behavior, but will cause undefined behavior with most
65/// safe operations (including dropping it).
66///
67/// [`Vec<T>`]: ../../std/vec/struct.Vec.html
68///
69/// # Examples
70///
71/// `MaybeUninit<T>` serves to enable unsafe code to deal with uninitialized data.
72/// It is a signal to the compiler indicating that the data here might *not*
73/// be initialized:
74///
75/// ```rust
76/// use std::mem::MaybeUninit;
77///
78/// // Create an explicitly uninitialized reference. The compiler knows that data inside
79/// // a `MaybeUninit<T>` may be invalid, and hence this is not UB:
80/// let mut x = MaybeUninit::<&i32>::uninit();
81/// // Set it to a valid value.
82/// unsafe { x.as_mut_ptr().write(&0); }
83/// // Extract the initialized data -- this is only allowed *after* properly
84/// // initializing `x`!
85/// let x = unsafe { x.assume_init() };
86/// ```
87///
88/// The compiler then knows to not make any incorrect assumptions or optimizations on this code.
89///
90/// You can think of `MaybeUninit<T>` as being a bit like `Option<T>` but without
91/// any of the run-time tracking and without any of the safety checks.
92///
93/// ## out-pointers
94///
95/// You can use `MaybeUninit<T>` to implement "out-pointers": instead of returning data
96/// from a function, pass it a pointer to some (uninitialized) memory to put the
97/// result into. This can be useful when it is important for the caller to control
98/// how the memory the result is stored in gets allocated, and you want to avoid
99/// unnecessary moves.
100///
101/// ```
102/// use std::mem::MaybeUninit;
103///
104/// unsafe fn make_vec(out: *mut Vec<i32>) {
105/// // `write` does not drop the old contents, which is important.
106/// out.write(vec![1, 2, 3]);
107/// }
108///
109/// let mut v = MaybeUninit::uninit();
110/// unsafe { make_vec(v.as_mut_ptr()); }
111/// // Now we know `v` is initialized! This also makes sure the vector gets
112/// // properly dropped.
113/// let v = unsafe { v.assume_init() };
114/// assert_eq!(&v, &[1, 2, 3]);
115/// ```
116///
117/// ## Initializing an array element-by-element
118///
119/// `MaybeUninit<T>` can be used to initialize a large array element-by-element:
120///
121/// ```
122/// use std::mem::{self, MaybeUninit};
dc9dc135
XL
123///
124/// let data = {
125/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
126/// // safe because the type we are claiming to have initialized here is a
127/// // bunch of `MaybeUninit`s, which do not require initialization.
128/// let mut data: [MaybeUninit<Vec<u32>>; 1000] = unsafe {
129/// MaybeUninit::uninit().assume_init()
130/// };
131///
416331ca
XL
132/// // Dropping a `MaybeUninit` does nothing. Thus using raw pointer
133/// // assignment instead of `ptr::write` does not cause the old
134/// // uninitialized value to be dropped. Also if there is a panic during
135/// // this loop, we have a memory leak, but there is no memory safety
136/// // issue.
dc9dc135 137/// for elem in &mut data[..] {
416331ca 138/// *elem = MaybeUninit::new(vec![42]);
dc9dc135
XL
139/// }
140///
141/// // Everything is initialized. Transmute the array to the
142/// // initialized type.
143/// unsafe { mem::transmute::<_, [Vec<u32>; 1000]>(data) }
144/// };
145///
146/// assert_eq!(&data[0], &[42]);
147/// ```
148///
149/// You can also work with partially initialized arrays, which could
150/// be found in low-level datastructures.
151///
152/// ```
153/// use std::mem::MaybeUninit;
154/// use std::ptr;
155///
156/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
157/// // safe because the type we are claiming to have initialized here is a
158/// // bunch of `MaybeUninit`s, which do not require initialization.
159/// let mut data: [MaybeUninit<String>; 1000] = unsafe { MaybeUninit::uninit().assume_init() };
160/// // Count the number of elements we have assigned.
161/// let mut data_len: usize = 0;
162///
163/// for elem in &mut data[0..500] {
416331ca 164/// *elem = MaybeUninit::new(String::from("hello"));
dc9dc135
XL
165/// data_len += 1;
166/// }
167///
168/// // For each item in the array, drop if we allocated it.
169/// for elem in &mut data[0..data_len] {
170/// unsafe { ptr::drop_in_place(elem.as_mut_ptr()); }
171/// }
172/// ```
173///
174/// ## Initializing a struct field-by-field
175///
5869c6ff 176/// You can use `MaybeUninit<T>`, and the [`std::ptr::addr_of_mut`] macro, to initialize structs field by field:
dc9dc135 177///
5869c6ff
XL
178/// ```rust
179/// use std::mem::MaybeUninit;
180/// use std::ptr::addr_of_mut;
181///
182/// #[derive(Debug, PartialEq)]
183/// pub struct Foo {
184/// name: String,
185/// list: Vec<u8>,
186/// }
187///
188/// let foo = {
189/// let mut uninit: MaybeUninit<Foo> = MaybeUninit::uninit();
190/// let ptr = uninit.as_mut_ptr();
191///
192/// // Initializing the `name` field
193/// unsafe { addr_of_mut!((*ptr).name).write("Bob".to_string()); }
194///
195/// // Initializing the `list` field
196/// // If there is a panic here, then the `String` in the `name` field leaks.
197/// unsafe { addr_of_mut!((*ptr).list).write(vec![0, 1, 2]); }
198///
199/// // All the fields are initialized, so we call `assume_init` to get an initialized Foo.
200/// unsafe { uninit.assume_init() }
201/// };
202///
203/// assert_eq!(
204/// foo,
205/// Foo {
206/// name: "Bob".to_string(),
207/// list: vec![0, 1, 2]
208/// }
209/// );
210/// ```
211/// [`std::ptr::addr_of_mut`]: crate::ptr::addr_of_mut
dc9dc135
XL
212/// [ub]: ../../reference/behavior-considered-undefined.html
213///
214/// # Layout
215///
216/// `MaybeUninit<T>` is guaranteed to have the same size, alignment, and ABI as `T`:
217///
218/// ```rust
219/// use std::mem::{MaybeUninit, size_of, align_of};
220/// assert_eq!(size_of::<MaybeUninit<u64>>(), size_of::<u64>());
221/// assert_eq!(align_of::<MaybeUninit<u64>>(), align_of::<u64>());
222/// ```
223///
224/// However remember that a type *containing* a `MaybeUninit<T>` is not necessarily the same
225/// layout; Rust does not in general guarantee that the fields of a `Foo<T>` have the same order as
226/// a `Foo<U>` even if `T` and `U` have the same size and alignment. Furthermore because any bit
227/// value is valid for a `MaybeUninit<T>` the compiler can't apply non-zero/niche-filling
228/// optimizations, potentially resulting in a larger size:
229///
230/// ```rust
231/// # use std::mem::{MaybeUninit, size_of};
232/// assert_eq!(size_of::<Option<bool>>(), 1);
233/// assert_eq!(size_of::<Option<MaybeUninit<bool>>>(), 2);
234/// ```
235///
236/// If `T` is FFI-safe, then so is `MaybeUninit<T>`.
237///
238/// While `MaybeUninit` is `#[repr(transparent)]` (indicating it guarantees the same size,
239/// alignment, and ABI as `T`), this does *not* change any of the previous caveats. `Option<T>` and
240/// `Option<MaybeUninit<T>>` may still have different sizes, and types containing a field of type
241/// `T` may be laid out (and sized) differently than if that field were `MaybeUninit<T>`.
242/// `MaybeUninit` is a union type, and `#[repr(transparent)]` on unions is unstable (see [the
243/// tracking issue](https://github.com/rust-lang/rust/issues/60405)). Over time, the exact
244/// guarantees of `#[repr(transparent)]` on unions may evolve, and `MaybeUninit` may or may not
245/// remain `#[repr(transparent)]`. That said, `MaybeUninit<T>` will *always* guarantee that it has
246/// the same size, alignment, and ABI as `T`; it's just that the way `MaybeUninit` implements that
247/// guarantee may evolve.
dc9dc135 248#[stable(feature = "maybe_uninit", since = "1.36.0")]
416331ca 249// Lang item so we can wrap other types in it. This is useful for generators.
e1599b0c 250#[lang = "maybe_uninit"]
dc9dc135 251#[derive(Copy)]
416331ca 252#[repr(transparent)]
dc9dc135
XL
253pub union MaybeUninit<T> {
254 uninit: (),
255 value: ManuallyDrop<T>,
256}
257
258#[stable(feature = "maybe_uninit", since = "1.36.0")]
259impl<T: Copy> Clone for MaybeUninit<T> {
260 #[inline(always)]
261 fn clone(&self) -> Self {
262 // Not calling `T::clone()`, we cannot know if we are initialized enough for that.
263 *self
264 }
265}
266
60c5eb7d
XL
267#[stable(feature = "maybe_uninit_debug", since = "1.41.0")]
268impl<T> fmt::Debug for MaybeUninit<T> {
269 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
270 f.pad(type_name::<Self>())
271 }
272}
273
dc9dc135
XL
274impl<T> MaybeUninit<T> {
275 /// Creates a new `MaybeUninit<T>` initialized with the given value.
276 /// It is safe to call [`assume_init`] on the return value of this function.
277 ///
278 /// Note that dropping a `MaybeUninit<T>` will never call `T`'s drop code.
279 /// It is your responsibility to make sure `T` gets dropped if it got initialized.
280 ///
29967ef6
XL
281 /// # Example
282 ///
283 /// ```
284 /// use std::mem::MaybeUninit;
285 ///
286 /// let v: MaybeUninit<Vec<u8>> = MaybeUninit::new(vec![42]);
287 /// ```
288 ///
3dfed10e 289 /// [`assume_init`]: MaybeUninit::assume_init
dc9dc135 290 #[stable(feature = "maybe_uninit", since = "1.36.0")]
dfeec247 291 #[rustc_const_stable(feature = "const_maybe_uninit", since = "1.36.0")]
dc9dc135
XL
292 #[inline(always)]
293 pub const fn new(val: T) -> MaybeUninit<T> {
294 MaybeUninit { value: ManuallyDrop::new(val) }
295 }
296
297 /// Creates a new `MaybeUninit<T>` in an uninitialized state.
298 ///
299 /// Note that dropping a `MaybeUninit<T>` will never call `T`'s drop code.
300 /// It is your responsibility to make sure `T` gets dropped if it got initialized.
301 ///
29967ef6 302 /// See the [type-level documentation][MaybeUninit] for some examples.
dc9dc135 303 ///
29967ef6
XL
304 /// # Example
305 ///
306 /// ```
307 /// use std::mem::MaybeUninit;
308 ///
309 /// let v: MaybeUninit<String> = MaybeUninit::uninit();
310 /// ```
dc9dc135 311 #[stable(feature = "maybe_uninit", since = "1.36.0")]
dfeec247 312 #[rustc_const_stable(feature = "const_maybe_uninit", since = "1.36.0")]
dc9dc135 313 #[inline(always)]
dfeec247 314 #[rustc_diagnostic_item = "maybe_uninit_uninit"]
dc9dc135
XL
315 pub const fn uninit() -> MaybeUninit<T> {
316 MaybeUninit { uninit: () }
317 }
318
60c5eb7d
XL
319 /// Create a new array of `MaybeUninit<T>` items, in an uninitialized state.
320 ///
321 /// Note: in a future Rust version this method may become unnecessary
322 /// when array literal syntax allows
323 /// [repeating const expressions](https://github.com/rust-lang/rust/issues/49147).
324 /// The example below could then use `let mut buf = [MaybeUninit::<u8>::uninit(); 32];`.
325 ///
326 /// # Examples
327 ///
328 /// ```no_run
1b1a35ee 329 /// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice)]
60c5eb7d
XL
330 ///
331 /// use std::mem::MaybeUninit;
332 ///
333 /// extern "C" {
334 /// fn read_into_buffer(ptr: *mut u8, max_len: usize) -> usize;
335 /// }
336 ///
337 /// /// Returns a (possibly smaller) slice of data that was actually read
338 /// fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] {
339 /// unsafe {
340 /// let len = read_into_buffer(buf.as_mut_ptr() as *mut u8, buf.len());
1b1a35ee 341 /// MaybeUninit::slice_assume_init_ref(&buf[..len])
60c5eb7d
XL
342 /// }
343 /// }
344 ///
345 /// let mut buf: [MaybeUninit<u8>; 32] = MaybeUninit::uninit_array();
346 /// let data = read(&mut buf);
347 /// ```
dfeec247 348 #[unstable(feature = "maybe_uninit_uninit_array", issue = "none")]
fc512014 349 #[rustc_const_unstable(feature = "maybe_uninit_uninit_array", issue = "none")]
60c5eb7d 350 #[inline(always)]
fc512014 351 pub const fn uninit_array<const LEN: usize>() -> [Self; LEN] {
1b1a35ee 352 // SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
dfeec247 353 unsafe { MaybeUninit::<[MaybeUninit<T>; LEN]>::uninit().assume_init() }
60c5eb7d
XL
354 }
355
dc9dc135
XL
356 /// Creates a new `MaybeUninit<T>` in an uninitialized state, with the memory being
357 /// filled with `0` bytes. It depends on `T` whether that already makes for
358 /// proper initialization. For example, `MaybeUninit<usize>::zeroed()` is initialized,
359 /// but `MaybeUninit<&'static i32>::zeroed()` is not because references must not
360 /// be null.
361 ///
362 /// Note that dropping a `MaybeUninit<T>` will never call `T`'s drop code.
363 /// It is your responsibility to make sure `T` gets dropped if it got initialized.
364 ///
365 /// # Example
366 ///
367 /// Correct usage of this function: initializing a struct with zero, where all
368 /// fields of the struct can hold the bit-pattern 0 as a valid value.
369 ///
370 /// ```rust
371 /// use std::mem::MaybeUninit;
372 ///
373 /// let x = MaybeUninit::<(u8, bool)>::zeroed();
374 /// let x = unsafe { x.assume_init() };
375 /// assert_eq!(x, (0, false));
376 /// ```
377 ///
3dfed10e
XL
378 /// *Incorrect* usage of this function: calling `x.zeroed().assume_init()`
379 /// when `0` is not a valid bit-pattern for the type:
dc9dc135
XL
380 ///
381 /// ```rust,no_run
382 /// use std::mem::MaybeUninit;
383 ///
fc512014 384 /// enum NotZero { One = 1, Two = 2 }
dc9dc135
XL
385 ///
386 /// let x = MaybeUninit::<(u8, NotZero)>::zeroed();
387 /// let x = unsafe { x.assume_init() };
388 /// // Inside a pair, we create a `NotZero` that does not have a valid discriminant.
f9f354fc 389 /// // This is undefined behavior. ⚠️
dc9dc135
XL
390 /// ```
391 #[stable(feature = "maybe_uninit", since = "1.36.0")]
392 #[inline]
dfeec247 393 #[rustc_diagnostic_item = "maybe_uninit_zeroed"]
dc9dc135
XL
394 pub fn zeroed() -> MaybeUninit<T> {
395 let mut u = MaybeUninit::<T>::uninit();
1b1a35ee 396 // SAFETY: `u.as_mut_ptr()` points to allocated memory.
dc9dc135
XL
397 unsafe {
398 u.as_mut_ptr().write_bytes(0u8, 1);
399 }
400 u
401 }
402
403 /// Sets the value of the `MaybeUninit<T>`. This overwrites any previous value
404 /// without dropping it, so be careful not to use this twice unless you want to
405 /// skip running the destructor. For your convenience, this also returns a mutable
406 /// reference to the (now safely initialized) contents of `self`.
e1599b0c 407 #[unstable(feature = "maybe_uninit_extra", issue = "63567")]
fc512014 408 #[rustc_const_unstable(feature = "maybe_uninit_extra", issue = "63567")]
dc9dc135 409 #[inline(always)]
fc512014 410 pub const fn write(&mut self, val: T) -> &mut T {
1b1a35ee
XL
411 *self = MaybeUninit::new(val);
412 // SAFETY: We just initialized this value.
413 unsafe { self.assume_init_mut() }
dc9dc135
XL
414 }
415
416 /// Gets a pointer to the contained value. Reading from this pointer or turning it
417 /// into a reference is undefined behavior unless the `MaybeUninit<T>` is initialized.
418 /// Writing to memory that this pointer (non-transitively) points to is undefined behavior
419 /// (except inside an `UnsafeCell<T>`).
420 ///
421 /// # Examples
422 ///
423 /// Correct usage of this method:
424 ///
425 /// ```rust
426 /// use std::mem::MaybeUninit;
427 ///
428 /// let mut x = MaybeUninit::<Vec<u32>>::uninit();
fc512014 429 /// unsafe { x.as_mut_ptr().write(vec![0, 1, 2]); }
dc9dc135
XL
430 /// // Create a reference into the `MaybeUninit<T>`. This is okay because we initialized it.
431 /// let x_vec = unsafe { &*x.as_ptr() };
432 /// assert_eq!(x_vec.len(), 3);
433 /// ```
434 ///
435 /// *Incorrect* usage of this method:
436 ///
437 /// ```rust,no_run
438 /// use std::mem::MaybeUninit;
439 ///
440 /// let x = MaybeUninit::<Vec<u32>>::uninit();
441 /// let x_vec = unsafe { &*x.as_ptr() };
f9f354fc 442 /// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️
dc9dc135
XL
443 /// ```
444 ///
445 /// (Notice that the rules around references to uninitialized data are not finalized yet, but
446 /// until they are, it is advisable to avoid them.)
447 #[stable(feature = "maybe_uninit", since = "1.36.0")]
3dfed10e 448 #[rustc_const_unstable(feature = "const_maybe_uninit_as_ptr", issue = "75251")]
dc9dc135 449 #[inline(always)]
3dfed10e
XL
450 pub const fn as_ptr(&self) -> *const T {
451 // `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
452 self as *const _ as *const T
dc9dc135
XL
453 }
454
455 /// Gets a mutable pointer to the contained value. Reading from this pointer or turning it
456 /// into a reference is undefined behavior unless the `MaybeUninit<T>` is initialized.
457 ///
458 /// # Examples
459 ///
460 /// Correct usage of this method:
461 ///
462 /// ```rust
463 /// use std::mem::MaybeUninit;
464 ///
465 /// let mut x = MaybeUninit::<Vec<u32>>::uninit();
fc512014 466 /// unsafe { x.as_mut_ptr().write(vec![0, 1, 2]); }
dc9dc135
XL
467 /// // Create a reference into the `MaybeUninit<Vec<u32>>`.
468 /// // This is okay because we initialized it.
469 /// let x_vec = unsafe { &mut *x.as_mut_ptr() };
470 /// x_vec.push(3);
471 /// assert_eq!(x_vec.len(), 4);
472 /// ```
473 ///
474 /// *Incorrect* usage of this method:
475 ///
476 /// ```rust,no_run
477 /// use std::mem::MaybeUninit;
478 ///
479 /// let mut x = MaybeUninit::<Vec<u32>>::uninit();
480 /// let x_vec = unsafe { &mut *x.as_mut_ptr() };
f9f354fc 481 /// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️
dc9dc135
XL
482 /// ```
483 ///
484 /// (Notice that the rules around references to uninitialized data are not finalized yet, but
485 /// until they are, it is advisable to avoid them.)
486 #[stable(feature = "maybe_uninit", since = "1.36.0")]
3dfed10e 487 #[rustc_const_unstable(feature = "const_maybe_uninit_as_ptr", issue = "75251")]
dc9dc135 488 #[inline(always)]
3dfed10e
XL
489 pub const fn as_mut_ptr(&mut self) -> *mut T {
490 // `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
491 self as *mut _ as *mut T
dc9dc135
XL
492 }
493
494 /// Extracts the value from the `MaybeUninit<T>` container. This is a great way
495 /// to ensure that the data will get dropped, because the resulting `T` is
496 /// subject to the usual drop handling.
497 ///
498 /// # Safety
499 ///
500 /// It is up to the caller to guarantee that the `MaybeUninit<T>` really is in an initialized
501 /// state. Calling this when the content is not yet fully initialized causes immediate undefined
502 /// behavior. The [type-level documentation][inv] contains more information about
503 /// this initialization invariant.
504 ///
505 /// [inv]: #initialization-invariant
506 ///
416331ca
XL
507 /// On top of that, remember that most types have additional invariants beyond merely
508 /// being considered initialized at the type level. For example, a `1`-initialized [`Vec<T>`]
509 /// is considered initialized (under the current implementation; this does not constitute
510 /// a stable guarantee) because the only requirement the compiler knows about it
511 /// is that the data pointer must be non-null. Creating such a `Vec<T>` does not cause
512 /// *immediate* undefined behavior, but will cause undefined behavior with most
513 /// safe operations (including dropping it).
514 ///
1b1a35ee
XL
515 /// [`Vec<T>`]: ../../std/vec/struct.Vec.html
516 ///
dc9dc135
XL
517 /// # Examples
518 ///
519 /// Correct usage of this method:
520 ///
521 /// ```rust
522 /// use std::mem::MaybeUninit;
523 ///
524 /// let mut x = MaybeUninit::<bool>::uninit();
525 /// unsafe { x.as_mut_ptr().write(true); }
526 /// let x_init = unsafe { x.assume_init() };
527 /// assert_eq!(x_init, true);
528 /// ```
529 ///
530 /// *Incorrect* usage of this method:
531 ///
532 /// ```rust,no_run
533 /// use std::mem::MaybeUninit;
534 ///
535 /// let x = MaybeUninit::<Vec<u32>>::uninit();
536 /// let x_init = unsafe { x.assume_init() };
f9f354fc 537 /// // `x` had not been initialized yet, so this last line caused undefined behavior. ⚠️
dc9dc135
XL
538 /// ```
539 #[stable(feature = "maybe_uninit", since = "1.36.0")]
fc512014 540 #[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
dc9dc135 541 #[inline(always)]
dfeec247 542 #[rustc_diagnostic_item = "assume_init"]
fc512014 543 pub const unsafe fn assume_init(self) -> T {
f035d41b
XL
544 // SAFETY: the caller must guarantee that `self` is initialized.
545 // This also means that `self` must be a `value` variant.
546 unsafe {
547 intrinsics::assert_inhabited::<T>();
548 ManuallyDrop::into_inner(self.value)
549 }
dc9dc135
XL
550 }
551
552 /// Reads the value from the `MaybeUninit<T>` container. The resulting `T` is subject
553 /// to the usual drop handling.
554 ///
416331ca 555 /// Whenever possible, it is preferable to use [`assume_init`] instead, which
dc9dc135
XL
556 /// prevents duplicating the content of the `MaybeUninit<T>`.
557 ///
558 /// # Safety
559 ///
560 /// It is up to the caller to guarantee that the `MaybeUninit<T>` really is in an initialized
561 /// state. Calling this when the content is not yet fully initialized causes undefined
562 /// behavior. The [type-level documentation][inv] contains more information about
563 /// this initialization invariant.
564 ///
565 /// Moreover, this leaves a copy of the same data behind in the `MaybeUninit<T>`. When using
1b1a35ee
XL
566 /// multiple copies of the data (by calling `assume_init_read` multiple times, or first
567 /// calling `assume_init_read` and then [`assume_init`]), it is your responsibility
dc9dc135
XL
568 /// to ensure that that data may indeed be duplicated.
569 ///
570 /// [inv]: #initialization-invariant
3dfed10e 571 /// [`assume_init`]: MaybeUninit::assume_init
dc9dc135
XL
572 ///
573 /// # Examples
574 ///
575 /// Correct usage of this method:
576 ///
577 /// ```rust
578 /// #![feature(maybe_uninit_extra)]
579 /// use std::mem::MaybeUninit;
580 ///
581 /// let mut x = MaybeUninit::<u32>::uninit();
582 /// x.write(13);
1b1a35ee 583 /// let x1 = unsafe { x.assume_init_read() };
dc9dc135 584 /// // `u32` is `Copy`, so we may read multiple times.
1b1a35ee 585 /// let x2 = unsafe { x.assume_init_read() };
dc9dc135
XL
586 /// assert_eq!(x1, x2);
587 ///
588 /// let mut x = MaybeUninit::<Option<Vec<u32>>>::uninit();
589 /// x.write(None);
1b1a35ee 590 /// let x1 = unsafe { x.assume_init_read() };
dc9dc135 591 /// // Duplicating a `None` value is okay, so we may read multiple times.
1b1a35ee 592 /// let x2 = unsafe { x.assume_init_read() };
dc9dc135
XL
593 /// assert_eq!(x1, x2);
594 /// ```
595 ///
596 /// *Incorrect* usage of this method:
597 ///
598 /// ```rust,no_run
599 /// #![feature(maybe_uninit_extra)]
600 /// use std::mem::MaybeUninit;
601 ///
602 /// let mut x = MaybeUninit::<Option<Vec<u32>>>::uninit();
fc512014 603 /// x.write(Some(vec![0, 1, 2]));
1b1a35ee
XL
604 /// let x1 = unsafe { x.assume_init_read() };
605 /// let x2 = unsafe { x.assume_init_read() };
f9f354fc 606 /// // We now created two copies of the same vector, leading to a double-free ⚠️ when
dc9dc135
XL
607 /// // they both get dropped!
608 /// ```
e1599b0c 609 #[unstable(feature = "maybe_uninit_extra", issue = "63567")]
5869c6ff 610 #[rustc_const_unstable(feature = "maybe_uninit_extra", issue = "63567")]
dc9dc135 611 #[inline(always)]
5869c6ff 612 pub const unsafe fn assume_init_read(&self) -> T {
f035d41b
XL
613 // SAFETY: the caller must guarantee that `self` is initialized.
614 // Reading from `self.as_ptr()` is safe since `self` should be initialized.
615 unsafe {
616 intrinsics::assert_inhabited::<T>();
617 self.as_ptr().read()
618 }
dc9dc135
XL
619 }
620
1b1a35ee
XL
621 /// Drops the contained value in place.
622 ///
623 /// If you have ownership of the `MaybeUninit`, you can use [`assume_init`] instead.
624 ///
625 /// # Safety
626 ///
627 /// It is up to the caller to guarantee that the `MaybeUninit<T>` really is
628 /// in an initialized state. Calling this when the content is not yet fully
629 /// initialized causes undefined behavior.
630 ///
631 /// On top of that, all additional invariants of the type `T` must be
632 /// satisfied, as the `Drop` implementation of `T` (or its members) may
633 /// rely on this. For example, a `1`-initialized [`Vec<T>`] is considered
634 /// initialized (under the current implementation; this does not constitute
635 /// a stable guarantee) because the only requirement the compiler knows
636 /// about it is that the data pointer must be non-null. Dropping such a
637 /// `Vec<T>` however will cause undefined behaviour.
638 ///
639 /// [`assume_init`]: MaybeUninit::assume_init
640 /// [`Vec<T>`]: ../../std/vec/struct.Vec.html
641 #[unstable(feature = "maybe_uninit_extra", issue = "63567")]
642 pub unsafe fn assume_init_drop(&mut self) {
643 // SAFETY: the caller must guarantee that `self` is initialized and
644 // satisfies all invariants of `T`.
645 // Dropping the value in place is safe if that is the case.
646 unsafe { ptr::drop_in_place(self.as_mut_ptr()) }
647 }
648
60c5eb7d
XL
649 /// Gets a shared reference to the contained value.
650 ///
651 /// This can be useful when we want to access a `MaybeUninit` that has been
652 /// initialized but don't have ownership of the `MaybeUninit` (preventing the use
653 /// of `.assume_init()`).
dc9dc135
XL
654 ///
655 /// # Safety
656 ///
60c5eb7d
XL
657 /// Calling this when the content is not yet fully initialized causes undefined
658 /// behavior: it is up to the caller to guarantee that the `MaybeUninit<T>` really
659 /// is in an initialized state.
660 ///
661 /// # Examples
662 ///
663 /// ### Correct usage of this method:
664 ///
665 /// ```rust
666 /// #![feature(maybe_uninit_ref)]
667 /// use std::mem::MaybeUninit;
668 ///
669 /// let mut x = MaybeUninit::<Vec<u32>>::uninit();
670 /// // Initialize `x`:
671 /// unsafe { x.as_mut_ptr().write(vec![1, 2, 3]); }
672 /// // Now that our `MaybeUninit<_>` is known to be initialized, it is okay to
673 /// // create a shared reference to it:
674 /// let x: &Vec<u32> = unsafe {
1b1a35ee
XL
675 /// // SAFETY: `x` has been initialized.
676 /// x.assume_init_ref()
60c5eb7d
XL
677 /// };
678 /// assert_eq!(x, &vec![1, 2, 3]);
679 /// ```
680 ///
681 /// ### *Incorrect* usages of this method:
682 ///
683 /// ```rust,no_run
684 /// #![feature(maybe_uninit_ref)]
685 /// use std::mem::MaybeUninit;
686 ///
687 /// let x = MaybeUninit::<Vec<u32>>::uninit();
1b1a35ee 688 /// let x_vec: &Vec<u32> = unsafe { x.assume_init_ref() };
f9f354fc 689 /// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️
60c5eb7d
XL
690 /// ```
691 ///
692 /// ```rust,no_run
693 /// #![feature(maybe_uninit_ref)]
694 /// use std::{cell::Cell, mem::MaybeUninit};
695 ///
696 /// let b = MaybeUninit::<Cell<bool>>::uninit();
697 /// // Initialize the `MaybeUninit` using `Cell::set`:
698 /// unsafe {
1b1a35ee
XL
699 /// b.assume_init_ref().set(true);
700 /// // ^^^^^^^^^^^^^^^
701 /// // Reference to an uninitialized `Cell<bool>`: UB!
60c5eb7d
XL
702 /// }
703 /// ```
e1599b0c 704 #[unstable(feature = "maybe_uninit_ref", issue = "63568")]
fc512014 705 #[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
dc9dc135 706 #[inline(always)]
fc512014 707 pub const unsafe fn assume_init_ref(&self) -> &T {
f035d41b
XL
708 // SAFETY: the caller must guarantee that `self` is initialized.
709 // This also means that `self` must be a `value` variant.
710 unsafe {
711 intrinsics::assert_inhabited::<T>();
fc512014 712 &*self.as_ptr()
f035d41b 713 }
dc9dc135
XL
714 }
715
60c5eb7d
XL
716 /// Gets a mutable (unique) reference to the contained value.
717 ///
718 /// This can be useful when we want to access a `MaybeUninit` that has been
719 /// initialized but don't have ownership of the `MaybeUninit` (preventing the use
720 /// of `.assume_init()`).
dc9dc135
XL
721 ///
722 /// # Safety
723 ///
60c5eb7d
XL
724 /// Calling this when the content is not yet fully initialized causes undefined
725 /// behavior: it is up to the caller to guarantee that the `MaybeUninit<T>` really
1b1a35ee 726 /// is in an initialized state. For instance, `.assume_init_mut()` cannot be used to
60c5eb7d
XL
727 /// initialize a `MaybeUninit`.
728 ///
729 /// # Examples
730 ///
731 /// ### Correct usage of this method:
732 ///
733 /// ```rust
734 /// #![feature(maybe_uninit_ref)]
735 /// use std::mem::MaybeUninit;
736 ///
737 /// # unsafe extern "C" fn initialize_buffer(buf: *mut [u8; 2048]) { *buf = [0; 2048] }
738 /// # #[cfg(FALSE)]
739 /// extern "C" {
740 /// /// Initializes *all* the bytes of the input buffer.
741 /// fn initialize_buffer(buf: *mut [u8; 2048]);
742 /// }
743 ///
744 /// let mut buf = MaybeUninit::<[u8; 2048]>::uninit();
745 ///
746 /// // Initialize `buf`:
747 /// unsafe { initialize_buffer(buf.as_mut_ptr()); }
748 /// // Now we know that `buf` has been initialized, so we could `.assume_init()` it.
749 /// // However, using `.assume_init()` may trigger a `memcpy` of the 2048 bytes.
750 /// // To assert our buffer has been initialized without copying it, we upgrade
751 /// // the `&mut MaybeUninit<[u8; 2048]>` to a `&mut [u8; 2048]`:
752 /// let buf: &mut [u8; 2048] = unsafe {
1b1a35ee
XL
753 /// // SAFETY: `buf` has been initialized.
754 /// buf.assume_init_mut()
60c5eb7d
XL
755 /// };
756 ///
757 /// // Now we can use `buf` as a normal slice:
758 /// buf.sort_unstable();
759 /// assert!(
74b04a01 760 /// buf.windows(2).all(|pair| pair[0] <= pair[1]),
60c5eb7d
XL
761 /// "buffer is sorted",
762 /// );
763 /// ```
764 ///
765 /// ### *Incorrect* usages of this method:
766 ///
1b1a35ee 767 /// You cannot use `.assume_init_mut()` to initialize a value:
60c5eb7d
XL
768 ///
769 /// ```rust,no_run
770 /// #![feature(maybe_uninit_ref)]
771 /// use std::mem::MaybeUninit;
772 ///
773 /// let mut b = MaybeUninit::<bool>::uninit();
774 /// unsafe {
1b1a35ee 775 /// *b.assume_init_mut() = true;
60c5eb7d 776 /// // We have created a (mutable) reference to an uninitialized `bool`!
f9f354fc 777 /// // This is undefined behavior. ⚠️
60c5eb7d
XL
778 /// }
779 /// ```
780 ///
781 /// For instance, you cannot [`Read`] into an uninitialized buffer:
782 ///
783 /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
784 ///
785 /// ```rust,no_run
786 /// #![feature(maybe_uninit_ref)]
787 /// use std::{io, mem::MaybeUninit};
788 ///
789 /// fn read_chunk (reader: &'_ mut dyn io::Read) -> io::Result<[u8; 64]>
790 /// {
791 /// let mut buffer = MaybeUninit::<[u8; 64]>::uninit();
1b1a35ee
XL
792 /// reader.read_exact(unsafe { buffer.assume_init_mut() })?;
793 /// // ^^^^^^^^^^^^^^^^^^^^^^^^
60c5eb7d
XL
794 /// // (mutable) reference to uninitialized memory!
795 /// // This is undefined behavior.
796 /// Ok(unsafe { buffer.assume_init() })
797 /// }
798 /// ```
799 ///
800 /// Nor can you use direct field access to do field-by-field gradual initialization:
801 ///
802 /// ```rust,no_run
803 /// #![feature(maybe_uninit_ref)]
804 /// use std::{mem::MaybeUninit, ptr};
805 ///
806 /// struct Foo {
807 /// a: u32,
808 /// b: u8,
809 /// }
810 ///
811 /// let foo: Foo = unsafe {
812 /// let mut foo = MaybeUninit::<Foo>::uninit();
1b1a35ee
XL
813 /// ptr::write(&mut foo.assume_init_mut().a as *mut u32, 1337);
814 /// // ^^^^^^^^^^^^^^^^^^^^^
60c5eb7d
XL
815 /// // (mutable) reference to uninitialized memory!
816 /// // This is undefined behavior.
1b1a35ee
XL
817 /// ptr::write(&mut foo.assume_init_mut().b as *mut u8, 42);
818 /// // ^^^^^^^^^^^^^^^^^^^^^
60c5eb7d
XL
819 /// // (mutable) reference to uninitialized memory!
820 /// // This is undefined behavior.
821 /// foo.assume_init()
822 /// };
823 /// ```
1b1a35ee 824 // FIXME(#76092): We currently rely on the above being incorrect, i.e., we have references
dc9dc135
XL
825 // to uninitialized data (e.g., in `libcore/fmt/float.rs`). We should make
826 // a final decision about the rules before stabilization.
e1599b0c 827 #[unstable(feature = "maybe_uninit_ref", issue = "63568")]
fc512014 828 #[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
dc9dc135 829 #[inline(always)]
fc512014 830 pub const unsafe fn assume_init_mut(&mut self) -> &mut T {
f035d41b
XL
831 // SAFETY: the caller must guarantee that `self` is initialized.
832 // This also means that `self` must be a `value` variant.
833 unsafe {
834 intrinsics::assert_inhabited::<T>();
fc512014 835 &mut *self.as_mut_ptr()
f035d41b 836 }
dc9dc135
XL
837 }
838
5869c6ff
XL
839 /// Extracts the values from an array of `MaybeUninit` containers.
840 ///
841 /// # Safety
842 ///
843 /// It is up to the caller to guarantee that all elements of the array are
844 /// in an initialized state.
845 ///
846 /// # Examples
847 ///
848 /// ```
849 /// #![feature(maybe_uninit_uninit_array)]
850 /// #![feature(maybe_uninit_array_assume_init)]
851 /// use std::mem::MaybeUninit;
852 ///
853 /// let mut array: [MaybeUninit<i32>; 3] = MaybeUninit::uninit_array();
854 /// array[0] = MaybeUninit::new(0);
855 /// array[1] = MaybeUninit::new(1);
856 /// array[2] = MaybeUninit::new(2);
857 ///
858 /// // SAFETY: Now safe as we initialised all elements
859 /// let array = unsafe {
860 /// MaybeUninit::array_assume_init(array)
861 /// };
862 ///
863 /// assert_eq!(array, [0, 1, 2]);
864 /// ```
865 #[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")]
866 #[inline(always)]
867 pub unsafe fn array_assume_init<const N: usize>(array: [Self; N]) -> [T; N] {
868 // SAFETY:
869 // * The caller guarantees that all elements of the array are initialized
870 // * `MaybeUninit<T>` and T are guaranteed to have the same layout
871 // * MaybeUnint does not drop, so there are no double-frees
872 // And thus the conversion is safe
873 unsafe {
874 intrinsics::assert_inhabited::<[T; N]>();
875 (&array as *const _ as *const [T; N]).read()
876 }
877 }
878
60c5eb7d
XL
879 /// Assuming all the elements are initialized, get a slice to them.
880 ///
881 /// # Safety
882 ///
883 /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
884 /// really are in an initialized state.
885 /// Calling this when the content is not yet fully initialized causes undefined behavior.
1b1a35ee
XL
886 ///
887 /// See [`assume_init_ref`] for more details and examples.
888 ///
889 /// [`assume_init_ref`]: MaybeUninit::assume_init_ref
890 #[unstable(feature = "maybe_uninit_slice", issue = "63569")]
fc512014 891 #[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
60c5eb7d 892 #[inline(always)]
fc512014 893 pub const unsafe fn slice_assume_init_ref(slice: &[Self]) -> &[T] {
f035d41b
XL
894 // SAFETY: casting slice to a `*const [T]` is safe since the caller guarantees that
895 // `slice` is initialized, and`MaybeUninit` is guaranteed to have the same layout as `T`.
896 // The pointer obtained is valid since it refers to memory owned by `slice` which is a
897 // reference and thus guaranteed to be valid for reads.
898 unsafe { &*(slice as *const [Self] as *const [T]) }
60c5eb7d
XL
899 }
900
901 /// Assuming all the elements are initialized, get a mutable slice to them.
902 ///
903 /// # Safety
904 ///
905 /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
906 /// really are in an initialized state.
907 /// Calling this when the content is not yet fully initialized causes undefined behavior.
1b1a35ee
XL
908 ///
909 /// See [`assume_init_mut`] for more details and examples.
910 ///
911 /// [`assume_init_mut`]: MaybeUninit::assume_init_mut
912 #[unstable(feature = "maybe_uninit_slice", issue = "63569")]
fc512014 913 #[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
60c5eb7d 914 #[inline(always)]
fc512014 915 pub const unsafe fn slice_assume_init_mut(slice: &mut [Self]) -> &mut [T] {
f035d41b
XL
916 // SAFETY: similar to safety notes for `slice_get_ref`, but we have a
917 // mutable reference which is also guaranteed to be valid for writes.
918 unsafe { &mut *(slice as *mut [Self] as *mut [T]) }
60c5eb7d
XL
919 }
920
dc9dc135 921 /// Gets a pointer to the first element of the array.
e1599b0c 922 #[unstable(feature = "maybe_uninit_slice", issue = "63569")]
fc512014 923 #[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
dc9dc135 924 #[inline(always)]
fc512014 925 pub const fn slice_as_ptr(this: &[MaybeUninit<T>]) -> *const T {
29967ef6 926 this.as_ptr() as *const T
dc9dc135
XL
927 }
928
929 /// Gets a mutable pointer to the first element of the array.
e1599b0c 930 #[unstable(feature = "maybe_uninit_slice", issue = "63569")]
fc512014 931 #[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
dc9dc135 932 #[inline(always)]
fc512014 933 pub const fn slice_as_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T {
29967ef6 934 this.as_mut_ptr() as *mut T
dc9dc135 935 }
fc512014
XL
936
937 /// Copies the elements from `src` to `this`, returning a mutable reference to the now initalized contents of `this`.
938 ///
939 /// If `T` does not implement `Copy`, use [`write_slice_cloned`]
940 ///
941 /// This is similar to [`slice::copy_from_slice`].
942 ///
943 /// # Panics
944 ///
945 /// This function will panic if the two slices have different lengths.
946 ///
947 /// # Examples
948 ///
949 /// ```
950 /// #![feature(maybe_uninit_write_slice)]
951 /// use std::mem::MaybeUninit;
952 ///
953 /// let mut dst = [MaybeUninit::uninit(); 32];
954 /// let src = [0; 32];
955 ///
956 /// let init = MaybeUninit::write_slice(&mut dst, &src);
957 ///
958 /// assert_eq!(init, src);
959 /// ```
960 ///
961 /// ```
962 /// #![feature(maybe_uninit_write_slice, vec_spare_capacity)]
963 /// use std::mem::MaybeUninit;
964 ///
965 /// let mut vec = Vec::with_capacity(32);
966 /// let src = [0; 16];
967 ///
968 /// MaybeUninit::write_slice(&mut vec.spare_capacity_mut()[..src.len()], &src);
969 ///
970 /// // SAFETY: we have just copied all the elements of len into the spare capacity
971 /// // the first src.len() elements of the vec are valid now.
972 /// unsafe {
973 /// vec.set_len(src.len());
974 /// }
975 ///
976 /// assert_eq!(vec, src);
977 /// ```
978 ///
979 /// [`write_slice_cloned`]: MaybeUninit::write_slice_cloned
fc512014
XL
980 #[unstable(feature = "maybe_uninit_write_slice", issue = "79995")]
981 pub fn write_slice<'a>(this: &'a mut [MaybeUninit<T>], src: &[T]) -> &'a mut [T]
982 where
983 T: Copy,
984 {
985 // SAFETY: &[T] and &[MaybeUninit<T>] have the same layout
986 let uninit_src: &[MaybeUninit<T>] = unsafe { super::transmute(src) };
987
988 this.copy_from_slice(uninit_src);
989
990 // SAFETY: Valid elements have just been copied into `this` so it is initalized
991 unsafe { MaybeUninit::slice_assume_init_mut(this) }
992 }
993
994 /// Clones the elements from `src` to `this`, returning a mutable reference to the now initalized contents of `this`.
995 /// Any already initalized elements will not be dropped.
996 ///
997 /// If `T` implements `Copy`, use [`write_slice`]
998 ///
999 /// This is similar to [`slice::clone_from_slice`] but does not drop existing elements.
1000 ///
1001 /// # Panics
1002 ///
1003 /// This function will panic if the two slices have different lengths, or if the implementation of `Clone` panics.
1004 ///
1005 /// If there is a panic, the already cloned elements will be dropped.
1006 ///
1007 /// # Examples
1008 ///
1009 /// ```
1010 /// #![feature(maybe_uninit_write_slice)]
1011 /// use std::mem::MaybeUninit;
1012 ///
1013 /// let mut dst = [MaybeUninit::uninit(), MaybeUninit::uninit(), MaybeUninit::uninit(), MaybeUninit::uninit(), MaybeUninit::uninit()];
1014 /// let src = ["wibbly".to_string(), "wobbly".to_string(), "timey".to_string(), "wimey".to_string(), "stuff".to_string()];
1015 ///
1016 /// let init = MaybeUninit::write_slice_cloned(&mut dst, &src);
1017 ///
1018 /// assert_eq!(init, src);
1019 /// ```
1020 ///
1021 /// ```
1022 /// #![feature(maybe_uninit_write_slice, vec_spare_capacity)]
1023 /// use std::mem::MaybeUninit;
1024 ///
1025 /// let mut vec = Vec::with_capacity(32);
1026 /// let src = ["rust", "is", "a", "pretty", "cool", "language"];
1027 ///
1028 /// MaybeUninit::write_slice_cloned(&mut vec.spare_capacity_mut()[..src.len()], &src);
1029 ///
1030 /// // SAFETY: we have just cloned all the elements of len into the spare capacity
1031 /// // the first src.len() elements of the vec are valid now.
1032 /// unsafe {
1033 /// vec.set_len(src.len());
1034 /// }
1035 ///
1036 /// assert_eq!(vec, src);
1037 /// ```
1038 ///
1039 /// [`write_slice`]: MaybeUninit::write_slice
fc512014
XL
1040 #[unstable(feature = "maybe_uninit_write_slice", issue = "79995")]
1041 pub fn write_slice_cloned<'a>(this: &'a mut [MaybeUninit<T>], src: &[T]) -> &'a mut [T]
1042 where
1043 T: Clone,
1044 {
1045 // unlike copy_from_slice this does not call clone_from_slice on the slice
1046 // this is because `MaybeUninit<T: Clone>` does not implement Clone.
1047
1048 struct Guard<'a, T> {
1049 slice: &'a mut [MaybeUninit<T>],
1050 initialized: usize,
1051 }
1052
1053 impl<'a, T> Drop for Guard<'a, T> {
1054 fn drop(&mut self) {
1055 let initialized_part = &mut self.slice[..self.initialized];
1056 // SAFETY: this raw slice will contain only initialized objects
1057 // that's why, it is allowed to drop it.
1058 unsafe {
1059 crate::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(initialized_part));
1060 }
1061 }
1062 }
1063
1064 assert_eq!(this.len(), src.len(), "destination and source slices have different lengths");
1065 // NOTE: We need to explicitly slice them to the same length
1066 // for bounds checking to be elided, and the optimizer will
1067 // generate memcpy for simple cases (for example T = u8).
1068 let len = this.len();
1069 let src = &src[..len];
1070
1071 // guard is needed b/c panic might happen during a clone
1072 let mut guard = Guard { slice: this, initialized: 0 };
1073
1074 for i in 0..len {
1075 guard.slice[i].write(src[i].clone());
1076 guard.initialized += 1;
1077 }
1078
1079 super::forget(guard);
1080
1081 // SAFETY: Valid elements have just been written into `this` so it is initalized
1082 unsafe { MaybeUninit::slice_assume_init_mut(this) }
1083 }
dc9dc135 1084}