diff options
| author | antoyo <antoyo@users.noreply.github.com> | 2022-08-27 12:09:37 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-27 12:09:37 -0400 |
| commit | b4626b3ca07c4fcdce1d96840567005e294e5ecc (patch) | |
| tree | 3fb39322d5717e88333d80eb6ca4dd996f1688a0 /example | |
| parent | b4eb2c30a262763d59711d285979facc95e1ba56 (diff) | |
| parent | 61a7b96c800c7f0a338b3f8f4b5b1e89f82b1668 (diff) | |
| download | rust-b4626b3ca07c4fcdce1d96840567005e294e5ecc.tar.gz rust-b4626b3ca07c4fcdce1d96840567005e294e5ecc.zip | |
Merge pull request #209 from rust-lang/2022-08-26_sync_from_rust
2022/08/26 sync from rust
Diffstat (limited to 'example')
| -rw-r--r-- | example/alloc_system.rs | 2 | ||||
| -rw-r--r-- | example/mini_core.rs | 55 | ||||
| -rw-r--r-- | example/mini_core_hello_world.rs | 7 |
3 files changed, 48 insertions, 16 deletions
diff --git a/example/alloc_system.rs b/example/alloc_system.rs index 5f66ca67f2d..89661918d05 100644 --- a/example/alloc_system.rs +++ b/example/alloc_system.rs @@ -156,7 +156,7 @@ mod platform { struct Header(*mut u8); const HEAP_ZERO_MEMORY: DWORD = 0x00000008; unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header { - &mut *(ptr as *mut Header).offset(-1) + &mut *(ptr as *mut Header).sub(1) } unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 { let aligned = ptr.add(align - (ptr as usize & (align - 1))); diff --git a/example/mini_core.rs b/example/mini_core.rs index a8435287d9f..b23ecda35d3 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -1,6 +1,6 @@ #![feature( no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types, - untagged_unions, decl_macro, rustc_attrs, transparent_unions, auto_traits, + decl_macro, rustc_attrs, transparent_unions, auto_traits, thread_local )] #![no_core] @@ -39,14 +39,14 @@ impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {} // *mut T -> *mut U impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {} -impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {} +impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U, ()>> for Box<T, ()> {} #[lang = "receiver"] pub trait Receiver {} impl<T: ?Sized> Receiver for &T {} impl<T: ?Sized> Receiver for &mut T {} -impl<T: ?Sized> Receiver for Box<T> {} +impl<T: ?Sized, A: Allocator> Receiver for Box<T, A> {} #[lang = "copy"] pub unsafe trait Copy {} @@ -411,7 +411,15 @@ pub trait FnMut<Args>: FnOnce<Args> { #[lang = "panic"] #[track_caller] -pub fn panic(_msg: &str) -> ! { +pub fn panic(_msg: &'static str) -> ! { + unsafe { + libc::puts("Panicking\n\0" as *const str as *const u8); + intrinsics::abort(); + } +} + +#[lang = "panic_no_unwind"] +fn panic_no_unwind() -> ! { unsafe { libc::puts("Panicking\n\0" as *const str as *const u8); intrinsics::abort(); @@ -450,17 +458,32 @@ pub trait Deref { pub trait Allocator { } +impl Allocator for () {} + pub struct Global; impl Allocator for Global {} +#[repr(transparent)] +#[rustc_layout_scalar_valid_range_start(1)] +#[rustc_nonnull_optimization_guaranteed] +pub struct NonNull<T: ?Sized>(pub *const T); + +impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> {} +impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> {} + +pub struct Unique<T: ?Sized> { + pub pointer: NonNull<T>, + pub _marker: PhantomData<T>, +} + +impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {} +impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Unique<U>> for Unique<T> where T: Unsize<U> {} + #[lang = "owned_box"] -pub struct Box< - T: ?Sized, - A: Allocator = Global, ->(*mut T, A); +pub struct Box<T: ?Sized, A: Allocator = Global>(Unique<T>, A); -impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {} +impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {} impl<T: ?Sized, A: Allocator> Drop for Box<T, A> { fn drop(&mut self) { @@ -468,7 +491,7 @@ impl<T: ?Sized, A: Allocator> Drop for Box<T, A> { } } -impl<T> Deref for Box<T> { +impl<T: ?Sized, A: Allocator> Deref for Box<T, A> { type Target = T; fn deref(&self) -> &Self::Target { @@ -482,8 +505,8 @@ unsafe fn allocate(size: usize, _align: usize) -> *mut u8 { } #[lang = "box_free"] -unsafe fn box_free<T: ?Sized, A: Allocator>(ptr: *mut T, alloc: A) { - libc::free(ptr as *mut u8); +unsafe fn box_free<T: ?Sized>(ptr: Unique<T>, _alloc: ()) { + libc::free(ptr.pointer.0 as *mut u8); } #[lang = "drop"] @@ -505,16 +528,18 @@ pub union MaybeUninit<T> { } pub mod intrinsics { + use crate::Sized; + extern "rust-intrinsic" { pub fn abort() -> !; pub fn size_of<T>() -> usize; - pub fn size_of_val<T: ?::Sized>(val: *const T) -> usize; + pub fn size_of_val<T: ?Sized>(val: *const T) -> usize; pub fn min_align_of<T>() -> usize; - pub fn min_align_of_val<T: ?::Sized>(val: *const T) -> usize; + pub fn min_align_of_val<T: ?Sized>(val: *const T) -> usize; pub fn copy<T>(src: *const T, dst: *mut T, count: usize); pub fn transmute<T, U>(e: T) -> U; pub fn ctlz_nonzero<T>(x: T) -> T; - pub fn needs_drop<T>() -> bool; + pub fn needs_drop<T: ?Sized>() -> bool; pub fn bitreverse<T>(x: T) -> T; pub fn bswap<T>(x: T) -> T; pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize); diff --git a/example/mini_core_hello_world.rs b/example/mini_core_hello_world.rs index 69d591565ac..14fd9eeffa6 100644 --- a/example/mini_core_hello_world.rs +++ b/example/mini_core_hello_world.rs @@ -47,6 +47,11 @@ struct NoisyDrop { inner: NoisyDropInner, } +struct NoisyDropUnsized { + inner: NoisyDropInner, + text: str, +} + struct NoisyDropInner; impl Drop for NoisyDrop { @@ -184,7 +189,9 @@ fn main() { assert_eq!(intrinsics::min_align_of_val(&a) as u8, intrinsics::min_align_of::<&str>() as u8); assert!(!intrinsics::needs_drop::<u8>()); + assert!(!intrinsics::needs_drop::<[u8]>()); assert!(intrinsics::needs_drop::<NoisyDrop>()); + assert!(intrinsics::needs_drop::<NoisyDropUnsized>()); Unique { pointer: 0 as *const &str, |
