about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbjorn3 <17426603+bjorn3@users.noreply.github.com>2023-03-15 14:26:03 +0000
committerbjorn3 <17426603+bjorn3@users.noreply.github.com>2023-03-15 14:26:03 +0000
commit6f6007156b2ddd5ceee356db5d79f4491bf011f3 (patch)
tree5b6934af9476154a690b9eb1be4274d3c028203d
parentb42358a23f145688f048145be2ac9fe4f6b36335 (diff)
downloadrust-6f6007156b2ddd5ceee356db5d79f4491bf011f3.tar.gz
rust-6f6007156b2ddd5ceee356db5d79f4491bf011f3.zip
Introduce Box::new in mini_core
-rw-r--r--example/mini_core.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/example/mini_core.rs b/example/mini_core.rs
index 1f9db1eb2a9..73b83b89f6d 100644
--- a/example/mini_core.rs
+++ b/example/mini_core.rs
@@ -518,6 +518,17 @@ pub struct Box<T: ?Sized>(Unique<T>, ());
 
 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
 
+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 }, ())
+        }
+    }
+}
+
 impl<T: ?Sized> Drop for Box<T> {
     fn drop(&mut self) {
         // drop is currently performed by compiler.