@@ -241,6 +241,77 @@ impl<T> Box<T> {
241
241
pub fn pin ( x : T ) -> Pin < Box < T > > {
242
242
( box x) . into ( )
243
243
}
244
+
245
+ /// Allocates memory on the heap then places `x` into it,
246
+ /// returning an error if the allocation fails
247
+ ///
248
+ /// This doesn't actually allocate if `T` is zero-sized.
249
+ ///
250
+ /// # Examples
251
+ ///
252
+ /// ```
253
+ /// #![feature(allocator_api)]
254
+ ///
255
+ /// let five = Box::try_new(5)?;
256
+ /// # Ok::<(), std::alloc::AllocError>(())
257
+ /// ```
258
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
259
+ #[ inline]
260
+ pub fn try_new ( x : T ) -> Result < Self , AllocError > {
261
+ Self :: try_new_in ( x, Global )
262
+ }
263
+
264
+ /// Constructs a new box with uninitialized contents on the heap,
265
+ /// returning an error if the allocation fails
266
+ ///
267
+ /// # Examples
268
+ ///
269
+ /// ```
270
+ /// #![feature(allocator_api, new_uninit)]
271
+ ///
272
+ ///
273
+ /// let mut five = Box::<u32>::try_new_uninit()?;
274
+ ///
275
+ /// let five = unsafe {
276
+ /// // Deferred initialization:
277
+ /// five.as_mut_ptr().write(5);
278
+ ///
279
+ /// five.assume_init()
280
+ /// };
281
+ ///
282
+ /// assert_eq!(*five, 5);
283
+ /// # Ok::<(), std::alloc::AllocError>(())
284
+ /// ```
285
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
286
+ // #[unstable(feature = "new_uninit", issue = "63291")]
287
+ pub fn try_new_uninit ( ) -> Result < Box < mem:: MaybeUninit < T > > , AllocError > {
288
+ Box :: try_new_uninit_in ( Global )
289
+ }
290
+
291
+ /// Constructs a new `Box` with uninitialized contents, with the memory
292
+ /// being filled with `0` bytes on the heap
293
+ ///
294
+ /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
295
+ /// of this method.
296
+ ///
297
+ /// # Examples
298
+ ///
299
+ /// ```
300
+ /// #![feature(allocator_api, new_uninit)]
301
+ ///
302
+ /// let zero = Box::<u32>::try_new_zeroed()?;
303
+ /// let zero = unsafe { zero.assume_init() };
304
+ ///
305
+ /// assert_eq!(*zero, 0);
306
+ /// # Ok::<(), std::alloc::AllocError>(())
307
+ /// ```
308
+ ///
309
+ /// [zeroed]: mem::MaybeUninit::zeroed
310
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
311
+ // #[unstable(feature = "new_uninit", issue = "63291")]
312
+ pub fn try_new_zeroed ( ) -> Result < Box < mem:: MaybeUninit < T > > , AllocError > {
313
+ Box :: try_new_zeroed_in ( Global )
314
+ }
244
315
}
245
316
246
317
impl < T , A : Allocator > Box < T , A > {
@@ -380,7 +451,8 @@ impl<T, A: Allocator> Box<T, A> {
380
451
}
381
452
382
453
/// Constructs a new `Box` with uninitialized contents, with the memory
383
- /// being filled with `0` bytes in the provided allocator.
454
+ /// being filled with `0` bytes in the provided allocator,
455
+ /// returning an error if the allocation fails,
384
456
///
385
457
/// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
386
458
/// of this method.
0 commit comments