about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2012-01-13 14:20:56 -0800
committerBrian Anderson <banderson@mozilla.com>2012-01-13 14:21:17 -0800
commite66c036c9f7f6e88e6eda5ddd726d488b180e346 (patch)
tree670fe55c5e1897007c818ac56c40d3de2759c8d6 /src/libcore
parent31bb6a60bb59d40c97ab3f68a313d199429bf4ef (diff)
libcore: Add task::try
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/task.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libcore/task.rs b/src/libcore/task.rs
index afb64da8b6f..b24e27596b5 100644
--- a/src/libcore/task.rs
+++ b/src/libcore/task.rs
@@ -50,6 +50,7 @@ export spawn_connected;
 export connected_fn;
 export connected_task;
 export currently_unwinding;
+export try;
 
 #[abi = "rust-intrinsic"]
 native mod rusti {
@@ -349,6 +350,30 @@ fn currently_unwinding() -> bool {
     rustrt::rust_task_is_unwinding(rustrt::rust_get_task())
 }
 
+/*
+Function: try
+
+Execute a function in another task and return either the return value
+of the function or result::err.
+
+Returns:
+
+If the function executed successfully then try returns result::ok
+containing the value returned by the function. If the function fails
+then try returns result::err containing nil.
+*/
+fn try<T:send>(+f: fn~() -> T) -> result::t<T,()> {
+    let p = comm::port();
+    let ch = comm::chan(p);
+    alt join(spawn_joinable {||
+        unsupervise();
+        comm::send(ch, f());
+    }) {
+      tr_success. { result::ok(comm::recv(p)) }
+      tr_failure. { result::err(()) }
+    }
+}
+
 // Local Variables:
 // mode: rust;
 // fill-column: 78;