diff options
| author | Antoni Boucher <bouanto@zoho.com> | 2023-06-19 18:51:02 -0400 |
|---|---|---|
| committer | Antoni Boucher <bouanto@zoho.com> | 2023-06-19 18:51:02 -0400 |
| commit | 38c16e9862951e3f7235c960abe0539338606e48 (patch) | |
| tree | 1c1c55146224e770b07d7544feeab91809f31e71 /example | |
| parent | 876a7d8d6fa1c18b8a652836b22f0d362784c685 (diff) | |
| download | rust-38c16e9862951e3f7235c960abe0539338606e48.tar.gz rust-38c16e9862951e3f7235c960abe0539338606e48.zip | |
Merge commit '1bbee3e217d75e7bc3bfe5d8c1b35e776fce96e6' into sync-cg_gcc-2023-06-19
Diffstat (limited to 'example')
| -rw-r--r-- | example/mini_core.rs | 20 | ||||
| -rw-r--r-- | example/mini_core_hello_world.rs | 3 | ||||
| -rw-r--r-- | example/std_example.rs | 1 |
3 files changed, 22 insertions, 2 deletions
diff --git a/example/mini_core.rs b/example/mini_core.rs index c27b610f2ab..0cd7e6047c2 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -451,6 +451,9 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) { drop_in_place(to_drop); } +#[lang = "unpin"] +pub auto trait Unpin {} + #[lang = "deref"] pub trait Deref { type Target: ?Sized; @@ -488,10 +491,23 @@ pub struct Box<T: ?Sized, A: Allocator = Global>(Unique<T>, A); impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {} +impl<T> Box<T> { + pub fn new(val: T) -> Box<T> { + unsafe { + let size = intrinsics::size_of::<T>(); + let ptr = libc::malloc(size); + intrinsics::copy(&val as *const T as *const u8, ptr, size); + Box(Unique { pointer: NonNull(ptr as *const T), _marker: PhantomData }, Global) + } + } +} + impl<T: ?Sized, A: Allocator> Drop for Box<T, A> { fn drop(&mut self) { - // inner value is dropped by compiler - libc::free(self.pointer.0 as *mut u8); + // inner value is dropped by compiler. + unsafe { + libc::free(self.0.pointer.0 as *mut u8); + } } } diff --git a/example/mini_core_hello_world.rs b/example/mini_core_hello_world.rs index cff26077740..b93d6859706 100644 --- a/example/mini_core_hello_world.rs +++ b/example/mini_core_hello_world.rs @@ -168,6 +168,9 @@ fn main() { world as Box<dyn SomeTrait>; assert_eq!(intrinsics::bitreverse(0b10101000u8), 0b00010101u8); + assert_eq!(intrinsics::bitreverse(0xddccu16), 0x33bbu16); + assert_eq!(intrinsics::bitreverse(0xffee_ddccu32), 0x33bb77ffu32); + assert_eq!(intrinsics::bitreverse(0x1234_5678_ffee_ddccu64), 0x33bb77ff1e6a2c48u64); assert_eq!(intrinsics::bswap(0xabu8), 0xabu8); assert_eq!(intrinsics::bswap(0xddccu16), 0xccddu16); diff --git a/example/std_example.rs b/example/std_example.rs index 5c171c49fd1..18f2ddcde12 100644 --- a/example/std_example.rs +++ b/example/std_example.rs @@ -58,6 +58,7 @@ fn main() { assert_eq!(0b0000000000000000000000000010000010000000000000000000000000000000_0000000000100000000000000000000000001000000000000100000000000000u128.leading_zeros(), 26); assert_eq!(0b0000000000000000000000000010000000000000000000000000000000000000_0000000000000000000000000000000000001000000000000000000010000000u128.trailing_zeros(), 7); + assert_eq!(0x1234_5678_ffee_ddcc_1234_5678_ffee_ddccu128.reverse_bits(), 0x33bb77ff1e6a2c4833bb77ff1e6a2c48u128); let _d = 0i128.checked_div(2i128); let _d = 0u128.checked_div(2u128); |
