@@ -346,9 +346,13 @@ impl<T, A: Allocator> Box<T, A> {
346346 /// ```
347347 #[ cfg( not( no_global_oom_handling) ) ]
348348 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
349+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
349350 #[ must_use]
350351 #[ inline]
351- pub fn new_in ( x : T , alloc : A ) -> Self {
352+ pub const fn new_in ( x : T , alloc : A ) -> Self
353+ where
354+ A : ~const Allocator + ~const Drop ,
355+ {
352356 let mut boxed = Self :: new_uninit_in ( alloc) ;
353357 unsafe {
354358 boxed. as_mut_ptr ( ) . write ( x) ;
@@ -372,8 +376,13 @@ impl<T, A: Allocator> Box<T, A> {
372376 /// # Ok::<(), std::alloc::AllocError>(())
373377 /// ```
374378 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
379+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
375380 #[ inline]
376- pub fn try_new_in ( x : T , alloc : A ) -> Result < Self , AllocError > {
381+ pub const fn try_new_in ( x : T , alloc : A ) -> Result < Self , AllocError >
382+ where
383+ T : ~const Drop ,
384+ A : ~const Allocator + ~const Drop ,
385+ {
377386 let mut boxed = Self :: try_new_uninit_in ( alloc) ?;
378387 unsafe {
379388 boxed. as_mut_ptr ( ) . write ( x) ;
@@ -402,10 +411,14 @@ impl<T, A: Allocator> Box<T, A> {
402411 /// assert_eq!(*five, 5)
403412 /// ```
404413 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
414+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
405415 #[ cfg( not( no_global_oom_handling) ) ]
406416 #[ must_use]
407417 // #[unstable(feature = "new_uninit", issue = "63291")]
408- pub fn new_uninit_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A > {
418+ pub const fn new_uninit_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A >
419+ where
420+ A : ~const Allocator + ~const Drop ,
421+ {
409422 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
410423 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
411424 // That would make code size bigger.
@@ -439,7 +452,11 @@ impl<T, A: Allocator> Box<T, A> {
439452 /// ```
440453 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
441454 // #[unstable(feature = "new_uninit", issue = "63291")]
442- pub fn try_new_uninit_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError > {
455+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
456+ pub const fn try_new_uninit_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError >
457+ where
458+ A : ~const Allocator + ~const Drop ,
459+ {
443460 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
444461 let ptr = alloc. allocate ( layout) ?. cast ( ) ;
445462 unsafe { Ok ( Box :: from_raw_in ( ptr. as_ptr ( ) , alloc) ) }
@@ -466,10 +483,14 @@ impl<T, A: Allocator> Box<T, A> {
466483 ///
467484 /// [zeroed]: mem::MaybeUninit::zeroed
468485 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
486+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
469487 #[ cfg( not( no_global_oom_handling) ) ]
470488 // #[unstable(feature = "new_uninit", issue = "63291")]
471489 #[ must_use]
472- pub fn new_zeroed_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A > {
490+ pub const fn new_zeroed_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A >
491+ where
492+ A : ~const Allocator + ~const Drop ,
493+ {
473494 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
474495 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
475496 // That would make code size bigger.
@@ -503,7 +524,11 @@ impl<T, A: Allocator> Box<T, A> {
503524 /// [zeroed]: mem::MaybeUninit::zeroed
504525 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
505526 // #[unstable(feature = "new_uninit", issue = "63291")]
506- pub fn try_new_zeroed_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError > {
527+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
528+ pub const fn try_new_zeroed_in ( alloc : A ) -> Result < Box < mem:: MaybeUninit < T > , A > , AllocError >
529+ where
530+ A : ~const Allocator + ~const Drop ,
531+ {
507532 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
508533 let ptr = alloc. allocate_zeroed ( layout) ?. cast ( ) ;
509534 unsafe { Ok ( Box :: from_raw_in ( ptr. as_ptr ( ) , alloc) ) }
@@ -513,20 +538,22 @@ impl<T, A: Allocator> Box<T, A> {
513538 /// `x` will be pinned in memory and unable to be moved.
514539 #[ cfg( not( no_global_oom_handling) ) ]
515540 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
541+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
516542 #[ must_use]
517543 #[ inline( always) ]
518- pub fn pin_in ( x : T , alloc : A ) -> Pin < Self >
544+ pub const fn pin_in ( x : T , alloc : A ) -> Pin < Self >
519545 where
520- A : ' static ,
546+ A : ' static + ~ const Allocator + ~ const Drop ,
521547 {
522- Self :: new_in ( x, alloc) . into ( )
548+ Self :: into_pin ( Self :: new_in ( x, alloc) )
523549 }
524550
525551 /// Converts a `Box<T>` into a `Box<[T]>`
526552 ///
527553 /// This conversion does not allocate on the heap and happens in place.
528554 #[ unstable( feature = "box_into_boxed_slice" , issue = "71582" ) ]
529- pub fn into_boxed_slice ( boxed : Self ) -> Box < [ T ] , A > {
555+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
556+ pub const fn into_boxed_slice ( boxed : Self ) -> Box < [ T ] , A > {
530557 let ( raw, alloc) = Box :: into_raw_with_allocator ( boxed) ;
531558 unsafe { Box :: from_raw_in ( raw as * mut [ T ; 1 ] , alloc) }
532559 }
@@ -543,8 +570,12 @@ impl<T, A: Allocator> Box<T, A> {
543570 /// assert_eq!(Box::into_inner(c), 5);
544571 /// ```
545572 #[ unstable( feature = "box_into_inner" , issue = "80437" ) ]
573+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
546574 #[ inline]
547- pub fn into_inner ( boxed : Self ) -> T {
575+ pub const fn into_inner ( boxed : Self ) -> T
576+ where
577+ Self : ~const Drop ,
578+ {
548579 * boxed
549580 }
550581}
@@ -758,8 +789,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
758789 /// assert_eq!(*five, 5)
759790 /// ```
760791 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
792+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
761793 #[ inline]
762- pub unsafe fn assume_init ( self ) -> Box < T , A > {
794+ pub const unsafe fn assume_init ( self ) -> Box < T , A > {
763795 let ( raw, alloc) = Box :: into_raw_with_allocator ( self ) ;
764796 unsafe { Box :: from_raw_in ( raw as * mut T , alloc) }
765797 }
@@ -792,8 +824,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
792824 /// }
793825 /// ```
794826 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
827+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
795828 #[ inline]
796- pub fn write ( mut boxed : Self , value : T ) -> Box < T , A > {
829+ pub const fn write ( mut boxed : Self , value : T ) -> Box < T , A > {
797830 unsafe {
798831 ( * boxed) . write ( value) ;
799832 boxed. assume_init ( )
@@ -938,8 +971,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
938971 /// [memory layout]: self#memory-layout
939972 /// [`Layout`]: crate::Layout
940973 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
974+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
941975 #[ inline]
942- pub unsafe fn from_raw_in ( raw : * mut T , alloc : A ) -> Self {
976+ pub const unsafe fn from_raw_in ( raw : * mut T , alloc : A ) -> Self {
943977 Box ( unsafe { Unique :: new_unchecked ( raw) } , alloc)
944978 }
945979
@@ -1035,8 +1069,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10351069 ///
10361070 /// [memory layout]: self#memory-layout
10371071 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1072+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
10381073 #[ inline]
1039- pub fn into_raw_with_allocator ( b : Self ) -> ( * mut T , A ) {
1074+ pub const fn into_raw_with_allocator ( b : Self ) -> ( * mut T , A ) {
10401075 let ( leaked, alloc) = Box :: into_unique ( b) ;
10411076 ( leaked. as_ptr ( ) , alloc)
10421077 }
@@ -1046,9 +1081,10 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10461081 issue = "none" ,
10471082 reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
10481083 ) ]
1084+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
10491085 #[ inline]
10501086 #[ doc( hidden) ]
1051- pub fn into_unique ( b : Self ) -> ( Unique < T > , A ) {
1087+ pub const fn into_unique ( b : Self ) -> ( Unique < T > , A ) {
10521088 // Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
10531089 // raw pointer for the type system. Turning it directly into a raw pointer would not be
10541090 // recognized as "releasing" the unique pointer to permit aliased raw accesses,
@@ -1064,8 +1100,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10641100 /// to call it as `Box::allocator(&b)` instead of `b.allocator()`. This
10651101 /// is so that there is no conflict with a method on the inner type.
10661102 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1103+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
10671104 #[ inline]
1068- pub fn allocator ( b : & Self ) -> & A {
1105+ pub const fn allocator ( b : & Self ) -> & A {
10691106 & b. 1
10701107 }
10711108
@@ -1105,8 +1142,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11051142 /// assert_eq!(*static_ref, [4, 2, 3]);
11061143 /// ```
11071144 #[ stable( feature = "box_leak" , since = "1.26.0" ) ]
1145+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
11081146 #[ inline]
1109- pub fn leak < ' a > ( b : Self ) -> & ' a mut T
1147+ pub const fn leak < ' a > ( b : Self ) -> & ' a mut T
11101148 where
11111149 A : ' a ,
11121150 {
@@ -1119,7 +1157,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11191157 ///
11201158 /// This is also available via [`From`].
11211159 #[ unstable( feature = "box_into_pin" , issue = "62370" ) ]
1122- pub fn into_pin ( boxed : Self ) -> Pin < Self >
1160+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1161+ pub const fn into_pin ( boxed : Self ) -> Pin < Self >
11231162 where
11241163 A : ' static ,
11251164 {
@@ -1131,7 +1170,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11311170}
11321171
11331172#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1134- unsafe impl < #[ may_dangle] T : ?Sized , A : Allocator > Drop for Box < T , A > {
1173+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1174+ unsafe impl < #[ may_dangle] T : ?Sized , A : Allocator > const Drop for Box < T , A > {
11351175 fn drop ( & mut self ) {
11361176 // FIXME: Do nothing, drop is currently performed by compiler.
11371177 }
@@ -1341,7 +1381,8 @@ impl<T> From<T> for Box<T> {
13411381}
13421382
13431383#[ stable( feature = "pin" , since = "1.33.0" ) ]
1344- impl < T : ?Sized , A : Allocator > From < Box < T , A > > for Pin < Box < T , A > >
1384+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1385+ impl < T : ?Sized , A : Allocator > const From < Box < T , A > > for Pin < Box < T , A > >
13451386where
13461387 A : ' static ,
13471388{
@@ -1720,7 +1761,8 @@ impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
17201761}
17211762
17221763#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1723- impl < T : ?Sized , A : Allocator > Deref for Box < T , A > {
1764+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1765+ impl < T : ?Sized , A : Allocator > const Deref for Box < T , A > {
17241766 type Target = T ;
17251767
17261768 fn deref ( & self ) -> & T {
@@ -1729,7 +1771,8 @@ impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
17291771}
17301772
17311773#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1732- impl < T : ?Sized , A : Allocator > DerefMut for Box < T , A > {
1774+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1775+ impl < T : ?Sized , A : Allocator > const DerefMut for Box < T , A > {
17331776 fn deref_mut ( & mut self ) -> & mut T {
17341777 & mut * * self
17351778 }
@@ -1908,7 +1951,8 @@ impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
19081951 * could have a method to project a Pin<T> from it.
19091952 */
19101953#[ stable( feature = "pin" , since = "1.33.0" ) ]
1911- impl < T : ?Sized , A : Allocator > Unpin for Box < T , A > where A : ' static { }
1954+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1955+ impl < T : ?Sized , A : Allocator > const Unpin for Box < T , A > where A : ' static { }
19121956
19131957#[ unstable( feature = "generator_trait" , issue = "43122" ) ]
19141958impl < G : ?Sized + Generator < R > + Unpin , R , A : Allocator > Generator < R > for Box < G , A >
0 commit comments