@@ -157,7 +157,7 @@ use core::hash::{Hash, Hasher};
157157#[ cfg( not( no_global_oom_handling) ) ]
158158use core:: iter:: FromIterator ;
159159use core:: iter:: { FusedIterator , Iterator } ;
160- use core:: marker:: { Unpin , Unsize } ;
160+ use core:: marker:: { Destruct , Unpin , Unsize } ;
161161use core:: mem;
162162use core:: ops:: {
163163 CoerceUnsized , Deref , DerefMut , DispatchFromDyn , Generator , GeneratorState , Receiver ,
@@ -373,11 +373,12 @@ impl<T, A: Allocator> Box<T, A> {
373373 /// ```
374374 #[ cfg( not( no_global_oom_handling) ) ]
375375 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
376+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
376377 #[ must_use]
377378 #[ inline]
378- pub fn new_in ( x : T , alloc : A ) -> Self
379+ pub const fn new_in ( x : T , alloc : A ) -> Self
379380 where
380- A : Allocator ,
381+ A : ~ const Allocator + ~ const Destruct ,
381382 {
382383 let mut boxed = Self :: new_uninit_in ( alloc) ;
383384 unsafe {
@@ -402,10 +403,12 @@ impl<T, A: Allocator> Box<T, A> {
402403 /// # Ok::<(), std::alloc::AllocError>(())
403404 /// ```
404405 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
406+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
405407 #[ inline]
406- pub fn try_new_in ( x : T , alloc : A ) -> Result < Self , AllocError >
408+ pub const fn try_new_in ( x : T , alloc : A ) -> Result < Self , AllocError >
407409 where
408- A : Allocator ,
410+ T : ~const Destruct ,
411+ A : ~const Allocator + ~const Destruct ,
409412 {
410413 let mut boxed = Self :: try_new_uninit_in ( alloc) ?;
411414 unsafe {
@@ -435,12 +438,13 @@ impl<T, A: Allocator> Box<T, A> {
435438 /// assert_eq!(*five, 5)
436439 /// ```
437440 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
441+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
438442 #[ cfg( not( no_global_oom_handling) ) ]
439443 #[ must_use]
440444 // #[unstable(feature = "new_uninit", issue = "63291")]
441- pub fn new_uninit_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A >
445+ pub const fn new_uninit_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A >
442446 where
443- A : Allocator ,
447+ A : ~ const Allocator + ~ const Destruct ,
444448 {
445449 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
446450 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -475,9 +479,10 @@ impl<T, A: Allocator> Box<T, A> {
475479 /// ```
476480 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
477481 // #[unstable(feature = "new_uninit", issue = "63291")]
478- pub fn try_new_uninit_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError >
482+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
483+ pub const fn try_new_uninit_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError >
479484 where
480- A : Allocator ,
485+ A : ~ const Allocator + ~ const Destruct ,
481486 {
482487 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
483488 let ptr = alloc. allocate ( layout) ?. cast ( ) ;
@@ -505,12 +510,13 @@ impl<T, A: Allocator> Box<T, A> {
505510 ///
506511 /// [zeroed]: mem::MaybeUninit::zeroed
507512 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
513+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
508514 #[ cfg( not( no_global_oom_handling) ) ]
509515 // #[unstable(feature = "new_uninit", issue = "63291")]
510516 #[ must_use]
511- pub fn new_zeroed_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A >
517+ pub const fn new_zeroed_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A >
512518 where
513- A : Allocator ,
519+ A : ~ const Allocator + ~ const Destruct ,
514520 {
515521 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
516522 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -545,9 +551,10 @@ impl<T, A: Allocator> Box<T, A> {
545551 /// [zeroed]: mem::MaybeUninit::zeroed
546552 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
547553 // #[unstable(feature = "new_uninit", issue = "63291")]
548- pub fn try_new_zeroed_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError >
554+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
555+ pub const fn try_new_zeroed_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError >
549556 where
550- A : Allocator ,
557+ A : ~ const Allocator + ~ const Destruct ,
551558 {
552559 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
553560 let ptr = alloc. allocate_zeroed ( layout) ?. cast ( ) ;
@@ -563,11 +570,12 @@ impl<T, A: Allocator> Box<T, A> {
563570 /// construct a (pinned) `Box` in a different way than with [`Box::new_in`].
564571 #[ cfg( not( no_global_oom_handling) ) ]
565572 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
573+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
566574 #[ must_use]
567575 #[ inline( always) ]
568- pub fn pin_in ( x : T , alloc : A ) -> Pin < Self >
576+ pub const fn pin_in ( x : T , alloc : A ) -> Pin < Self >
569577 where
570- A : ' static + Allocator ,
578+ A : ' static + ~ const Allocator + ~ const Destruct ,
571579 {
572580 Self :: into_pin ( Self :: new_in ( x, alloc) )
573581 }
@@ -576,7 +584,8 @@ impl<T, A: Allocator> Box<T, A> {
576584 ///
577585 /// This conversion does not allocate on the heap and happens in place.
578586 #[ unstable( feature = "box_into_boxed_slice" , issue = "71582" ) ]
579- pub fn into_boxed_slice ( boxed : Self ) -> Box < [ T ] , A > {
587+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
588+ pub const fn into_boxed_slice ( boxed : Self ) -> Box < [ T ] , A > {
580589 let ( raw, alloc) = Box :: into_raw_with_allocator ( boxed) ;
581590 unsafe { Box :: from_raw_in ( raw as * mut [ T ; 1 ] , alloc) }
582591 }
@@ -593,8 +602,12 @@ impl<T, A: Allocator> Box<T, A> {
593602 /// assert_eq!(Box::into_inner(c), 5);
594603 /// ```
595604 #[ unstable( feature = "box_into_inner" , issue = "80437" ) ]
605+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
596606 #[ inline]
597- pub fn into_inner ( boxed : Self ) -> T {
607+ pub const fn into_inner ( boxed : Self ) -> T
608+ where
609+ Self : ~const Destruct ,
610+ {
598611 * boxed
599612 }
600613}
@@ -808,8 +821,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
808821 /// assert_eq!(*five, 5)
809822 /// ```
810823 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
824+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
811825 #[ inline]
812- pub unsafe fn assume_init ( self ) -> Box < T , A > {
826+ pub const unsafe fn assume_init ( self ) -> Box < T , A > {
813827 let ( raw, alloc) = Box :: into_raw_with_allocator ( self ) ;
814828 unsafe { Box :: from_raw_in ( raw as * mut T , alloc) }
815829 }
@@ -842,8 +856,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
842856 /// }
843857 /// ```
844858 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
859+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
845860 #[ inline]
846- pub fn write ( mut boxed : Self , value : T ) -> Box < T , A > {
861+ pub const fn write ( mut boxed : Self , value : T ) -> Box < T , A > {
847862 unsafe {
848863 ( * boxed) . write ( value) ;
849864 boxed. assume_init ( )
@@ -1086,8 +1101,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10861101 ///
10871102 /// [memory layout]: self#memory-layout
10881103 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1104+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
10891105 #[ inline]
1090- pub fn into_raw_with_allocator ( b : Self ) -> ( * mut T , A ) {
1106+ pub const fn into_raw_with_allocator ( b : Self ) -> ( * mut T , A ) {
10911107 let ( leaked, alloc) = Box :: into_unique ( b) ;
10921108 ( leaked. as_ptr ( ) , alloc)
10931109 }
@@ -1097,9 +1113,10 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10971113 issue = "none" ,
10981114 reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
10991115 ) ]
1116+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
11001117 #[ inline]
11011118 #[ doc( hidden) ]
1102- pub fn into_unique ( b : Self ) -> ( Unique < T > , A ) {
1119+ pub const fn into_unique ( b : Self ) -> ( Unique < T > , A ) {
11031120 // Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
11041121 // raw pointer for the type system. Turning it directly into a raw pointer would not be
11051122 // recognized as "releasing" the unique pointer to permit aliased raw accesses,
@@ -1157,8 +1174,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11571174 /// assert_eq!(*static_ref, [4, 2, 3]);
11581175 /// ```
11591176 #[ stable( feature = "box_leak" , since = "1.26.0" ) ]
1177+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
11601178 #[ inline]
1161- pub fn leak < ' a > ( b : Self ) -> & ' a mut T
1179+ pub const fn leak < ' a > ( b : Self ) -> & ' a mut T
11621180 where
11631181 A : ' a ,
11641182 {
@@ -1228,7 +1246,7 @@ impl<T: Default> Default for Box<T> {
12281246#[ cfg( not( no_global_oom_handling) ) ]
12291247#[ stable( feature = "rust1" , since = "1.0.0" ) ]
12301248#[ rustc_const_unstable( feature = "const_default_impls" , issue = "87864" ) ]
1231- impl < T > Default for Box < [ T ] > {
1249+ impl < T > const Default for Box < [ T ] > {
12321250 fn default ( ) -> Self {
12331251 let ptr: Unique < [ T ] > = Unique :: < [ T ; 0 ] > :: dangling ( ) ;
12341252 Box ( ptr, Global )
@@ -1238,7 +1256,7 @@ impl<T> Default for Box<[T]> {
12381256#[ cfg( not( no_global_oom_handling) ) ]
12391257#[ stable( feature = "default_box_extra" , since = "1.17.0" ) ]
12401258#[ rustc_const_unstable( feature = "const_default_impls" , issue = "87864" ) ]
1241- impl Default for Box < str > {
1259+ impl const Default for Box < str > {
12421260 fn default ( ) -> Self {
12431261 // SAFETY: This is the same as `Unique::cast<U>` but with an unsized `U = str`.
12441262 let ptr: Unique < str > = unsafe {
@@ -1434,7 +1452,8 @@ impl<T> From<T> for Box<T> {
14341452}
14351453
14361454#[ stable( feature = "pin" , since = "1.33.0" ) ]
1437- impl < T : ?Sized , A : Allocator > From < Box < T , A > > for Pin < Box < T , A > >
1455+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1456+ impl < T : ?Sized , A : Allocator > const From < Box < T , A > > for Pin < Box < T , A > >
14381457where
14391458 A : ' static ,
14401459{
@@ -1822,7 +1841,8 @@ impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
18221841}
18231842
18241843#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1825- impl < T : ?Sized , A : Allocator > Deref for Box < T , A > {
1844+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1845+ impl < T : ?Sized , A : Allocator > const Deref for Box < T , A > {
18261846 type Target = T ;
18271847
18281848 fn deref ( & self ) -> & T {
@@ -1831,7 +1851,8 @@ impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
18311851}
18321852
18331853#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1834- impl < T : ?Sized , A : Allocator > DerefMut for Box < T , A > {
1854+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1855+ impl < T : ?Sized , A : Allocator > const DerefMut for Box < T , A > {
18351856 fn deref_mut ( & mut self ) -> & mut T {
18361857 & mut * * self
18371858 }
@@ -2010,7 +2031,8 @@ impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
20102031 * could have a method to project a Pin<T> from it.
20112032 */
20122033#[ stable( feature = "pin" , since = "1.33.0" ) ]
2013- impl < T : ?Sized , A : Allocator > Unpin for Box < T , A > where A : ' static { }
2034+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
2035+ impl < T : ?Sized , A : Allocator > const Unpin for Box < T , A > where A : ' static { }
20142036
20152037#[ unstable( feature = "generator_trait" , issue = "43122" ) ]
20162038impl < G : ?Sized + Generator < R > + Unpin , R , A : Allocator > Generator < R > for Box < G , A >
0 commit comments