From 0c7ba3ccbea76fe975343c4ef98f8579206e1460 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 10 Jun 2013 23:13:04 -0700 Subject: [PATCH] Add a finally impl for `extern "Rust" fn()` --- src/libstd/unstable/finally.rs | 38 ++++++++++++++-------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/libstd/unstable/finally.rs b/src/libstd/unstable/finally.rs index 5001fb421cd86..b15bceddb1c21 100644 --- a/src/libstd/unstable/finally.rs +++ b/src/libstd/unstable/finally.rs @@ -31,17 +31,20 @@ pub trait Finally { fn finally(&self, dtor: &fn()) -> T; } -impl<'self,T> Finally for &'self fn() -> T { - fn finally(&self, dtor: &fn()) -> T { - let _d = Finallyalizer { - dtor: dtor - }; - - (*self)() +macro_rules! finally_fn { + ($fnty:ty) => { + impl Finally for $fnty { + fn finally(&self, dtor: &fn()) -> T { + let _d = Finallyalizer { + dtor: dtor + }; + (*self)() + } + } } } -impl Finally for ~fn() -> T { +impl<'self,T> Finally for &'self fn() -> T { fn finally(&self, dtor: &fn()) -> T { let _d = Finallyalizer { dtor: dtor @@ -51,15 +54,9 @@ impl Finally for ~fn() -> T { } } -impl Finally 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() @@ -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); @@ -136,4 +130,4 @@ fn test_managed() { }; assert_eq!(do managed.finally {}, 10); assert_eq!(*i, 20); -} \ No newline at end of file +}