Skip to content

Commit d2b7a2e

Browse files
committed
Fix minicore
1 parent 228e254 commit d2b7a2e

1 file changed

Lines changed: 49 additions & 7 deletions

File tree

example/mini_core.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@
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]
1943
unsafe extern "C" fn _Unwind_Resume() {
2044
intrinsics::unreachable();
@@ -113,7 +137,7 @@ unsafe impl<'a, T: PointeeSized> Sync for &'a T {}
113137
unsafe impl Sync for [u8; 16] {}
114138

115139
#[lang = "freeze"]
116-
unsafe auto trait Freeze {}
140+
pub unsafe auto trait Freeze {}
117141

118142
unsafe impl<T: PointeeSized> Freeze for PhantomData<T> {}
119143
unsafe 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+
595626
impl<T: PointeeSized, U: PointeeSized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
596627
impl<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
604635
impl<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

611642
impl<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

Comments
 (0)