about summary refs log tree commit diff
path: root/library/alloc/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-12-03 07:12:36 +0000
committerbors <bors@rust-lang.org>2021-12-03 07:12:36 +0000
commit3e21768a0a3fc84befd1cbe825ae6849e9941b73 (patch)
tree27a98b7f0c0149f13f782285532de6a3137f0997 /library/alloc/src
parent190367ba2ef1d279004b8372cf022b5fc96145dd (diff)
parenta2160609c0016c1d65304905043bfea4b7f9ca52 (diff)
Auto merge of #91486 - matthiaskrgr:rollup-699fo18, r=matthiaskrgr
Rollup of 10 pull requests

Successful merges:

 - #88906 (Implement write() method for Box<MaybeUninit<T>>)
 - #90269 (Make `Option::expect` unstably const)
 - #90854 (Type can be unsized and uninhabited)
 - #91170 (rustdoc: preload fonts)
 - #91273 (Fix ICE #91268 by checking that the snippet ends with a `)`)
 - #91381 (Android: -ldl must appear after -lgcc when linking)
 - #91453 (Document Windows TLS drop behaviour)
 - #91462 (Use try_normalize_erasing_regions in needs_drop)
 - #91474 (suppress warning about set_errno being unused on DragonFly)
 - #91483 (Sync rustfmt subtree)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/alloc/src')
-rw-r--r--library/alloc/src/boxed.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs
index f6332b072cf..0b72b3f0ee7 100644
--- a/library/alloc/src/boxed.rs
+++ b/library/alloc/src/boxed.rs
@@ -763,6 +763,42 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
         let (raw, alloc) = Box::into_raw_with_allocator(self);
         unsafe { Box::from_raw_in(raw as *mut T, alloc) }
     }
+
+    /// Writes the value and converts to `Box<T, A>`.
+    ///
+    /// This method converts the box similarly to [`Box::assume_init`] but
+    /// writes `value` into it before conversion thus guaranteeing safety.
+    /// In some scenarios use of this method may improve performance because
+    /// the compiler may be able to optimize copying from stack.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(new_uninit)]
+    ///
+    /// let big_box = Box::<[usize; 1024]>::new_uninit();
+    ///
+    /// let mut array = [0; 1024];
+    /// for (i, place) in array.iter_mut().enumerate() {
+    ///     *place = i;
+    /// }
+    ///
+    /// // The optimizer may be able to elide this copy, so previous code writes
+    /// // to heap directly.
+    /// let big_box = Box::write(big_box, array);
+    ///
+    /// for (i, x) in big_box.iter().enumerate() {
+    ///     assert_eq!(*x, i);
+    /// }
+    /// ```
+    #[unstable(feature = "new_uninit", issue = "63291")]
+    #[inline]
+    pub fn write(mut boxed: Self, value: T) -> Box<T, A> {
+        unsafe {
+            (*boxed).write(value);
+            boxed.assume_init()
+        }
+    }
 }
 
 impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> {