@@ -91,8 +91,10 @@ use core::ops::{
9191 CoerceUnsized , DispatchFromDyn , Deref , DerefMut , Receiver , Generator , GeneratorState
9292} ;
9393use core:: ptr:: { self , NonNull , Unique } ;
94+ use core:: slice;
9495use core:: task:: { Context , Poll } ;
9596
97+ use crate :: alloc:: { self , Global , Alloc } ;
9698use crate :: vec:: Vec ;
9799use crate :: raw_vec:: RawVec ;
98100use crate :: str:: from_boxed_utf8_unchecked;
@@ -121,6 +123,34 @@ impl<T> Box<T> {
121123 box x
122124 }
123125
126+ /// Constructs a new box with uninitialized contents.
127+ ///
128+ /// # Examples
129+ ///
130+ /// ```
131+ /// #![feature(new_uninit)]
132+ ///
133+ /// let mut five = Box::<u32>::new_uninit();
134+ ///
135+ /// let five = unsafe {
136+ /// // Deferred initialization:
137+ /// five.as_mut_ptr().write(5);
138+ ///
139+ /// five.assume_init()
140+ /// };
141+ ///
142+ /// assert_eq!(*five, 5)
143+ /// ```
144+ #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
145+ pub fn new_uninit ( ) -> Box < mem:: MaybeUninit < T > > {
146+ let layout = alloc:: Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
147+ let ptr = unsafe {
148+ Global . alloc ( layout)
149+ . unwrap_or_else ( |_| alloc:: handle_alloc_error ( layout) )
150+ } ;
151+ Box ( ptr. cast ( ) . into ( ) )
152+ }
153+
124154 /// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
125155 /// `x` will be pinned in memory and unable to be moved.
126156 #[ stable( feature = "pin" , since = "1.33.0" ) ]
@@ -130,6 +160,111 @@ impl<T> Box<T> {
130160 }
131161}
132162
163+ impl < T > Box < [ T ] > {
164+ /// Constructs a new boxed slice with uninitialized contents.
165+ ///
166+ /// # Examples
167+ ///
168+ /// ```
169+ /// #![feature(new_uninit)]
170+ ///
171+ /// let mut values = Box::<[u32]>::new_uninit_slice(3);
172+ ///
173+ /// let values = unsafe {
174+ /// // Deferred initialization:
175+ /// values[0].as_mut_ptr().write(1);
176+ /// values[1].as_mut_ptr().write(2);
177+ /// values[2].as_mut_ptr().write(3);
178+ ///
179+ /// values.assume_init()
180+ /// };
181+ ///
182+ /// assert_eq!(*values, [1, 2, 3])
183+ /// ```
184+ #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
185+ pub fn new_uninit_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
186+ let layout = alloc:: Layout :: array :: < mem:: MaybeUninit < T > > ( len) . unwrap ( ) ;
187+ let ptr = unsafe { alloc:: alloc ( layout) } ;
188+ let unique = Unique :: new ( ptr) . unwrap_or_else ( || alloc:: handle_alloc_error ( layout) ) ;
189+ let slice = unsafe { slice:: from_raw_parts_mut ( unique. cast ( ) . as_ptr ( ) , len) } ;
190+ Box ( Unique :: from ( slice) )
191+ }
192+ }
193+
194+ impl < T > Box < mem:: MaybeUninit < T > > {
195+ /// Converts to `Box<T>`.
196+ ///
197+ /// # Safety
198+ ///
199+ /// As with [`MaybeUninit::assume_init`],
200+ /// it is up to the caller to guarantee that the value
201+ /// really is in an initialized state.
202+ /// Calling this when the content is not yet fully initialized
203+ /// causes immediate undefined behavior.
204+ ///
205+ /// [`MaybeUninit::assume_init`]: ../../std/mem/union.MaybeUninit.html#method.assume_init
206+ ///
207+ /// # Examples
208+ ///
209+ /// ```
210+ /// #![feature(new_uninit)]
211+ ///
212+ /// let mut five = Box::<u32>::new_uninit();
213+ ///
214+ /// let five: Box<u32> = unsafe {
215+ /// // Deferred initialization:
216+ /// five.as_mut_ptr().write(5);
217+ ///
218+ /// five.assume_init()
219+ /// };
220+ ///
221+ /// assert_eq!(*five, 5)
222+ /// ```
223+ #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
224+ #[ inline]
225+ pub unsafe fn assume_init ( self ) -> Box < T > {
226+ Box ( Box :: into_unique ( self ) . cast ( ) )
227+ }
228+ }
229+
230+ impl < T > Box < [ mem:: MaybeUninit < T > ] > {
231+ /// Converts to `Box<[T]>`.
232+ ///
233+ /// # Safety
234+ ///
235+ /// As with [`MaybeUninit::assume_init`],
236+ /// it is up to the caller to guarantee that the values
237+ /// really are in an initialized state.
238+ /// Calling this when the content is not yet fully initialized
239+ /// causes immediate undefined behavior.
240+ ///
241+ /// [`MaybeUninit::assume_init`]: ../../std/mem/union.MaybeUninit.html#method.assume_init
242+ ///
243+ /// # Examples
244+ ///
245+ /// ```
246+ /// #![feature(new_uninit)]
247+ ///
248+ /// let mut values = Box::<[u32]>::new_uninit_slice(3);
249+ ///
250+ /// let values = unsafe {
251+ /// // Deferred initialization:
252+ /// values[0].as_mut_ptr().write(1);
253+ /// values[1].as_mut_ptr().write(2);
254+ /// values[2].as_mut_ptr().write(3);
255+ ///
256+ /// values.assume_init()
257+ /// };
258+ ///
259+ /// assert_eq!(*values, [1, 2, 3])
260+ /// ```
261+ #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
262+ #[ inline]
263+ pub unsafe fn assume_init ( self ) -> Box < [ T ] > {
264+ Box ( Unique :: new_unchecked ( Box :: into_raw ( self ) as _ ) )
265+ }
266+ }
267+
133268impl < T : ?Sized > Box < T > {
134269 /// Constructs a box from a raw pointer.
135270 ///
0 commit comments