@@ -290,8 +290,6 @@ impl<T> Box<T> {
290290 /// # Examples
291291 ///
292292 /// ```
293- /// #![feature(new_zeroed_alloc)]
294- ///
295293 /// let zero = Box::<u32>::new_zeroed();
296294 /// let zero = unsafe { zero.assume_init() };
297295 ///
@@ -301,7 +299,7 @@ impl<T> Box<T> {
301299 /// [zeroed]: mem::MaybeUninit::zeroed
302300 #[ cfg( not( no_global_oom_handling) ) ]
303301 #[ inline]
304- #[ unstable ( feature = "new_zeroed_alloc" , issue = "129396 " ) ]
302+ #[ stable ( feature = "new_zeroed_alloc" , since = "CURRENT_RUSTC_VERSION " ) ]
305303 #[ must_use]
306304 pub fn new_zeroed ( ) -> Box < mem:: MaybeUninit < T > > {
307305 Self :: new_zeroed_in ( Global )
@@ -358,7 +356,6 @@ impl<T> Box<T> {
358356 /// # Ok::<(), std::alloc::AllocError>(())
359357 /// ```
360358 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
361- // #[unstable(feature = "new_uninit", issue = "63291")]
362359 #[ inline]
363360 pub fn try_new_uninit ( ) -> Result < Box < mem:: MaybeUninit < T > > , AllocError > {
364361 Box :: try_new_uninit_in ( Global )
@@ -384,7 +381,6 @@ impl<T> Box<T> {
384381 ///
385382 /// [zeroed]: mem::MaybeUninit::zeroed
386383 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
387- // #[unstable(feature = "new_uninit", issue = "63291")]
388384 #[ inline]
389385 pub fn try_new_zeroed ( ) -> Result < Box < mem:: MaybeUninit < T > > , AllocError > {
390386 Box :: try_new_zeroed_in ( Global )
@@ -463,7 +459,6 @@ impl<T, A: Allocator> Box<T, A> {
463459 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
464460 #[ cfg( not( no_global_oom_handling) ) ]
465461 #[ must_use]
466- // #[unstable(feature = "new_uninit", issue = "63291")]
467462 pub fn new_uninit_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A >
468463 where
469464 A : Allocator ,
@@ -496,7 +491,6 @@ impl<T, A: Allocator> Box<T, A> {
496491 /// # Ok::<(), std::alloc::AllocError>(())
497492 /// ```
498493 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
499- // #[unstable(feature = "new_uninit", issue = "63291")]
500494 pub fn try_new_uninit_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError >
501495 where
502496 A : Allocator ,
@@ -532,7 +526,6 @@ impl<T, A: Allocator> Box<T, A> {
532526 /// [zeroed]: mem::MaybeUninit::zeroed
533527 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
534528 #[ cfg( not( no_global_oom_handling) ) ]
535- // #[unstable(feature = "new_uninit", issue = "63291")]
536529 #[ must_use]
537530 pub fn new_zeroed_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A >
538531 where
@@ -570,7 +563,6 @@ impl<T, A: Allocator> Box<T, A> {
570563 ///
571564 /// [zeroed]: mem::MaybeUninit::zeroed
572565 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
573- // #[unstable(feature = "new_uninit", issue = "63291")]
574566 pub fn try_new_zeroed_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError >
575567 where
576568 A : Allocator ,
@@ -627,6 +619,37 @@ impl<T, A: Allocator> Box<T, A> {
627619 pub fn into_inner ( boxed : Self ) -> T {
628620 * boxed
629621 }
622+
623+ /// Consumes the `Box` without consuming its allocation, returning the wrapped value and a `Box`
624+ /// to the uninitialized memory where the wrapped value used to live.
625+ ///
626+ /// This can be used together with [`write`](Box::write) to reuse the allocation for multiple
627+ /// boxed values.
628+ ///
629+ /// # Examples
630+ ///
631+ /// ```
632+ /// #![feature(box_take)]
633+ ///
634+ /// let c = Box::new(5);
635+ ///
636+ /// // take the value out of the box
637+ /// let (value, uninit) = Box::take(c);
638+ /// assert_eq!(value, 5);
639+ ///
640+ /// // reuse the box for a second value
641+ /// let c = Box::write(uninit, 6);
642+ /// assert_eq!(*c, 6);
643+ /// ```
644+ #[ unstable( feature = "box_take" , issue = "147212" ) ]
645+ pub fn take ( boxed : Self ) -> ( T , Box < mem:: MaybeUninit < T > , A > ) {
646+ unsafe {
647+ let ( raw, alloc) = Box :: into_raw_with_allocator ( boxed) ;
648+ let value = raw. read ( ) ;
649+ let uninit = Box :: from_raw_in ( raw. cast :: < mem:: MaybeUninit < T > > ( ) , alloc) ;
650+ ( value, uninit)
651+ }
652+ }
630653}
631654
632655impl < T > Box < [ T ] > {
@@ -640,7 +663,7 @@ impl<T> Box<[T]> {
640663 /// values[0].write(1);
641664 /// values[1].write(2);
642665 /// values[2].write(3);
643- /// let values = unsafe {values.assume_init() };
666+ /// let values = unsafe { values.assume_init() };
644667 ///
645668 /// assert_eq!(*values, [1, 2, 3])
646669 /// ```
@@ -660,8 +683,6 @@ impl<T> Box<[T]> {
660683 /// # Examples
661684 ///
662685 /// ```
663- /// #![feature(new_zeroed_alloc)]
664- ///
665686 /// let values = Box::<[u32]>::new_zeroed_slice(3);
666687 /// let values = unsafe { values.assume_init() };
667688 ///
@@ -670,7 +691,7 @@ impl<T> Box<[T]> {
670691 ///
671692 /// [zeroed]: mem::MaybeUninit::zeroed
672693 #[ cfg( not( no_global_oom_handling) ) ]
673- #[ unstable ( feature = "new_zeroed_alloc" , issue = "129396 " ) ]
694+ #[ stable ( feature = "new_zeroed_alloc" , since = "CURRENT_RUSTC_VERSION " ) ]
674695 #[ must_use]
675696 pub fn new_zeroed_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
676697 unsafe { RawVec :: with_capacity_zeroed ( len) . into_box ( len) }
@@ -785,7 +806,6 @@ impl<T, A: Allocator> Box<[T], A> {
785806 /// ```
786807 #[ cfg( not( no_global_oom_handling) ) ]
787808 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
788- // #[unstable(feature = "new_uninit", issue = "63291")]
789809 #[ must_use]
790810 pub fn new_uninit_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
791811 unsafe { RawVec :: with_capacity_in ( len, alloc) . into_box ( len) }
@@ -813,7 +833,6 @@ impl<T, A: Allocator> Box<[T], A> {
813833 /// [zeroed]: mem::MaybeUninit::zeroed
814834 #[ cfg( not( no_global_oom_handling) ) ]
815835 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
816- // #[unstable(feature = "new_uninit", issue = "63291")]
817836 #[ must_use]
818837 pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
819838 unsafe { RawVec :: with_capacity_zeroed_in ( len, alloc) . into_box ( len) }
@@ -1718,7 +1737,7 @@ impl Default for Box<str> {
17181737}
17191738
17201739#[ cfg( not( no_global_oom_handling) ) ]
1721- #[ stable( feature = "pin_default_impls" , since = "CURRENT_RUSTC_VERSION " ) ]
1740+ #[ stable( feature = "pin_default_impls" , since = "1.91.0 " ) ]
17221741impl < T > Default for Pin < Box < T > >
17231742where
17241743 T : ?Sized ,
0 commit comments