@@ -136,13 +136,13 @@ use core::fmt;
136
136
use core:: future:: Future ;
137
137
use core:: hash:: { Hash , Hasher } ;
138
138
use core:: iter:: { FromIterator , FusedIterator , Iterator } ;
139
- use core:: marker:: { Unpin , Unsize } ;
139
+ use core:: marker:: { PhantomData , Unpin , Unsize } ;
140
140
use core:: mem;
141
141
use core:: ops:: {
142
142
CoerceUnsized , Deref , DerefMut , DispatchFromDyn , Generator , GeneratorState , Receiver ,
143
143
} ;
144
144
use core:: pin:: Pin ;
145
- use core:: ptr:: { self , NonNull , Unique } ;
145
+ use core:: ptr:: { self , NonNull } ;
146
146
use core:: task:: { Context , Poll } ;
147
147
148
148
use crate :: alloc:: { self , AllocInit , AllocRef , Global } ;
@@ -156,7 +156,26 @@ use crate::vec::Vec;
156
156
#[ lang = "owned_box" ]
157
157
#[ fundamental]
158
158
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
159
- pub struct Box < T : ?Sized > ( Unique < T > ) ;
159
+ pub struct Box < T : ?Sized > ( BoxPtr < T > ) ;
160
+
161
+ // FIXME: remove this struct and give `Box` two fields.
162
+ // Currently this causes an ICE with 'assertion failed: sig.c_variadic || extra_args.is_empty()'
163
+ struct BoxPtr < T : ?Sized > {
164
+ ptr : NonNull < T > ,
165
+
166
+ // NOTE: this marker has no consequences for variance, but is necessary
167
+ // for dropck to understand that we logically own a `T`.
168
+ //
169
+ // For details, see:
170
+ // https://github.com/rust-lang/rfcs/blob/master/text/0769-sound-generic-drop.md#phantom-data
171
+ _phantom : PhantomData < T > ,
172
+ }
173
+
174
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
175
+ unsafe impl < T : Send + ?Sized > Send for Box < T > { }
176
+
177
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
178
+ unsafe impl < T : Sync + ?Sized > Sync for Box < T > { }
160
179
161
180
impl < T > Box < T > {
162
181
/// Allocates memory on the heap and then places `x` into it.
@@ -382,7 +401,7 @@ impl<T: ?Sized> Box<T> {
382
401
#[ stable( feature = "box_raw" , since = "1.4.0" ) ]
383
402
#[ inline]
384
403
pub unsafe fn from_raw ( raw : * mut T ) -> Self {
385
- Box ( Unique :: new_unchecked ( raw) )
404
+ Box ( BoxPtr { ptr : NonNull :: new_unchecked ( raw) , _phantom : PhantomData } )
386
405
}
387
406
388
407
/// Consumes the `Box`, returning a wrapped raw pointer.
@@ -462,22 +481,14 @@ impl<T: ?Sized> Box<T> {
462
481
#[ unstable( feature = "box_into_raw_non_null" , issue = "47336" ) ]
463
482
#[ inline]
464
483
pub fn into_raw_non_null ( b : Box < T > ) -> NonNull < T > {
465
- Box :: into_unique ( b) . into ( )
466
- }
467
-
468
- #[ unstable( feature = "ptr_internals" , issue = "none" , reason = "use into_raw_non_null instead" ) ]
469
- #[ inline]
470
- #[ doc( hidden) ]
471
- pub fn into_unique ( b : Box < T > ) -> Unique < T > {
472
- let b = mem:: ManuallyDrop :: new ( b) ;
473
- let mut unique = b. 0 ;
484
+ let mut b = mem:: ManuallyDrop :: new ( b) ;
474
485
// Box is kind-of a library type, but recognized as a "unique pointer" by
475
486
// Stacked Borrows. This function here corresponds to "reborrowing to
476
487
// a raw pointer", but there is no actual reborrow here -- so
477
488
// without some care, the pointer we are returning here still carries
478
489
// the tag of `b`, with `Unique` permission.
479
490
// We round-trip through a mutable reference to avoid that.
480
- unsafe { Unique :: new_unchecked ( unique . as_mut ( ) as * mut T ) }
491
+ unsafe { NonNull :: new_unchecked ( b . 0 . ptr . as_mut ( ) as * mut T ) }
481
492
}
482
493
483
494
/// Consumes and leaks the `Box`, returning a mutable reference,
@@ -910,7 +921,7 @@ impl<T: fmt::Debug + ?Sized> fmt::Debug for Box<T> {
910
921
impl < T : ?Sized > fmt:: Pointer for Box < T > {
911
922
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
912
923
// It's not possible to extract the inner Uniq directly from the Box,
913
- // instead we cast it to a *const which aliases the Unique
924
+ // instead we cast it to a *const which aliases the NonNull
914
925
let ptr: * const T = & * * self ;
915
926
fmt:: Pointer :: fmt ( & ptr, f)
916
927
}
@@ -1025,9 +1036,11 @@ impl<A, F: Fn<A> + ?Sized> Fn<A> for Box<F> {
1025
1036
1026
1037
#[ unstable( feature = "coerce_unsized" , issue = "27732" ) ]
1027
1038
impl < T : ?Sized + Unsize < U > , U : ?Sized > CoerceUnsized < Box < U > > for Box < T > { }
1039
+ impl < T : ?Sized + Unsize < U > , U : ?Sized > CoerceUnsized < BoxPtr < U > > for BoxPtr < T > { }
1028
1040
1029
1041
#[ unstable( feature = "dispatch_from_dyn" , issue = "none" ) ]
1030
1042
impl < T : ?Sized + Unsize < U > , U : ?Sized > DispatchFromDyn < Box < U > > for Box < T > { }
1043
+ impl < T : ?Sized + Unsize < U > , U : ?Sized > DispatchFromDyn < BoxPtr < U > > for BoxPtr < T > { }
1031
1044
1032
1045
#[ stable( feature = "boxed_slice_from_iter" , since = "1.32.0" ) ]
1033
1046
impl < A > FromIterator < A > for Box < [ A ] > {
0 commit comments