@@ -157,7 +157,7 @@ use core::hash::{Hash, Hasher};
157
157
#[ cfg( not( no_global_oom_handling) ) ]
158
158
use core:: iter:: FromIterator ;
159
159
use core:: iter:: { FusedIterator , Iterator } ;
160
- use core:: marker:: { Unpin , Unsize } ;
160
+ use core:: marker:: { Destruct , Unpin , Unsize } ;
161
161
use core:: mem;
162
162
use core:: ops:: {
163
163
CoerceUnsized , Deref , DerefMut , DispatchFromDyn , Generator , GeneratorState , Receiver ,
@@ -373,11 +373,12 @@ impl<T, A: Allocator> Box<T, A> {
373
373
/// ```
374
374
#[ cfg( not( no_global_oom_handling) ) ]
375
375
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
376
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
376
377
#[ must_use]
377
378
#[ inline]
378
- pub fn new_in ( x : T , alloc : A ) -> Self
379
+ pub const fn new_in ( x : T , alloc : A ) -> Self
379
380
where
380
- A : Allocator ,
381
+ A : ~ const Allocator + ~ const Destruct ,
381
382
{
382
383
let mut boxed = Self :: new_uninit_in ( alloc) ;
383
384
unsafe {
@@ -402,10 +403,12 @@ impl<T, A: Allocator> Box<T, A> {
402
403
/// # Ok::<(), std::alloc::AllocError>(())
403
404
/// ```
404
405
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
406
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
405
407
#[ 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 >
407
409
where
408
- A : Allocator ,
410
+ T : ~const Destruct ,
411
+ A : ~const Allocator + ~const Destruct ,
409
412
{
410
413
let mut boxed = Self :: try_new_uninit_in ( alloc) ?;
411
414
unsafe {
@@ -435,12 +438,13 @@ impl<T, A: Allocator> Box<T, A> {
435
438
/// assert_eq!(*five, 5)
436
439
/// ```
437
440
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
441
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
438
442
#[ cfg( not( no_global_oom_handling) ) ]
439
443
#[ must_use]
440
444
// #[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 >
442
446
where
443
- A : Allocator ,
447
+ A : ~ const Allocator + ~ const Destruct ,
444
448
{
445
449
let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
446
450
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -475,9 +479,10 @@ impl<T, A: Allocator> Box<T, A> {
475
479
/// ```
476
480
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
477
481
// #[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 >
479
484
where
480
- A : Allocator ,
485
+ A : ~ const Allocator + ~ const Destruct ,
481
486
{
482
487
let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
483
488
let ptr = alloc. allocate ( layout) ?. cast ( ) ;
@@ -505,12 +510,13 @@ impl<T, A: Allocator> Box<T, A> {
505
510
///
506
511
/// [zeroed]: mem::MaybeUninit::zeroed
507
512
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
513
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
508
514
#[ cfg( not( no_global_oom_handling) ) ]
509
515
// #[unstable(feature = "new_uninit", issue = "63291")]
510
516
#[ 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 >
512
518
where
513
- A : Allocator ,
519
+ A : ~ const Allocator + ~ const Destruct ,
514
520
{
515
521
let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
516
522
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -545,9 +551,10 @@ impl<T, A: Allocator> Box<T, A> {
545
551
/// [zeroed]: mem::MaybeUninit::zeroed
546
552
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
547
553
// #[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 >
549
556
where
550
- A : Allocator ,
557
+ A : ~ const Allocator + ~ const Destruct ,
551
558
{
552
559
let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
553
560
let ptr = alloc. allocate_zeroed ( layout) ?. cast ( ) ;
@@ -563,11 +570,12 @@ impl<T, A: Allocator> Box<T, A> {
563
570
/// construct a (pinned) `Box` in a different way than with [`Box::new_in`].
564
571
#[ cfg( not( no_global_oom_handling) ) ]
565
572
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
573
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
566
574
#[ must_use]
567
575
#[ 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 >
569
577
where
570
- A : ' static + Allocator ,
578
+ A : ' static + ~ const Allocator + ~ const Destruct ,
571
579
{
572
580
Self :: into_pin ( Self :: new_in ( x, alloc) )
573
581
}
@@ -576,7 +584,8 @@ impl<T, A: Allocator> Box<T, A> {
576
584
///
577
585
/// This conversion does not allocate on the heap and happens in place.
578
586
#[ 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 > {
580
589
let ( raw, alloc) = Box :: into_raw_with_allocator ( boxed) ;
581
590
unsafe { Box :: from_raw_in ( raw as * mut [ T ; 1 ] , alloc) }
582
591
}
@@ -593,8 +602,12 @@ impl<T, A: Allocator> Box<T, A> {
593
602
/// assert_eq!(Box::into_inner(c), 5);
594
603
/// ```
595
604
#[ unstable( feature = "box_into_inner" , issue = "80437" ) ]
605
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
596
606
#[ 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
+ {
598
611
* boxed
599
612
}
600
613
}
@@ -808,8 +821,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
808
821
/// assert_eq!(*five, 5)
809
822
/// ```
810
823
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
824
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
811
825
#[ inline]
812
- pub unsafe fn assume_init ( self ) -> Box < T , A > {
826
+ pub const unsafe fn assume_init ( self ) -> Box < T , A > {
813
827
let ( raw, alloc) = Box :: into_raw_with_allocator ( self ) ;
814
828
unsafe { Box :: from_raw_in ( raw as * mut T , alloc) }
815
829
}
@@ -842,8 +856,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
842
856
/// }
843
857
/// ```
844
858
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
859
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
845
860
#[ 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 > {
847
862
unsafe {
848
863
( * boxed) . write ( value) ;
849
864
boxed. assume_init ( )
@@ -1086,8 +1101,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1086
1101
///
1087
1102
/// [memory layout]: self#memory-layout
1088
1103
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1104
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1089
1105
#[ 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 ) {
1091
1107
let ( leaked, alloc) = Box :: into_unique ( b) ;
1092
1108
( leaked. as_ptr ( ) , alloc)
1093
1109
}
@@ -1097,9 +1113,10 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1097
1113
issue = "none" ,
1098
1114
reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
1099
1115
) ]
1116
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1100
1117
#[ inline]
1101
1118
#[ doc( hidden) ]
1102
- pub fn into_unique ( b : Self ) -> ( Unique < T > , A ) {
1119
+ pub const fn into_unique ( b : Self ) -> ( Unique < T > , A ) {
1103
1120
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
1104
1121
// raw pointer for the type system. Turning it directly into a raw pointer would not be
1105
1122
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
@@ -1157,8 +1174,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1157
1174
/// assert_eq!(*static_ref, [4, 2, 3]);
1158
1175
/// ```
1159
1176
#[ stable( feature = "box_leak" , since = "1.26.0" ) ]
1177
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1160
1178
#[ inline]
1161
- pub fn leak < ' a > ( b : Self ) -> & ' a mut T
1179
+ pub const fn leak < ' a > ( b : Self ) -> & ' a mut T
1162
1180
where
1163
1181
A : ' a ,
1164
1182
{
@@ -1228,7 +1246,7 @@ impl<T: Default> Default for Box<T> {
1228
1246
#[ cfg( not( no_global_oom_handling) ) ]
1229
1247
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1230
1248
#[ rustc_const_unstable( feature = "const_default_impls" , issue = "87864" ) ]
1231
- impl < T > Default for Box < [ T ] > {
1249
+ impl < T > const Default for Box < [ T ] > {
1232
1250
fn default ( ) -> Self {
1233
1251
let ptr: Unique < [ T ] > = Unique :: < [ T ; 0 ] > :: dangling ( ) ;
1234
1252
Box ( ptr, Global )
@@ -1238,7 +1256,7 @@ impl<T> Default for Box<[T]> {
1238
1256
#[ cfg( not( no_global_oom_handling) ) ]
1239
1257
#[ stable( feature = "default_box_extra" , since = "1.17.0" ) ]
1240
1258
#[ rustc_const_unstable( feature = "const_default_impls" , issue = "87864" ) ]
1241
- impl Default for Box < str > {
1259
+ impl const Default for Box < str > {
1242
1260
fn default ( ) -> Self {
1243
1261
// SAFETY: This is the same as `Unique::cast<U>` but with an unsized `U = str`.
1244
1262
let ptr: Unique < str > = unsafe {
@@ -1434,7 +1452,8 @@ impl<T> From<T> for Box<T> {
1434
1452
}
1435
1453
1436
1454
#[ 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 > >
1438
1457
where
1439
1458
A : ' static ,
1440
1459
{
@@ -1822,7 +1841,8 @@ impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
1822
1841
}
1823
1842
1824
1843
#[ 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 > {
1826
1846
type Target = T ;
1827
1847
1828
1848
fn deref ( & self ) -> & T {
@@ -1831,7 +1851,8 @@ impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
1831
1851
}
1832
1852
1833
1853
#[ 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 > {
1835
1856
fn deref_mut ( & mut self ) -> & mut T {
1836
1857
& mut * * self
1837
1858
}
@@ -2010,7 +2031,8 @@ impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
2010
2031
* could have a method to project a Pin<T> from it.
2011
2032
*/
2012
2033
#[ 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 { }
2014
2036
2015
2037
#[ unstable( feature = "generator_trait" , issue = "43122" ) ]
2016
2038
impl < G : ?Sized + Generator < R > + Unpin , R , A : Allocator > Generator < R > for Box < G , A >
0 commit comments