mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-10 11:06:49 +00:00
libarena: use unboxed closures
This commit is contained in:
parent
341e7bc08b
commit
1c5aac2b30
@ -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!();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user