@@ -88,7 +88,8 @@ use core::ops::{
88
88
use core:: ptr:: { self , NonNull , Unique } ;
89
89
use core:: task:: { Context , Poll } ;
90
90
91
- use crate :: alloc:: { Alloc , AllocErr , Global , Layout , handle_alloc_error} ;
91
+ use crate :: abort_adapter:: AbortAdapter ;
92
+ use crate :: alloc:: { Alloc , Global , Layout } ;
92
93
use crate :: vec:: Vec ;
93
94
use crate :: raw_vec:: RawVec ;
94
95
use crate :: str:: from_boxed_utf8_unchecked;
@@ -99,7 +100,7 @@ use crate::str::from_boxed_utf8_unchecked;
99
100
#[ lang = "owned_box" ]
100
101
#[ fundamental]
101
102
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
102
- pub struct Box < T : ?Sized , A = Global > ( Unique < T > , pub ( crate ) A ) ;
103
+ pub struct Box < T : ?Sized , A = AbortAdapter < Global > > ( Unique < T > , pub ( crate ) A ) ;
103
104
104
105
impl < T > Box < T > {
105
106
/// Allocates memory on the heap and then places `x` into it.
@@ -126,7 +127,7 @@ impl<T> Box<T> {
126
127
}
127
128
}
128
129
129
- impl < T , A : Alloc < Err = AllocErr > > Box < T , A > {
130
+ impl < T , A : Alloc > Box < T , A > {
130
131
/// Allocates memory in the given allocator and then places `x` into it.
131
132
///
132
133
/// This doesn't actually allocate if `T` is zero-sized.
@@ -140,15 +141,15 @@ impl<T, A: Alloc<Err = AllocErr>> Box<T, A> {
140
141
/// ```
141
142
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
142
143
#[ inline( always) ]
143
- pub fn new_in ( x : T , a : A ) -> Box < T , A > {
144
+ pub fn new_in ( x : T , a : A ) -> Result < Box < T , A > , A :: Err > {
144
145
let mut a = a;
145
146
let layout = Layout :: for_value ( & x) ;
146
147
let size = layout. size ( ) ;
147
148
let ptr = if size == 0 {
148
149
Unique :: empty ( )
149
150
} else {
150
151
unsafe {
151
- let ptr = a. alloc ( layout) . unwrap_or_else ( |_| handle_alloc_error ( layout ) ) ;
152
+ let ptr = a. alloc ( layout) ? ;
152
153
ptr. cast ( ) . into ( )
153
154
}
154
155
} ;
@@ -157,15 +158,15 @@ impl<T, A: Alloc<Err = AllocErr>> Box<T, A> {
157
158
unsafe {
158
159
ptr:: write ( ptr. as_ptr ( ) as * mut T , x) ;
159
160
}
160
- Box ( ptr, a)
161
+ Ok ( Box ( ptr, a) )
161
162
}
162
163
163
164
/// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
164
165
/// `x` will be pinned in memory and unable to be moved.
165
166
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
166
167
#[ inline( always) ]
167
- pub fn pin_in ( x : T , a : A ) -> Pin < Box < T , A > > {
168
- Box :: new_in ( x, a) . into ( )
168
+ pub fn pin_in ( x : T , a : A ) -> Result < Pin < Box < T , A > > , A :: Err > {
169
+ Box :: new_in ( x, a) . map ( Into :: into )
169
170
}
170
171
}
171
172
@@ -209,7 +210,7 @@ impl<T: ?Sized> Box<T> {
209
210
#[ stable( feature = "box_raw" , since = "1.4.0" ) ]
210
211
#[ inline]
211
212
pub unsafe fn from_raw ( raw : * mut T ) -> Self {
212
- Box ( Unique :: new_unchecked ( raw) , Global )
213
+ Box ( Unique :: new_unchecked ( raw) , AbortAdapter ( Global ) )
213
214
}
214
215
}
215
216
@@ -293,7 +294,7 @@ impl<T: ?Sized, A> Box<T, A> {
293
294
/// [`Box::from_raw`]: struct.Box.html#method.from_raw
294
295
#[ stable( feature = "box_raw" , since = "1.4.0" ) ]
295
296
#[ inline]
296
- pub fn into_raw ( b : Box < T , A > ) -> * mut T {
297
+ pub fn into_raw ( b : Self ) -> * mut T {
297
298
Box :: into_raw_non_null ( b) . as_ptr ( )
298
299
}
299
300
@@ -333,10 +334,20 @@ impl<T: ?Sized, A> Box<T, A> {
333
334
Box :: into_unique ( b) . into ( )
334
335
}
335
336
337
+ /// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>` along
338
+ /// with the allocator.
339
+ // Also feature = "box_into_raw_non_null", issue = "47336", FIXME how to write?
340
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
341
+ #[ inline]
342
+ pub fn into_both_non_null ( b : Self ) -> ( NonNull < T > , A ) {
343
+ let ( unique, alloc) = Box :: into_both_unique ( b) . into ( ) ;
344
+ ( unique. into ( ) , alloc)
345
+ }
346
+
336
347
#[ unstable( feature = "ptr_internals" , issue = "0" , reason = "use into_raw_non_null instead" ) ]
337
348
#[ inline]
338
349
#[ doc( hidden) ]
339
- pub fn into_unique ( b : Box < T , A > ) -> Unique < T > {
350
+ pub fn into_unique ( b : Self ) -> Unique < T > {
340
351
let mut unique = b. 0 ;
341
352
mem:: forget ( b) ;
342
353
// Box is kind-of a library type, but recognized as a "unique pointer" by
@@ -348,6 +359,22 @@ impl<T: ?Sized, A> Box<T, A> {
348
359
unsafe { Unique :: new_unchecked ( unique. as_mut ( ) as * mut T ) }
349
360
}
350
361
362
+ /// Consumes the `Box`, returning the wrapped pointer as `Unique<T>` along
363
+ /// with the allocator.
364
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
365
+ #[ inline]
366
+ #[ doc( hidden) ]
367
+ pub fn into_both_unique ( mut b : Self ) -> ( Unique < T > , A ) {
368
+ let unique = b. 0 . into ( ) ;
369
+ let alloc = unsafe {
370
+ let mut a = mem:: MaybeUninit :: uninit ( ) ;
371
+ ptr:: swap ( a. as_mut_ptr ( ) , & mut b. 1 as * mut _ ) ;
372
+ mem:: forget ( b) ;
373
+ a. assume_init ( )
374
+ } ;
375
+ ( unique, alloc)
376
+ }
377
+
351
378
/// Consumes and leaks the `Box`, returning a mutable reference,
352
379
/// `&'a mut T`. Note that the type `T` must outlive the chosen lifetime
353
380
/// `'a`. If the type has only static references, or none at all, then this
@@ -391,7 +418,7 @@ impl<T: ?Sized, A> Box<T, A> {
391
418
/// ```
392
419
#[ stable( feature = "box_leak" , since = "1.26.0" ) ]
393
420
#[ inline]
394
- pub fn leak < ' a > ( b : Box < T , A > ) -> & ' a mut T
421
+ pub fn leak < ' a > ( b : Self ) -> & ' a mut T
395
422
where
396
423
T : ' a // Technically not needed, but kept to be explicit.
397
424
{
@@ -412,6 +439,7 @@ impl<T: ?Sized, A> Box<T, A> {
412
439
}
413
440
}
414
441
442
+
415
443
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
416
444
unsafe impl < #[ may_dangle] T : ?Sized , A > Drop for Box < T , A > {
417
445
fn drop ( & mut self ) {
@@ -420,29 +448,31 @@ unsafe impl<#[may_dangle] T: ?Sized, A> Drop for Box<T, A> {
420
448
}
421
449
422
450
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
423
- impl < T : Default , A : Alloc < Err = AllocErr > + Default > Default for Box < T , A > {
451
+ impl < T : Default , A : Alloc < Err = ! > + Default > Default for Box < T , A > {
424
452
/// Creates a `Box<T, A>`, with the `Default` value for T.
425
453
fn default ( ) -> Box < T , A > {
426
- Box :: new_in ( Default :: default ( ) , A :: default ( ) )
454
+ let Ok ( b) = Box :: new_in ( Default :: default ( ) , A :: default ( ) ) ;
455
+ b
427
456
}
428
457
}
429
458
430
459
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
431
- impl < T , A : Alloc < Err = AllocErr > + Default > Default for Box < [ T ] , A > {
460
+ impl < T , A : Alloc < Err =! > + Default > Default for Box < [ T ] , A > {
432
461
fn default ( ) -> Box < [ T ] , A > {
433
- Box :: < [ T ; 0 ] , A > :: new_in ( [ ] , A :: default ( ) )
462
+ let Ok ( b) = Box :: < [ T ; 0 ] , A > :: new_in ( [ ] , Default :: default ( ) ) ;
463
+ b
434
464
}
435
465
}
436
466
437
467
#[ stable( feature = "default_box_extra" , since = "1.17.0" ) ]
438
- impl < A : Alloc < Err = AllocErr > + Default > Default for Box < str , A > {
468
+ impl < A : Alloc < Err = ! > + Default > Default for Box < str , A > {
439
469
fn default ( ) -> Box < str , A > {
440
470
unsafe { from_boxed_utf8_unchecked ( Default :: default ( ) ) }
441
471
}
442
472
}
443
473
444
474
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
445
- impl < T : Clone , A : Alloc < Err = AllocErr > + Clone > Clone for Box < T , A > {
475
+ impl < T : Clone , A : Alloc < Err = ! > + Clone > Clone for Box < T , A > {
446
476
/// Returns a new box with a `clone()` of this box's contents.
447
477
///
448
478
/// # Examples
@@ -453,8 +483,9 @@ impl<T: Clone, A: Alloc<Err = AllocErr> + Clone> Clone for Box<T, A> {
453
483
/// ```
454
484
#[ rustfmt:: skip]
455
485
#[ inline]
456
- fn clone ( & self ) -> Box < T , A > {
457
- Box :: new_in ( ( * * self ) . clone ( ) , self . 1 . clone ( ) )
486
+ fn clone ( & self ) -> Self {
487
+ let Ok ( b) = Box :: new_in ( ( * * self ) . clone ( ) , self . 1 . clone ( ) ) ;
488
+ b
458
489
}
459
490
/// Copies `source`'s contents into `self` without creating a new allocation.
460
491
///
@@ -469,13 +500,13 @@ impl<T: Clone, A: Alloc<Err = AllocErr> + Clone> Clone for Box<T, A> {
469
500
/// assert_eq!(*y, 5);
470
501
/// ```
471
502
#[ inline]
472
- fn clone_from ( & mut self , source : & Box < T , A > ) {
503
+ fn clone_from ( & mut self , source : & Self ) {
473
504
( * * self ) . clone_from ( & ( * * source) ) ;
474
505
}
475
506
}
476
507
477
508
#[ stable( feature = "box_slice_clone" , since = "1.3.0" ) ]
478
- impl < A : Alloc < Err = AllocErr > + Clone > Clone for Box < str , A > {
509
+ impl < A : Alloc < Err = ! > + Clone > Clone for Box < str , A > {
479
510
fn clone ( & self ) -> Self {
480
511
// this makes a copy of the data
481
512
let buf = Box :: < [ u8 ] , A > :: from_slice_in ( self . as_bytes ( ) , self . 1 . clone ( ) ) ;
@@ -485,6 +516,7 @@ impl<A: Alloc<Err = AllocErr> + Clone> Clone for Box<str, A> {
485
516
}
486
517
}
487
518
519
+ /// Just the contents are compared, the allocator is ignored
488
520
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
489
521
impl < T : ?Sized + PartialEq , A > PartialEq for Box < T , A > {
490
522
#[ inline]
@@ -496,6 +528,7 @@ impl<T: ?Sized + PartialEq, A> PartialEq for Box<T, A> {
496
528
PartialEq :: ne ( & * * self , & * * other)
497
529
}
498
530
}
531
+ /// Just the contents are compared, the allocator is ignored
499
532
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
500
533
impl < T : ?Sized + PartialOrd , A > PartialOrd for Box < T , A > {
501
534
#[ inline]
@@ -519,23 +552,27 @@ impl<T: ?Sized + PartialOrd, A> PartialOrd for Box<T, A> {
519
552
PartialOrd :: gt ( & * * self , & * * other)
520
553
}
521
554
}
555
+ /// Just the contents are compared, the allocator is ignored
522
556
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
523
557
impl < T : ?Sized + Ord , A > Ord for Box < T , A > {
524
558
#[ inline]
525
559
fn cmp ( & self , other : & Box < T , A > ) -> Ordering {
526
560
Ord :: cmp ( & * * self , & * * other)
527
561
}
528
562
}
563
+ /// Just the contents are compared, the allocator is ignored
529
564
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
530
565
impl < T : ?Sized + Eq , A > Eq for Box < T , A > { }
531
566
567
+ /// Just the contents are compared, the allocator is ignored
532
568
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
533
569
impl < T : ?Sized + Hash , A > Hash for Box < T , A > {
534
570
fn hash < H : Hasher > ( & self , state : & mut H ) {
535
571
( * * self ) . hash ( state) ;
536
572
}
537
573
}
538
574
575
+ /// Just the contents are compared, the allocator is ignored
539
576
#[ stable( feature = "indirect_hasher_impl" , since = "1.22.0" ) ]
540
577
impl < T : ?Sized + Hasher , A > Hasher for Box < T , A > {
541
578
fn finish ( & self ) -> u64 {
@@ -583,7 +620,7 @@ impl<T: ?Sized + Hasher, A> Hasher for Box<T, A> {
583
620
}
584
621
585
622
#[ stable( feature = "from_for_ptrs" , since = "1.6.0" ) ]
586
- impl < T , A : Alloc < Err = AllocErr > + Default > From < T > for Box < T , A > {
623
+ impl < T , A : Alloc < Err = ! > + Default > From < T > for Box < T , A > {
587
624
/// Converts a generic type `T` into a `Box<T>`
588
625
///
589
626
/// The conversion allocates on the heap and moves `t`
@@ -597,7 +634,8 @@ impl<T, A: Alloc<Err = AllocErr> + Default> From<T> for Box<T, A> {
597
634
/// assert_eq!(Box::from(x), boxed);
598
635
/// ```
599
636
fn from ( t : T ) -> Self {
600
- Box :: new_in ( t, A :: default ( ) )
637
+ let Ok ( b) = Box :: new_in ( t, Default :: default ( ) ) ;
638
+ b
601
639
}
602
640
}
603
641
@@ -611,10 +649,10 @@ impl<T: ?Sized, A> From<Box<T, A>> for Pin<Box<T, A>> {
611
649
}
612
650
}
613
651
614
- impl < T : Copy , A : Alloc < Err = AllocErr > > Box < [ T ] , A > {
652
+ impl < T : Copy , A : Alloc < Err = ! > > Box < [ T ] , A > {
615
653
fn from_slice_in ( slice : & [ T ] , a : A ) -> Box < [ T ] , A > {
616
654
let len = slice. len ( ) ;
617
- let buf = RawVec :: with_capacity_in ( len, a) ;
655
+ let Ok ( buf) = RawVec :: with_capacity_in ( len, a) ;
618
656
unsafe {
619
657
ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
620
658
buf. into_box ( )
@@ -623,7 +661,7 @@ impl<T: Copy, A: Alloc<Err = AllocErr>> Box<[T], A> {
623
661
}
624
662
625
663
#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
626
- impl < T : Copy , A : Alloc < Err = AllocErr > + Default > From < & [ T ] > for Box < [ T ] , A > {
664
+ impl < T : Copy , A : Alloc < Err = ! > + Default > From < & [ T ] > for Box < [ T ] , A > {
627
665
/// Converts a `&[T]` into a `Box<[T]>`
628
666
///
629
667
/// This conversion allocates on the heap
@@ -643,7 +681,7 @@ impl<T: Copy, A: Alloc<Err = AllocErr> + Default> From<&[T]> for Box<[T], A> {
643
681
}
644
682
645
683
#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
646
- impl < A : Alloc < Err = AllocErr > + Default > From < & str > for Box < str , A > {
684
+ impl < A : Alloc < Err = ! > + Default > From < & str > for Box < str , A > {
647
685
/// Converts a `&str` into a `Box<str>`
648
686
///
649
687
/// This conversion allocates on the heap
@@ -715,7 +753,7 @@ impl<A> Box<dyn Any, A> {
715
753
}
716
754
}
717
755
718
- impl < A : Alloc < Err =AllocErr > > Box < dyn Any + Send , A > {
756
+ impl < A : Alloc < Err =! > > Box < dyn Any + Send , A > {
719
757
#[ inline]
720
758
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
721
759
/// Attempt to downcast the box to a concrete type.
@@ -909,9 +947,9 @@ impl<A, F: Fn<A> + ?Sized, Alloc> Fn<A> for Box<F, Alloc> {
909
947
#[ rustc_paren_sugar]
910
948
#[ unstable( feature = "fnbox" , issue = "28796" ) ]
911
949
#[ rustc_deprecated( reason = "use `FnOnce`, `FnMut`, or `Fn` instead" , since = "1.37.0" ) ]
912
- pub trait FnBox < A > : FnOnce < A > {
950
+ pub trait FnBox < Args , A : Alloc = AbortAdapter < Global > > : FnOnce < Args > {
913
951
/// Performs the call operation.
914
- fn call_box ( self : Box < Self > , args : A ) -> Self :: Output ;
952
+ fn call_box ( self : Box < Self , A > , args : Args ) -> Self :: Output ;
915
953
}
916
954
917
955
#[ unstable( feature = "fnbox" , issue = "28796" ) ]
@@ -940,10 +978,12 @@ impl<A> FromIterator<A> for Box<[A]> {
940
978
}
941
979
942
980
#[ stable( feature = "box_slice_clone" , since = "1.3.0" ) ]
943
- impl < T : Clone , A : Alloc < Err = AllocErr > + Clone > Clone for Box < [ T ] , A > {
981
+ impl < T : Clone , A : Alloc < Err = ! > + Clone > Clone for Box < [ T ] , A > {
944
982
fn clone ( & self ) -> Self {
983
+ let Ok ( b) = RawVec :: with_capacity_in ( self . len ( ) , self . 1 . clone ( ) ) ;
984
+
945
985
let mut new = BoxBuilder {
946
- data : RawVec :: with_capacity_in ( self . len ( ) , self . 1 . clone ( ) ) ,
986
+ data : b ,
947
987
len : 0 ,
948
988
} ;
949
989
@@ -961,20 +1001,20 @@ impl<T: Clone, A: Alloc<Err = AllocErr> + Clone> Clone for Box<[T], A> {
961
1001
return unsafe { new. into_box ( ) } ;
962
1002
963
1003
// Helper type for responding to panics correctly.
964
- struct BoxBuilder < T , A : Alloc < Err = AllocErr > > {
1004
+ struct BoxBuilder < T , A : Alloc > {
965
1005
data : RawVec < T , A > ,
966
1006
len : usize ,
967
1007
}
968
1008
969
- impl < T , A : Alloc < Err = AllocErr > > BoxBuilder < T , A > {
1009
+ impl < T , A : Alloc > BoxBuilder < T , A > {
970
1010
unsafe fn into_box ( self ) -> Box < [ T ] , A > {
971
1011
let raw = ptr:: read ( & self . data ) ;
972
1012
mem:: forget ( self ) ;
973
1013
raw. into_box ( )
974
1014
}
975
1015
}
976
1016
977
- impl < T , A : Alloc < Err = AllocErr > > Drop for BoxBuilder < T , A > {
1017
+ impl < T , A : Alloc > Drop for BoxBuilder < T , A > {
978
1018
fn drop ( & mut self ) {
979
1019
let mut data = self . data . ptr ( ) ;
980
1020
let max = unsafe { data. add ( self . len ) } ;
0 commit comments