From 5f59012cce2675d805e5a6a3af75b76ee015af24 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 11 Apr 2013 17:56:24 +1000 Subject: [PATCH] Implement Finally for ~fn and @fn types --- src/libcore/unstable/finally.rs | 43 ++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/libcore/unstable/finally.rs b/src/libcore/unstable/finally.rs index 4d2daa6f150..559287b5297 100644 --- a/src/libcore/unstable/finally.rs +++ b/src/libcore/unstable/finally.rs @@ -25,7 +25,7 @@ do || { use ops::Drop; -#[cfg(test)] use task::failing; +#[cfg(test)] use task::{failing, spawn}; pub trait Finally { fn finally(&self, dtor: &fn()) -> T; @@ -41,6 +41,26 @@ impl<'self,T> Finally for &'self fn() -> T { } } +impl Finally for ~fn() -> T { + fn finally(&self, dtor: &fn()) -> T { + let _d = Finallyalizer { + dtor: dtor + }; + + (*self)() + } +} + +impl Finally for @fn() -> T { + fn finally(&self, dtor: &fn()) -> T { + let _d = Finallyalizer { + dtor: dtor + }; + + (*self)() + } +} + struct Finallyalizer<'self> { dtor: &'self fn() } @@ -96,3 +116,24 @@ fn test_compact() { do_some_fallible_work.finally( but_always_run_this_function); } + +#[test] +fn test_owned() { + fn spawn_with_finalizer(f: ~fn()) { + do spawn { do f.finally { } } + } + let owned: ~fn() = || { }; + spawn_with_finalizer(owned); +} + +#[test] +fn test_managed() { + let i = @mut 10; + let managed: @fn() -> int = || { + let r = *i; + *i += 10; + r + }; + assert!(do managed.finally {} == 10); + assert!(*i == 20); +} \ No newline at end of file