libarena: use unboxed closures

This commit is contained in:
Jorge Aparicio 2014-12-07 11:11:15 -05:00
parent 341e7bc08b
commit 1c5aac2b30

View File

@ -28,6 +28,7 @@
html_root_url = "http://doc.rust-lang.org/nightly/")] html_root_url = "http://doc.rust-lang.org/nightly/")]
#![feature(unsafe_destructor)] #![feature(unsafe_destructor)]
#![feature(unboxed_closures)]
#![allow(missing_docs)] #![allow(missing_docs)]
extern crate alloc; extern crate alloc;
@ -209,7 +210,7 @@ impl Arena {
} }
#[inline] #[inline]
fn alloc_copy<T>(&self, op: || -> T) -> &mut T { fn alloc_copy<T, F>(&self, op: F) -> &mut T where F: FnOnce() -> T {
unsafe { unsafe {
let ptr = self.alloc_copy_inner(mem::size_of::<T>(), let ptr = self.alloc_copy_inner(mem::size_of::<T>(),
mem::min_align_of::<T>()); mem::min_align_of::<T>());
@ -263,7 +264,7 @@ impl Arena {
} }
#[inline] #[inline]
fn alloc_noncopy<T>(&self, op: || -> T) -> &mut T { fn alloc_noncopy<T, F>(&self, op: F) -> &mut T where F: FnOnce() -> T {
unsafe { unsafe {
let tydesc = get_tydesc::<T>(); let tydesc = get_tydesc::<T>();
let (ty_ptr, ptr) = let (ty_ptr, ptr) =
@ -287,7 +288,7 @@ impl Arena {
/// Allocates a new item in the arena, using `op` to initialize the value, /// Allocates a new item in the arena, using `op` to initialize the value,
/// and returns a reference to it. /// and returns a reference to it.
#[inline] #[inline]
pub fn alloc<T>(&self, op: || -> T) -> &mut T { pub fn alloc<T, F>(&self, op: F) -> &mut T where F: FnOnce() -> T {
unsafe { unsafe {
if intrinsics::needs_drop::<T>() { if intrinsics::needs_drop::<T>() {
self.alloc_noncopy(op) self.alloc_noncopy(op)
@ -339,7 +340,7 @@ fn test_arena_destructors_fail() {
arena.alloc(|| { [0u8, 1u8, 2u8] }); arena.alloc(|| { [0u8, 1u8, 2u8] });
} }
// Now, panic while allocating // Now, panic while allocating
arena.alloc::<Rc<int>>(|| { arena.alloc::<Rc<int>, _>(|| {
panic!(); panic!();
}); });
} }