|
|
@ -28,7 +28,7 @@ For instance, a custom implementation of `Box` might write `Drop` like this:
|
|
|
|
```rust
|
|
|
|
```rust
|
|
|
|
#![feature(ptr_internals, allocator_api, unique)]
|
|
|
|
#![feature(ptr_internals, allocator_api, unique)]
|
|
|
|
|
|
|
|
|
|
|
|
use std::heap::{Heap, Alloc, Layout};
|
|
|
|
use std::alloc::{Global, GlobalAlloc, Layout, Void};
|
|
|
|
use std::mem;
|
|
|
|
use std::mem;
|
|
|
|
use std::ptr::{drop_in_place, Unique};
|
|
|
|
use std::ptr::{drop_in_place, Unique};
|
|
|
|
|
|
|
|
|
|
|
@ -38,7 +38,7 @@ impl<T> Drop for Box<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
drop_in_place(self.ptr.as_ptr());
|
|
|
|
drop_in_place(self.ptr.as_ptr());
|
|
|
|
Heap.dealloc(self.ptr.as_ptr() as *mut u8, Layout::new::<T>())
|
|
|
|
Global.dealloc(self.ptr.as_ptr() as *mut Void, Layout::new::<T>())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -54,7 +54,7 @@ However this wouldn't work:
|
|
|
|
```rust
|
|
|
|
```rust
|
|
|
|
#![feature(allocator_api, ptr_internals, unique)]
|
|
|
|
#![feature(allocator_api, ptr_internals, unique)]
|
|
|
|
|
|
|
|
|
|
|
|
use std::heap::{Heap, Alloc, Layout};
|
|
|
|
use std::alloc::{Global, GlobalAlloc, Layout, Void};
|
|
|
|
use std::ptr::{drop_in_place, Unique};
|
|
|
|
use std::ptr::{drop_in_place, Unique};
|
|
|
|
use std::mem;
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
|
|
|
@ -64,7 +64,7 @@ impl<T> Drop for Box<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
drop_in_place(self.ptr.as_ptr());
|
|
|
|
drop_in_place(self.ptr.as_ptr());
|
|
|
|
Heap.dealloc(self.ptr.as_ptr() as *mut u8, Layout::new::<T>());
|
|
|
|
Global.dealloc(self.ptr.as_ptr() as *mut Void, Layout::new::<T>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -76,7 +76,7 @@ impl<T> Drop for SuperBox<T> {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
// Hyper-optimized: deallocate the box's contents for it
|
|
|
|
// Hyper-optimized: deallocate the box's contents for it
|
|
|
|
// without `drop`ing the contents
|
|
|
|
// without `drop`ing the contents
|
|
|
|
Heap.dealloc(self.my_box.ptr.as_ptr() as *mut u8, Layout::new::<T>());
|
|
|
|
Global.dealloc(self.my_box.ptr.as_ptr() as *mut Void, Layout::new::<T>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -125,7 +125,7 @@ of Self during `drop` is to use an Option:
|
|
|
|
```rust
|
|
|
|
```rust
|
|
|
|
#![feature(allocator_api, ptr_internals, unique)]
|
|
|
|
#![feature(allocator_api, ptr_internals, unique)]
|
|
|
|
|
|
|
|
|
|
|
|
use std::heap::{Alloc, Heap, Layout};
|
|
|
|
use std::alloc::{GlobalAlloc, Global, Layout, Void};
|
|
|
|
use std::ptr::{drop_in_place, Unique};
|
|
|
|
use std::ptr::{drop_in_place, Unique};
|
|
|
|
use std::mem;
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
|
|
|
@ -135,7 +135,7 @@ impl<T> Drop for Box<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
drop_in_place(self.ptr.as_ptr());
|
|
|
|
drop_in_place(self.ptr.as_ptr());
|
|
|
|
Heap.dealloc(self.ptr.as_ptr() as *mut u8, Layout::new::<T>());
|
|
|
|
Global.dealloc(self.ptr.as_ptr() as *mut Void, Layout::new::<T>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -149,7 +149,7 @@ impl<T> Drop for SuperBox<T> {
|
|
|
|
// without `drop`ing the contents. Need to set the `box`
|
|
|
|
// without `drop`ing the contents. Need to set the `box`
|
|
|
|
// field as `None` to prevent Rust from trying to Drop it.
|
|
|
|
// field as `None` to prevent Rust from trying to Drop it.
|
|
|
|
let my_box = self.my_box.take().unwrap();
|
|
|
|
let my_box = self.my_box.take().unwrap();
|
|
|
|
Heap.dealloc(my_box.ptr.as_ptr() as *mut u8, Layout::new::<T>());
|
|
|
|
Global.dealloc(my_box.ptr.as_ptr() as *mut Void, Layout::new::<T>());
|
|
|
|
mem::forget(my_box);
|
|
|
|
mem::forget(my_box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|