@@ -87,8 +87,9 @@ use core::ops::{
87
87
use core:: ptr:: { self , NonNull , Unique } ;
88
88
use core:: task:: { Context , Poll } ;
89
89
90
+ use crate :: abort_adapter:: AbortAdapter ;
90
91
use crate :: alloc:: {
91
- Stage0Alloc as Alloc , AllocHelper , AllocErr , Global , Layout , handle_alloc_error , stage0_phantom, stage0_unphantom
92
+ Stage0Alloc as Alloc , Global , Layout , stage0_phantom, stage0_unphantom
92
93
} ;
93
94
use crate :: vec:: Vec ;
94
95
use crate :: raw_vec:: RawVec ;
@@ -101,7 +102,7 @@ use crate::str::from_boxed_utf8_unchecked;
101
102
#[ lang = "owned_box" ]
102
103
#[ fundamental]
103
104
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
104
- pub struct Box < T : ?Sized , A = Global > ( Unique < T > , pub ( crate ) A ) ;
105
+ pub struct Box < T : ?Sized , A = AbortAdapter < Global > > ( Unique < T > , pub ( crate ) A ) ;
105
106
106
107
// Use a variant with PhantomData in stage0, to satisfy the limitations of
107
108
// DispatchFromDyn in 1.35.
@@ -110,7 +111,7 @@ pub struct Box<T: ?Sized, A = Global>(Unique<T>, pub(crate) A);
110
111
#[ lang = "owned_box" ]
111
112
#[ fundamental]
112
113
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
113
- pub struct Box < T : ?Sized , A = Global > ( Unique < T > , pub ( crate ) PhantomData < A > ) ;
114
+ pub struct Box < T : ?Sized , A = AbortAdapter < Global > > ( Unique < T > , pub ( crate ) PhantomData < A > ) ;
114
115
115
116
impl < T > Box < T > {
116
117
/// Allocates memory on the heap and then places `x` into it.
@@ -137,7 +138,7 @@ impl<T> Box<T> {
137
138
}
138
139
}
139
140
140
- impl < T , A : Alloc < Err = AllocErr > > Box < T , A > {
141
+ impl < T , A : Alloc > Box < T , A > {
141
142
/// Allocates memory in the given allocator and then places `x` into it.
142
143
///
143
144
/// This doesn't actually allocate if `T` is zero-sized.
@@ -151,15 +152,15 @@ impl<T, A: Alloc<Err = AllocErr>> Box<T, A> {
151
152
/// ```
152
153
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
153
154
#[ inline( always) ]
154
- pub fn new_in ( x : T , a : A ) -> Box < T , A > {
155
+ pub fn new_in ( x : T , a : A ) -> Result < Box < T , A > , A :: Err > {
155
156
let mut a = a;
156
157
let layout = Layout :: for_value ( & x) ;
157
158
let size = layout. size ( ) ;
158
159
let ptr = if size == 0 {
159
160
Unique :: empty ( )
160
161
} else {
161
162
unsafe {
162
- let ptr = a. alloc ( layout) . unwrap_or_else ( |_| handle_alloc_error ( layout ) ) ;
163
+ let ptr = a. alloc ( layout) ? ;
163
164
ptr. cast ( ) . into ( )
164
165
}
165
166
} ;
@@ -168,15 +169,15 @@ impl<T, A: Alloc<Err = AllocErr>> Box<T, A> {
168
169
unsafe {
169
170
ptr:: write ( ptr. as_ptr ( ) as * mut T , x) ;
170
171
}
171
- Box ( ptr, stage0_phantom ( a) )
172
+ Ok ( Box ( ptr, stage0_phantom ( a) ) )
172
173
}
173
174
174
175
/// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
175
176
/// `x` will be pinned in memory and unable to be moved.
176
177
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
177
178
#[ inline( always) ]
178
- pub fn pin_in ( x : T , a : A ) -> Pin < Box < T , A > > {
179
- Box :: new_in ( x, a) . into ( )
179
+ pub fn pin_in ( x : T , a : A ) -> Result < Pin < Box < T , A > > , A :: Err > {
180
+ Box :: new_in ( x, a) . map ( Into :: into )
180
181
}
181
182
}
182
183
@@ -206,7 +207,7 @@ impl<T: ?Sized> Box<T> {
206
207
#[ stable( feature = "box_raw" , since = "1.4.0" ) ]
207
208
#[ inline]
208
209
pub unsafe fn from_raw ( raw : * mut T ) -> Self {
209
- Box ( Unique :: new_unchecked ( raw) , stage0_phantom ( Global ) )
210
+ Box ( Unique :: new_unchecked ( raw) , stage0_phantom ( AbortAdapter ( Global ) ) )
210
211
}
211
212
}
212
213
@@ -272,7 +273,7 @@ impl<T: ?Sized, A> Box<T, A> {
272
273
/// ```
273
274
#[ stable( feature = "box_raw" , since = "1.4.0" ) ]
274
275
#[ inline]
275
- pub fn into_raw ( b : Box < T , A > ) -> * mut T {
276
+ pub fn into_raw ( b : Self ) -> * mut T {
276
277
Box :: into_raw_non_null ( b) . as_ptr ( )
277
278
}
278
279
@@ -322,6 +323,21 @@ impl<T: ?Sized, A> Box<T, A> {
322
323
unsafe { Unique :: new_unchecked ( unique) }
323
324
}
324
325
326
+
327
+ #[ unstable( feature = "unique" , reason = "needs an RFC to flesh out design" ,
328
+ issue = "27730" ) ]
329
+ #[ inline]
330
+ pub fn into_both ( mut b : Self ) -> ( Unique < T > , A ) {
331
+ let unique = b. 0 ;
332
+ let alloc = unsafe {
333
+ let mut a = mem:: uninitialized ( ) ;
334
+ mem:: swap ( & mut a, & mut b. 1 ) ;
335
+ a
336
+ } ;
337
+ mem:: forget ( b) ;
338
+ ( unique, alloc)
339
+ }
340
+
325
341
/// Consumes and leaks the `Box`, returning a mutable reference,
326
342
/// `&'a mut T`. Note that the type `T` must outlive the chosen lifetime
327
343
/// `'a`. If the type has only static references, or none at all, then this
@@ -365,7 +381,7 @@ impl<T: ?Sized, A> Box<T, A> {
365
381
/// ```
366
382
#[ stable( feature = "box_leak" , since = "1.26.0" ) ]
367
383
#[ inline]
368
- pub fn leak < ' a > ( b : Box < T , A > ) -> & ' a mut T
384
+ pub fn leak < ' a > ( b : Self ) -> & ' a mut T
369
385
where
370
386
T : ' a // Technically not needed, but kept to be explicit.
371
387
{
@@ -386,6 +402,7 @@ impl<T: ?Sized, A> Box<T, A> {
386
402
}
387
403
}
388
404
405
+
389
406
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
390
407
unsafe impl < #[ may_dangle] T : ?Sized , A > Drop for Box < T , A > {
391
408
fn drop ( & mut self ) {
@@ -394,29 +411,31 @@ unsafe impl<#[may_dangle] T: ?Sized, A> Drop for Box<T, A> {
394
411
}
395
412
396
413
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
397
- impl < T : Default , A : Alloc < Err = AllocErr > + Default > Default for Box < T , A > {
414
+ impl < T : Default , A : Alloc < Err = ! > + Default > Default for Box < T , A > {
398
415
/// Creates a `Box<T, A>`, with the `Default` value for T.
399
416
fn default ( ) -> Box < T , A > {
400
- Box :: new_in ( Default :: default ( ) , A :: default ( ) )
417
+ let Ok ( b) = Box :: new_in ( Default :: default ( ) , A :: default ( ) ) ;
418
+ b
401
419
}
402
420
}
403
421
404
422
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
405
- impl < T , A : Alloc < Err = AllocErr > + Default > Default for Box < [ T ] , A > {
423
+ impl < T , A : Alloc < Err =! > + Default > Default for Box < [ T ] , A > {
406
424
fn default ( ) -> Box < [ T ] , A > {
407
- Box :: < [ T ; 0 ] , A > :: new_in ( [ ] , A :: default ( ) )
425
+ let Ok ( b) = Box :: < [ T ; 0 ] , A > :: new_in ( [ ] , Default :: default ( ) ) ;
426
+ b
408
427
}
409
428
}
410
429
411
430
#[ stable( feature = "default_box_extra" , since = "1.17.0" ) ]
412
- impl < A : Alloc < Err = AllocErr > + Default > Default for Box < str , A > {
431
+ impl < A : Alloc < Err = ! > + Default > Default for Box < str , A > {
413
432
fn default ( ) -> Box < str , A > {
414
433
unsafe { from_boxed_utf8_unchecked ( Default :: default ( ) ) }
415
434
}
416
435
}
417
436
418
437
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
419
- impl < T : Clone , A : Alloc < Err = AllocErr > + Clone > Clone for Box < T , A > {
438
+ impl < T : Clone , A : Alloc < Err = ! > + Clone > Clone for Box < T , A > {
420
439
/// Returns a new box with a `clone()` of this box's contents.
421
440
///
422
441
/// # Examples
@@ -427,8 +446,9 @@ impl<T: Clone, A: Alloc<Err = AllocErr> + Clone> Clone for Box<T, A> {
427
446
/// ```
428
447
#[ rustfmt:: skip]
429
448
#[ inline]
430
- fn clone ( & self ) -> Box < T , A > {
431
- Box :: new_in ( ( * * self ) . clone ( ) , stage0_unphantom ( self . 1 . clone ( ) ) )
449
+ fn clone ( & self ) -> Self {
450
+ let Ok ( b) = Box :: new_in ( ( * * self ) . clone ( ) , stage0_unphantom ( self . 1 . clone ( ) ) ) ;
451
+ b
432
452
}
433
453
/// Copies `source`'s contents into `self` without creating a new allocation.
434
454
///
@@ -443,23 +463,24 @@ impl<T: Clone, A: Alloc<Err = AllocErr> + Clone> Clone for Box<T, A> {
443
463
/// assert_eq!(*y, 5);
444
464
/// ```
445
465
#[ inline]
446
- fn clone_from ( & mut self , source : & Box < T , A > ) {
466
+ fn clone_from ( & mut self , source : & Self ) {
447
467
( * * self ) . clone_from ( & ( * * source) ) ;
448
468
}
449
469
}
450
470
451
471
#[ stable( feature = "box_slice_clone" , since = "1.3.0" ) ]
452
- impl < A : Alloc < Err = AllocErr > + Clone > Clone for Box < str , A > {
472
+ impl < A : Alloc < Err = ! > + Clone > Clone for Box < str , A > {
453
473
fn clone ( & self ) -> Self {
454
474
let len = self . len ( ) ;
455
- let buf = RawVec :: with_capacity_in ( len, stage0_unphantom ( self . 1 . clone ( ) ) ) ;
475
+ let Ok ( buf) = RawVec :: with_capacity_in ( len, stage0_unphantom ( self . 1 . clone ( ) ) ) ;
456
476
unsafe {
457
477
ptr:: copy_nonoverlapping ( self . as_ptr ( ) , buf. ptr ( ) , len) ;
458
478
from_boxed_utf8_unchecked ( buf. into_box ( ) )
459
479
}
460
480
}
461
481
}
462
482
483
+ /// Just the contents are compared, the allocator is ignored
463
484
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
464
485
impl < T : ?Sized + PartialEq , A > PartialEq for Box < T , A > {
465
486
#[ inline]
@@ -471,6 +492,7 @@ impl<T: ?Sized + PartialEq, A> PartialEq for Box<T, A> {
471
492
PartialEq :: ne ( & * * self , & * * other)
472
493
}
473
494
}
495
+ /// Just the contents are compared, the allocator is ignored
474
496
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
475
497
impl < T : ?Sized + PartialOrd , A > PartialOrd for Box < T , A > {
476
498
#[ inline]
@@ -494,23 +516,27 @@ impl<T: ?Sized + PartialOrd, A> PartialOrd for Box<T, A> {
494
516
PartialOrd :: gt ( & * * self , & * * other)
495
517
}
496
518
}
519
+ /// Just the contents are compared, the allocator is ignored
497
520
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
498
521
impl < T : ?Sized + Ord , A > Ord for Box < T , A > {
499
522
#[ inline]
500
523
fn cmp ( & self , other : & Box < T , A > ) -> Ordering {
501
524
Ord :: cmp ( & * * self , & * * other)
502
525
}
503
526
}
527
+ /// Just the contents are compared, the allocator is ignored
504
528
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
505
529
impl < T : ?Sized + Eq , A > Eq for Box < T , A > { }
506
530
531
+ /// Just the contents are compared, the allocator is ignored
507
532
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
508
533
impl < T : ?Sized + Hash , A > Hash for Box < T , A > {
509
534
fn hash < H : Hasher > ( & self , state : & mut H ) {
510
535
( * * self ) . hash ( state) ;
511
536
}
512
537
}
513
538
539
+ /// Just the contents are compared, the allocator is ignored
514
540
#[ stable( feature = "indirect_hasher_impl" , since = "1.22.0" ) ]
515
541
impl < T : ?Sized + Hasher , A > Hasher for Box < T , A > {
516
542
fn finish ( & self ) -> u64 {
@@ -558,7 +584,7 @@ impl<T: ?Sized + Hasher, A> Hasher for Box<T, A> {
558
584
}
559
585
560
586
#[ stable( feature = "from_for_ptrs" , since = "1.6.0" ) ]
561
- impl < T , A : Alloc < Err = AllocErr > + Default > From < T > for Box < T , A > {
587
+ impl < T , A : Alloc < Err = ! > + Default > From < T > for Box < T , A > {
562
588
/// Converts a generic type `T` into a `Box<T>`
563
589
///
564
590
/// The conversion allocates on the heap and moves `t`
@@ -572,7 +598,8 @@ impl<T, A: Alloc<Err = AllocErr> + Default> From<T> for Box<T, A> {
572
598
/// assert_eq!(Box::from(x), boxed);
573
599
/// ```
574
600
fn from ( t : T ) -> Self {
575
- Box :: new_in ( t, A :: default ( ) )
601
+ let Ok ( b) = Box :: new_in ( t, Default :: default ( ) ) ;
602
+ b
576
603
}
577
604
}
578
605
@@ -587,7 +614,7 @@ impl<T: ?Sized, A> From<Box<T, A>> for Pin<Box<T, A>> {
587
614
}
588
615
589
616
#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
590
- impl < T : Copy , A : Alloc < Err = AllocErr > + Default > From < & [ T ] > for Box < [ T ] , A > {
617
+ impl < T : Copy , A : Alloc < Err = ! > + Default > From < & [ T ] > for Box < [ T ] , A > {
591
618
/// Converts a `&[T]` into a `Box<[T]>`
592
619
///
593
620
/// This conversion allocates on the heap
@@ -603,14 +630,15 @@ impl<T: Copy, A: Alloc<Err = AllocErr> + Default> From<&[T]> for Box<[T], A> {
603
630
/// ```
604
631
fn from ( slice : & [ T ] ) -> Box < [ T ] , A > {
605
632
let a = A :: default ( ) ;
606
- let mut boxed = unsafe { RawVec :: with_capacity_in ( slice. len ( ) , a) . into_box ( ) } ;
633
+ let Ok ( vec) = RawVec :: with_capacity_in ( slice. len ( ) , a) ;
634
+ let mut boxed = unsafe { vec. into_box ( ) } ;
607
635
boxed. copy_from_slice ( slice) ;
608
636
boxed
609
637
}
610
638
}
611
639
612
640
#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
613
- impl < A : Alloc < Err = AllocErr > + Default > From < & str > for Box < str , A > {
641
+ impl < A : Alloc < Err = ! > + Default > From < & str > for Box < str , A > {
614
642
/// Converts a `&str` into a `Box<str>`
615
643
///
616
644
/// This conversion allocates on the heap
@@ -682,7 +710,7 @@ impl<A> Box<dyn Any, A> {
682
710
}
683
711
}
684
712
685
- impl < A : Alloc < Err =AllocErr > > Box < dyn Any + Send , A > {
713
+ impl < A : Alloc < Err =! > > Box < dyn Any + Send , A > {
686
714
#[ inline]
687
715
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
688
716
/// Attempt to downcast the box to a concrete type.
@@ -853,9 +881,9 @@ impl<A, F: Fn<A> + ?Sized, Alloc> Fn<A> for Box<F, Alloc> {
853
881
#[ rustc_paren_sugar]
854
882
#[ unstable( feature = "fnbox" ,
855
883
reason = "will be deprecated if and when `Box<FnOnce>` becomes usable" , issue = "28796" ) ]
856
- pub trait FnBox < Args > : FnOnce < Args > {
884
+ pub trait FnBox < Args , A : Alloc = AbortAdapter < Global > > : FnOnce < Args > {
857
885
/// Performs the call operation.
858
- fn call_box ( self : Box < Self > , args : Args ) -> Self :: Output ;
886
+ fn call_box ( self : Box < Self , A > , args : Args ) -> Self :: Output ;
859
887
}
860
888
861
889
//FIXME: Make generic over A along with DispatchFromDyn.
@@ -884,10 +912,12 @@ impl<A> FromIterator<A> for Box<[A]> {
884
912
}
885
913
886
914
#[ stable( feature = "box_slice_clone" , since = "1.3.0" ) ]
887
- impl < T : Clone , A : Alloc < Err = AllocErr > + Clone > Clone for Box < [ T ] , A > {
915
+ impl < T : Clone , A : Alloc < Err = ! > + Clone > Clone for Box < [ T ] , A > {
888
916
fn clone ( & self ) -> Self {
917
+ let Ok ( b) = RawVec :: with_capacity_in ( self . len ( ) , self . 1 . clone ( ) ) ;
918
+
889
919
let mut new = BoxBuilder {
890
- data : RawVec :: with_capacity_in ( self . len ( ) , stage0_unphantom ( self . 1 . clone ( ) ) ) ,
920
+ data : b ,
891
921
len : 0 ,
892
922
} ;
893
923
@@ -905,20 +935,20 @@ impl<T: Clone, A: Alloc<Err = AllocErr> + Clone> Clone for Box<[T], A> {
905
935
return unsafe { new. into_box ( ) } ;
906
936
907
937
// Helper type for responding to panics correctly.
908
- struct BoxBuilder < T , A : Alloc < Err = AllocErr > + AllocHelper < Err = AllocErr > > {
938
+ struct BoxBuilder < T , A : Alloc > {
909
939
data : RawVec < T , A > ,
910
940
len : usize ,
911
941
}
912
942
913
- impl < T , A : Alloc < Err = AllocErr > > BoxBuilder < T , A > {
943
+ impl < T , A : Alloc > BoxBuilder < T , A > {
914
944
unsafe fn into_box ( self ) -> Box < [ T ] , A > {
915
945
let raw = ptr:: read ( & self . data ) ;
916
946
mem:: forget ( self ) ;
917
947
raw. into_box ( )
918
948
}
919
949
}
920
950
921
- impl < T , A : Alloc < Err = AllocErr > > Drop for BoxBuilder < T , A > {
951
+ impl < T , A : Alloc > Drop for BoxBuilder < T , A > {
922
952
fn drop ( & mut self ) {
923
953
let mut data = self . data . ptr ( ) ;
924
954
let max = unsafe { data. add ( self . len ) } ;
0 commit comments