Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/destructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ For instance, a custom implementation of `Box` might write `Drop` like this:
```rust
#![feature(ptr_internals, allocator_api, unique)]

use std::alloc::{Global, GlobalAlloc, Layout};
use std::alloc::{Alloc, Global, GlobalAlloc, Layout};
use std::mem;
use std::ptr::{drop_in_place, Unique};
use std::ptr::{drop_in_place, NonNull, Unique};

struct Box<T>{ ptr: Unique<T> }

impl<T> Drop for Box<T> {
fn drop(&mut self) {
unsafe {
drop_in_place(self.ptr.as_ptr());
Global.dealloc(self.ptr.as_ptr() as *mut _, Layout::new::<T>())
let c: NonNull<T> = self.ptr.into();
Global.dealloc(c.cast(), Layout::new::<T>())
}
}
}
Expand All @@ -54,8 +55,8 @@ However this wouldn't work:
```rust
#![feature(allocator_api, ptr_internals, unique)]

use std::alloc::{Global, GlobalAlloc, Layout};
use std::ptr::{drop_in_place, Unique};
use std::alloc::{Alloc, Global, GlobalAlloc, Layout};
use std::ptr::{drop_in_place, Unique, NonNull};
use std::mem;

struct Box<T>{ ptr: Unique<T> }
Expand All @@ -64,7 +65,8 @@ impl<T> Drop for Box<T> {
fn drop(&mut self) {
unsafe {
drop_in_place(self.ptr.as_ptr());
Global.dealloc(self.ptr.as_ptr() as *mut _, Layout::new::<T>());
let c: NonNull<T> = self.ptr.into();
Global.dealloc(c.cast(), Layout::new::<T>());
}
}
}
Expand All @@ -76,7 +78,8 @@ impl<T> Drop for SuperBox<T> {
unsafe {
// Hyper-optimized: deallocate the box's contents for it
// without `drop`ing the contents
Global.dealloc(self.my_box.ptr.as_ptr() as *mut _, Layout::new::<T>());
let c: NonNull<T> = self.my_box.ptr.into();
Global.dealloc(c.cast::<u8>(), Layout::new::<T>());
}
}
}
Expand Down Expand Up @@ -125,8 +128,8 @@ of Self during `drop` is to use an Option:
```rust
#![feature(allocator_api, ptr_internals, unique)]

use std::alloc::{GlobalAlloc, Global, Layout};
use std::ptr::{drop_in_place, Unique};
use std::alloc::{Alloc, GlobalAlloc, Global, Layout};
use std::ptr::{drop_in_place, Unique, NonNull};
use std::mem;

struct Box<T>{ ptr: Unique<T> }
Expand All @@ -135,7 +138,8 @@ impl<T> Drop for Box<T> {
fn drop(&mut self) {
unsafe {
drop_in_place(self.ptr.as_ptr());
Global.dealloc(self.ptr.as_ptr() as *mut _, Layout::new::<T>());
let c: NonNull<T> = self.ptr.into();
Global.dealloc(c.cast(), Layout::new::<T>());
}
}
}
Expand All @@ -149,7 +153,8 @@ impl<T> Drop for SuperBox<T> {
// without `drop`ing the contents. Need to set the `box`
// field as `None` to prevent Rust from trying to Drop it.
let my_box = self.my_box.take().unwrap();
Global.dealloc(my_box.ptr.as_ptr() as *mut _, Layout::new::<T>());
let c: NonNull<T> = my_box.ptr.into();
Global.dealloc(c.cast(), Layout::new::<T>());
mem::forget(my_box);
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/vec-final.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#![feature(allocator_api)]
#![feature(unique)]

use std::ptr::{Unique, self};
use std::ptr::{Unique, NonNull, self};
use std::mem;
use std::ops::{Deref, DerefMut};
use std::marker::PhantomData;
use std::alloc::{GlobalAlloc, Layout, Global, oom};
use std::alloc::{Alloc, GlobalAlloc, Layout, Global, oom};

struct RawVec<T> {
ptr: Unique<T>,
Expand Down Expand Up @@ -38,18 +38,23 @@ impl<T> RawVec<T> {
(1, ptr)
} else {
let new_cap = 2 * self.cap;
let ptr = Global.realloc(self.ptr.as_ptr() as *mut _,
let c: NonNull<T> = self.ptr.into();
let ptr = Global.realloc(c.cast(),
Layout::array::<T>(self.cap).unwrap(),
Layout::array::<T>(new_cap).unwrap().size());
(new_cap, ptr)
};

// If allocate or reallocate fail, oom
if ptr.is_null() {
oom()
if ptr.is_err() {
oom(Layout::from_size_align_unchecked(
new_cap * elem_size,
mem::align_of::<T>(),
))
}
let ptr = ptr.unwrap();

self.ptr = Unique::new_unchecked(ptr as *mut _);
self.ptr = Unique::new_unchecked(ptr.as_ptr() as *mut _);
self.cap = new_cap;
}
}
Expand All @@ -60,7 +65,8 @@ impl<T> Drop for RawVec<T> {
let elem_size = mem::size_of::<T>();
if self.cap != 0 && elem_size != 0 {
unsafe {
Global.dealloc(self.ptr.as_ptr() as *mut _,
let c: NonNull<T> = self.ptr.into();
Global.dealloc(c.cast(),
Layout::array::<T>(self.cap).unwrap());
}
}
Expand Down