2013-01-25 19:51:53 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-01-15 19:11:16 -06:00
|
|
|
/*!
|
|
|
|
The Finally trait provides a method, `finally` on
|
|
|
|
stack closures that emulates Java-style try/finally blocks.
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
2013-09-23 19:20:36 -05:00
|
|
|
```
|
2013-01-15 19:11:16 -06:00
|
|
|
do || {
|
|
|
|
...
|
|
|
|
}.finally {
|
2013-08-16 00:41:28 -05:00
|
|
|
always_run_this();
|
2013-01-15 19:11:16 -06:00
|
|
|
}
|
2013-09-23 19:20:36 -05:00
|
|
|
```
|
2013-01-15 19:11:16 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
use ops::Drop;
|
|
|
|
|
2013-04-11 02:56:24 -05:00
|
|
|
#[cfg(test)] use task::{failing, spawn};
|
2013-02-28 10:57:33 -06:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
pub trait Finally<T> {
|
2013-11-18 23:15:42 -06:00
|
|
|
fn finally(&self, dtor: ||) -> T;
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
|
|
|
|
2013-06-11 01:13:04 -05:00
|
|
|
macro_rules! finally_fn {
|
|
|
|
($fnty:ty) => {
|
|
|
|
impl<T> Finally<T> for $fnty {
|
2013-11-18 23:15:42 -06:00
|
|
|
fn finally(&self, dtor: ||) -> T {
|
2013-06-11 01:13:04 -05:00
|
|
|
let _d = Finallyalizer {
|
|
|
|
dtor: dtor
|
|
|
|
};
|
|
|
|
(*self)()
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 19:11:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 01:13:04 -05:00
|
|
|
impl<'self,T> Finally<T> for &'self fn() -> T {
|
2013-11-18 23:15:42 -06:00
|
|
|
fn finally(&self, dtor: ||) -> T {
|
2013-04-11 02:56:24 -05:00
|
|
|
let _d = Finallyalizer {
|
|
|
|
dtor: dtor
|
|
|
|
};
|
|
|
|
|
|
|
|
(*self)()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 01:13:04 -05:00
|
|
|
finally_fn!(extern "Rust" fn() -> T)
|
2013-04-11 02:56:24 -05:00
|
|
|
|
2013-03-22 17:52:50 -05:00
|
|
|
struct Finallyalizer<'self> {
|
2013-03-14 13:22:51 -05:00
|
|
|
dtor: &'self fn()
|
2013-01-15 19:11:16 -06:00
|
|
|
}
|
|
|
|
|
2013-03-20 20:18:57 -05:00
|
|
|
#[unsafe_destructor]
|
2013-03-22 17:52:50 -05:00
|
|
|
impl<'self> Drop for Finallyalizer<'self> {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2013-01-15 19:11:16 -06:00
|
|
|
(self.dtor)();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_success() {
|
|
|
|
let mut i = 0;
|
|
|
|
do (|| {
|
|
|
|
i = 10;
|
|
|
|
}).finally {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!failing());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(i, 10);
|
2013-01-15 19:11:16 -06:00
|
|
|
i = 20;
|
|
|
|
}
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(i, 20);
|
2013-01-15 19:11:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_fail() {
|
|
|
|
let mut i = 0;
|
|
|
|
do (|| {
|
|
|
|
i = 10;
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!();
|
2013-01-15 19:11:16 -06:00
|
|
|
}).finally {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(failing());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(i, 10);
|
2013-01-15 19:11:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_retval() {
|
2013-11-18 23:15:42 -06:00
|
|
|
let closure: || -> int = || 10;
|
2013-03-01 16:32:37 -06:00
|
|
|
let i = do closure.finally { };
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(i, 10);
|
2013-01-15 19:11:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_compact() {
|
2013-06-11 01:13:04 -05:00
|
|
|
fn do_some_fallible_work() {}
|
2013-01-15 19:11:16 -06:00
|
|
|
fn but_always_run_this_function() { }
|
|
|
|
do_some_fallible_work.finally(
|
|
|
|
but_always_run_this_function);
|
2013-01-31 20:24:09 -06:00
|
|
|
}
|
2013-04-11 02:56:24 -05:00
|
|
|
|