@@ -346,9 +346,13 @@ impl<T, A: Allocator> Box<T, A> {
346
346
/// ```
347
347
#[ cfg( not( no_global_oom_handling) ) ]
348
348
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
349
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
349
350
#[ must_use]
350
351
#[ 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
+ {
352
356
let mut boxed = Self :: new_uninit_in ( alloc) ;
353
357
unsafe {
354
358
boxed. as_mut_ptr ( ) . write ( x) ;
@@ -372,8 +376,13 @@ impl<T, A: Allocator> Box<T, A> {
372
376
/// # Ok::<(), std::alloc::AllocError>(())
373
377
/// ```
374
378
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
379
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
375
380
#[ 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
+ {
377
386
let mut boxed = Self :: try_new_uninit_in ( alloc) ?;
378
387
unsafe {
379
388
boxed. as_mut_ptr ( ) . write ( x) ;
@@ -402,10 +411,14 @@ impl<T, A: Allocator> Box<T, A> {
402
411
/// assert_eq!(*five, 5)
403
412
/// ```
404
413
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
414
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
405
415
#[ cfg( not( no_global_oom_handling) ) ]
406
416
#[ must_use]
407
417
// #[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
+ {
409
422
let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
410
423
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
411
424
// That would make code size bigger.
@@ -439,7 +452,11 @@ impl<T, A: Allocator> Box<T, A> {
439
452
/// ```
440
453
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
441
454
// #[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
+ {
443
460
let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
444
461
let ptr = alloc. allocate ( layout) ?. cast ( ) ;
445
462
unsafe { Ok ( Box :: from_raw_in ( ptr. as_ptr ( ) , alloc) ) }
@@ -466,10 +483,14 @@ impl<T, A: Allocator> Box<T, A> {
466
483
///
467
484
/// [zeroed]: mem::MaybeUninit::zeroed
468
485
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
486
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
469
487
#[ cfg( not( no_global_oom_handling) ) ]
470
488
// #[unstable(feature = "new_uninit", issue = "63291")]
471
489
#[ 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
+ {
473
494
let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
474
495
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
475
496
// That would make code size bigger.
@@ -503,7 +524,11 @@ impl<T, A: Allocator> Box<T, A> {
503
524
/// [zeroed]: mem::MaybeUninit::zeroed
504
525
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
505
526
// #[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
+ {
507
532
let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
508
533
let ptr = alloc. allocate_zeroed ( layout) ?. cast ( ) ;
509
534
unsafe { Ok ( Box :: from_raw_in ( ptr. as_ptr ( ) , alloc) ) }
@@ -513,20 +538,22 @@ impl<T, A: Allocator> Box<T, A> {
513
538
/// `x` will be pinned in memory and unable to be moved.
514
539
#[ cfg( not( no_global_oom_handling) ) ]
515
540
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
541
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
516
542
#[ must_use]
517
543
#[ 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 >
519
545
where
520
- A : ' static ,
546
+ A : ' static + ~ const Allocator + ~ const Drop ,
521
547
{
522
- Self :: new_in ( x, alloc) . into ( )
548
+ Self :: into_pin ( Self :: new_in ( x, alloc) )
523
549
}
524
550
525
551
/// Converts a `Box<T>` into a `Box<[T]>`
526
552
///
527
553
/// This conversion does not allocate on the heap and happens in place.
528
554
#[ 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 > {
530
557
let ( raw, alloc) = Box :: into_raw_with_allocator ( boxed) ;
531
558
unsafe { Box :: from_raw_in ( raw as * mut [ T ; 1 ] , alloc) }
532
559
}
@@ -543,8 +570,12 @@ impl<T, A: Allocator> Box<T, A> {
543
570
/// assert_eq!(Box::into_inner(c), 5);
544
571
/// ```
545
572
#[ unstable( feature = "box_into_inner" , issue = "80437" ) ]
573
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
546
574
#[ 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
+ {
548
579
* boxed
549
580
}
550
581
}
@@ -758,8 +789,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
758
789
/// assert_eq!(*five, 5)
759
790
/// ```
760
791
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
792
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
761
793
#[ inline]
762
- pub unsafe fn assume_init ( self ) -> Box < T , A > {
794
+ pub const unsafe fn assume_init ( self ) -> Box < T , A > {
763
795
let ( raw, alloc) = Box :: into_raw_with_allocator ( self ) ;
764
796
unsafe { Box :: from_raw_in ( raw as * mut T , alloc) }
765
797
}
@@ -792,8 +824,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
792
824
/// }
793
825
/// ```
794
826
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
827
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
795
828
#[ 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 > {
797
830
unsafe {
798
831
( * boxed) . write ( value) ;
799
832
boxed. assume_init ( )
@@ -938,8 +971,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
938
971
/// [memory layout]: self#memory-layout
939
972
/// [`Layout`]: crate::Layout
940
973
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
974
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
941
975
#[ 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 {
943
977
Box ( unsafe { Unique :: new_unchecked ( raw) } , alloc)
944
978
}
945
979
@@ -1035,8 +1069,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1035
1069
///
1036
1070
/// [memory layout]: self#memory-layout
1037
1071
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1072
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1038
1073
#[ 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 ) {
1040
1075
let ( leaked, alloc) = Box :: into_unique ( b) ;
1041
1076
( leaked. as_ptr ( ) , alloc)
1042
1077
}
@@ -1046,9 +1081,10 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1046
1081
issue = "none" ,
1047
1082
reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
1048
1083
) ]
1084
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1049
1085
#[ inline]
1050
1086
#[ doc( hidden) ]
1051
- pub fn into_unique ( b : Self ) -> ( Unique < T > , A ) {
1087
+ pub const fn into_unique ( b : Self ) -> ( Unique < T > , A ) {
1052
1088
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
1053
1089
// raw pointer for the type system. Turning it directly into a raw pointer would not be
1054
1090
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
@@ -1064,8 +1100,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1064
1100
/// to call it as `Box::allocator(&b)` instead of `b.allocator()`. This
1065
1101
/// is so that there is no conflict with a method on the inner type.
1066
1102
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1103
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1067
1104
#[ inline]
1068
- pub fn allocator ( b : & Self ) -> & A {
1105
+ pub const fn allocator ( b : & Self ) -> & A {
1069
1106
& b. 1
1070
1107
}
1071
1108
@@ -1105,8 +1142,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1105
1142
/// assert_eq!(*static_ref, [4, 2, 3]);
1106
1143
/// ```
1107
1144
#[ stable( feature = "box_leak" , since = "1.26.0" ) ]
1145
+ #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1108
1146
#[ inline]
1109
- pub fn leak < ' a > ( b : Self ) -> & ' a mut T
1147
+ pub const fn leak < ' a > ( b : Self ) -> & ' a mut T
1110
1148
where
1111
1149
A : ' a ,
1112
1150
{
@@ -1119,7 +1157,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1119
1157
///
1120
1158
/// This is also available via [`From`].
1121
1159
#[ 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 >
1123
1162
where
1124
1163
A : ' static ,
1125
1164
{
@@ -1131,7 +1170,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
1131
1170
}
1132
1171
1133
1172
#[ 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 > {
1135
1175
fn drop ( & mut self ) {
1136
1176
// FIXME: Do nothing, drop is currently performed by compiler.
1137
1177
}
@@ -1341,7 +1381,8 @@ impl<T> From<T> for Box<T> {
1341
1381
}
1342
1382
1343
1383
#[ 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 > >
1345
1386
where
1346
1387
A : ' static ,
1347
1388
{
@@ -1720,7 +1761,8 @@ impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
1720
1761
}
1721
1762
1722
1763
#[ 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 > {
1724
1766
type Target = T ;
1725
1767
1726
1768
fn deref ( & self ) -> & T {
@@ -1729,7 +1771,8 @@ impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
1729
1771
}
1730
1772
1731
1773
#[ 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 > {
1733
1776
fn deref_mut ( & mut self ) -> & mut T {
1734
1777
& mut * * self
1735
1778
}
@@ -1908,7 +1951,8 @@ impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
1908
1951
* could have a method to project a Pin<T> from it.
1909
1952
*/
1910
1953
#[ 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 { }
1912
1956
1913
1957
#[ unstable( feature = "generator_trait" , issue = "43122" ) ]
1914
1958
impl < G : ?Sized + Generator < R > + Unpin , R , A : Allocator > Generator < R > for Box < G , A >
0 commit comments