Skip to content

Commit

Permalink
Touch up where Alloc is imported from
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Jul 10, 2017
1 parent 2e67cfe commit 56d90fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
26 changes: 7 additions & 19 deletions src/destructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ this is totally fine.
For instance, a custom implementation of `Box` might write `Drop` like this:

```rust
#![feature(alloc, heap_api, unique, allocator_api)]
#![feature(unique, allocator_api)]

extern crate alloc;

use std::ptr::{drop_in_place, Unique};
use std::heap::{Heap, Alloc, Layout};
use std::mem;

use alloc::allocator::{Layout, Alloc};
use alloc::heap::Heap;
use std::ptr::{drop_in_place, Unique};

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

Expand All @@ -56,16 +52,12 @@ use-after-free the `ptr` because when drop exits, it becomes inaccessible.
However this wouldn't work:

```rust
#![feature(alloc, allocator_api, heap_api, unique)]

extern crate alloc;
#![feature(allocator_api, unique)]

use std::heap::{Heap, Alloc, Layout};
use std::ptr::{drop_in_place, Unique};
use std::mem;

use alloc::allocator::{Layout, Alloc};
use alloc::heap::Heap;

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

impl<T> Drop for Box<T> {
Expand Down Expand Up @@ -131,16 +123,12 @@ The classic safe solution to overriding recursive drop and allowing moving out
of Self during `drop` is to use an Option:

```rust
#![feature(alloc, allocator_api, heap_api, unique)]

extern crate alloc;
#![feature(allocator_api, unique)]

use std::heap::{Alloc, Heap, Layout};
use std::ptr::{drop_in_place, Unique};
use std::mem;

use alloc::allocator::{Layout, Alloc};
use alloc::heap::Heap;

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

impl<T> Drop for Box<T> {
Expand Down
15 changes: 4 additions & 11 deletions src/vec-final.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

```rust
#![feature(unique)]
#![feature(alloc, allocator_api, heap_api)]

extern crate alloc;
#![feature(allocator_api)]

use std::ptr::{Unique, self};
use std::mem;
use std::ops::{Deref, DerefMut};
use std::marker::PhantomData;

use alloc::allocator::{Layout, Alloc};
use alloc::heap::Heap;
use std::heap::{Alloc, Layout, Heap};

struct RawVec<T> {
ptr: Unique<T>,
Expand Down Expand Up @@ -64,16 +60,13 @@ impl<T> Drop for RawVec<T> {
let elem_size = mem::size_of::<T>();
if self.cap != 0 && elem_size != 0 {
unsafe {
Heap.dealloc(self.ptr.as_ptr() as *mut _, Layout::array::<T>(self.cap).unwrap());
Heap.dealloc(self.ptr.as_ptr() as *mut _,
Layout::array::<T>(self.cap).unwrap());
}
}
}
}





pub struct Vec<T> {
buf: RawVec<T>,
len: usize,
Expand Down

0 comments on commit 56d90fa

Please sign in to comment.