Skip to content

Commit 6f60071

Browse files
committed
Introduce Box::new in mini_core
1 parent b42358a commit 6f60071

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

example/mini_core.rs

+11
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,17 @@ pub struct Box<T: ?Sized>(Unique<T>, ());
518518

519519
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
520520

521+
impl<T> Box<T> {
522+
pub fn new(val: T) -> Box<T> {
523+
unsafe {
524+
let size = intrinsics::size_of::<T>();
525+
let ptr = libc::malloc(size);
526+
intrinsics::copy(&val as *const T as *const u8, ptr, size);
527+
Box(Unique { pointer: NonNull(ptr as *const T), _marker: PhantomData }, ())
528+
}
529+
}
530+
}
531+
521532
impl<T: ?Sized> Drop for Box<T> {
522533
fn drop(&mut self) {
523534
// drop is currently performed by compiler.

0 commit comments

Comments
 (0)