Skip to content

Commit

Permalink
Add a finally impl for extern "Rust" fn()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Jun 11, 2013
1 parent df5b0c0 commit 0c7ba3c
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/libstd/unstable/finally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ pub trait Finally<T> {
fn finally(&self, dtor: &fn()) -> T;
}

impl<'self,T> Finally<T> for &'self fn() -> T {
fn finally(&self, dtor: &fn()) -> T {
let _d = Finallyalizer {
dtor: dtor
};

(*self)()
macro_rules! finally_fn {
($fnty:ty) => {
impl<T> Finally<T> for $fnty {
fn finally(&self, dtor: &fn()) -> T {
let _d = Finallyalizer {
dtor: dtor
};
(*self)()
}
}
}
}

impl<T> Finally<T> for ~fn() -> T {
impl<'self,T> Finally<T> for &'self fn() -> T {
fn finally(&self, dtor: &fn()) -> T {
let _d = Finallyalizer {
dtor: dtor
Expand All @@ -51,15 +54,9 @@ impl<T> Finally<T> for ~fn() -> T {
}
}

impl<T> Finally<T> for @fn() -> T {
fn finally(&self, dtor: &fn()) -> T {
let _d = Finallyalizer {
dtor: dtor
};

(*self)()
}
}
finally_fn!(~fn() -> T)
finally_fn!(@fn() -> T)
finally_fn!(extern "Rust" fn() -> T)

struct Finallyalizer<'self> {
dtor: &'self fn()
Expand Down Expand Up @@ -108,10 +105,7 @@ fn test_retval() {

#[test]
fn test_compact() {
// FIXME #4727: Should be able to use a fn item instead
// of a closure for do_some_fallible_work,
// but it's a type error.
let do_some_fallible_work: &fn() = || { };
fn do_some_fallible_work() {}
fn but_always_run_this_function() { }
do_some_fallible_work.finally(
but_always_run_this_function);
Expand All @@ -136,4 +130,4 @@ fn test_managed() {
};
assert_eq!(do managed.finally {}, 10);
assert_eq!(*i, 20);
}
}

1 comment on commit 0c7ba3c

@graydon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+

Please sign in to comment.