Touch up where `Alloc` is imported from

pull/31/head
Alex Crichton 7 years ago
parent 2e67cfed79
commit 56d90fa3c3

@ -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> }
@ -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> {
@ -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> {

@ -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>,
@ -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,

Loading…
Cancel
Save