aboutsummaryrefslogtreecommitdiffstats
path: root/src/guard.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/guard.rs')
-rw-r--r--src/guard.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/guard.rs b/src/guard.rs
new file mode 100644
index 0000000..75a28aa
--- /dev/null
+++ b/src/guard.rs
@@ -0,0 +1,14 @@
+pub fn guard<T> (finally: ~fn (), body: &fn () -> T) -> T {
+ let _guard = Guard { finally: finally };
+ body()
+}
+
+struct Guard {
+ priv finally: ~fn (),
+}
+
+impl Drop for Guard {
+ fn finalize (&self) {
+ (self.finally)();
+ }
+}