1515#![ no_core]
1616#![ allow( dead_code, internal_features, ambiguous_wide_pointer_comparisons) ]
1717
18+ #[ lang = "pointee_trait" ]
19+ pub trait Pointee : PointeeSized {
20+ #[ lang = "metadata_type" ]
21+ // needed so that layout_of will return `TooGeneric` instead of `Unknown`
22+ // when asked for the layout of `*const T`. Which is important for making
23+ // transmutes between raw pointers (and especially pattern types of raw pointers)
24+ // work.
25+ type Metadata : Copy + Sync + Unpin + Freeze ;
26+ }
27+
28+ #[ lang = "dyn_metadata" ]
29+ pub struct DynMetadata < Dyn : PointeeSized > {
30+ _vtable_ptr : NonNull < VTable > ,
31+ _phantom : PhantomData < Dyn > ,
32+ }
33+
34+ unsafe extern "C" {
35+ /// Opaque type for accessing vtables.
36+ ///
37+ /// Private implementation detail of `DynMetadata::size_of` etc.
38+ /// There is conceptually not actually any Abstract Machine memory behind this pointer.
39+ type VTable ;
40+ }
41+
1842#[ no_mangle]
1943unsafe extern "C" fn _Unwind_Resume ( ) {
2044 intrinsics:: unreachable ( ) ;
@@ -113,7 +137,7 @@ unsafe impl<'a, T: PointeeSized> Sync for &'a T {}
113137unsafe impl Sync for [ u8 ; 16 ] { }
114138
115139#[ lang = "freeze" ]
116- unsafe auto trait Freeze { }
140+ pub unsafe auto trait Freeze { }
117141
118142unsafe impl < T : PointeeSized > Freeze for PhantomData < T > { }
119143unsafe impl < T : PointeeSized > Freeze for * const T { }
@@ -592,6 +616,13 @@ macro_rules! pattern_type {
592616 } ;
593617}
594618
619+ impl < T : PointeeSized , U : PointeeSized > CoerceUnsized < pattern_type ! ( * const U is !null) > for pattern_type ! ( * const T is !null) where
620+ T : Unsize < U >
621+ {
622+ }
623+
624+ impl < T : DispatchFromDyn < U > , U > DispatchFromDyn < pattern_type ! ( U is !null) > for pattern_type ! ( T is !null) { }
625+
595626impl < T : PointeeSized , U : PointeeSized > CoerceUnsized < NonNull < U > > for NonNull < T > where T : Unsize < U > { }
596627impl < T : PointeeSized , U : PointeeSized > DispatchFromDyn < NonNull < U > > for NonNull < T > where T : Unsize < U > { }
597628
@@ -604,26 +635,37 @@ impl<T: PointeeSized, U: PointeeSized> CoerceUnsized<Unique<U>> for Unique<T> wh
604635impl < T : PointeeSized , U : PointeeSized > DispatchFromDyn < Unique < U > > for Unique < T > where T : Unsize < U > { }
605636
606637#[ lang = "owned_box" ]
607- pub struct Box < T : ?Sized , A : Allocator = Global > ( Unique < T > , A ) ;
638+ pub struct Box < T : ?Sized , A = Global > ( Unique < T > , A ) ;
608639
609- impl < T : ?Sized + Unsize < U > , U : ?Sized , A : Allocator > CoerceUnsized < Box < U , A > > for Box < T , A > { }
640+ impl < T : ?Sized + Unsize < U > , U : ?Sized > CoerceUnsized < Box < U > > for Box < T > { }
610641
611642impl < T > Box < T > {
612643 pub fn new ( val : T ) -> Box < T > {
613644 unsafe {
614645 let size = size_of :: < T > ( ) ;
615646 let ptr = libc:: malloc ( size) ;
616647 intrinsics:: copy ( & val as * const T as * const u8 , ptr, size) ;
617- Box ( Unique { pointer : NonNull ( ptr as * const T ) , _marker : PhantomData } , Global )
648+ Box (
649+ Unique {
650+ pointer : NonNull ( intrinsics:: transmute :: <
651+ * mut u8 ,
652+ pattern_type ! ( * const T is !null) ,
653+ > ( ptr) ) ,
654+ _marker : PhantomData ,
655+ } ,
656+ Global ,
657+ )
618658 }
619659 }
620660}
621661
622- impl < T : ?Sized , A : Allocator > Drop for Box < T , A > {
662+ impl < T : ?Sized , A > Drop for Box < T , A > {
623663 fn drop ( & mut self ) {
624- // inner value is dropped by compiler.
664+ // inner value is dropped by compiler
625665 unsafe {
626- libc:: free ( self . 0 . pointer . 0 as * mut u8 ) ;
666+ libc:: free ( intrinsics:: transmute :: < pattern_type ! ( * const T is !null) , * const T > (
667+ self . 0 . pointer . 0 ,
668+ ) as * mut u8 ) ;
627669 }
628670 }
629671}
0 commit comments