Skip to content

Commit

Permalink
auto merge of #5839 : bjz/rust/master, r=brson
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Apr 13, 2013
2 parents a9247e0 + 5f59012 commit 65ff441
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/libcore/unstable/finally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ do || {

use ops::Drop;

#[cfg(test)] use task::failing;
#[cfg(test)] use task::{failing, spawn};

pub trait Finally<T> {
fn finally(&self, dtor: &fn()) -> T;
Expand All @@ -41,6 +41,26 @@ impl<'self,T> Finally<T> for &'self fn() -> T {
}
}

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

(*self)()
}
}

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

(*self)()
}
}

struct Finallyalizer<'self> {
dtor: &'self fn()
}
Expand Down Expand Up @@ -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);
}

0 comments on commit 65ff441

Please sign in to comment.