Created
October 6, 2012 20:31
-
-
Save ben0x539/3846024 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn try<T: Send>(f: ~fn(~fn(~str) -> !) -> T) -> Result<T, ~str> { | |
let (wr, rd) = task::stream(); | |
let result: Result<T, ()> = do tasks::try |move wr| { | |
let fail_: fn~(~str) -> ! = fn~(x: ~str) -> ! { wr.send(x); fail; }; | |
f(fail_) | |
}; | |
return match move result { | |
Ok(move v) => Ok(move v), | |
Err(_) => match rd.try_recv() { | |
Some(move s) => Err(move s), | |
None => Err(~"fail") | |
} | |
} | |
} | |
fn main() { | |
let result: Result<int, ~str> = do try |fail_| { | |
fail_(~"This didn't work at all!"); | |
}; | |
io::println(fmt!("%?", result)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo.rs:4:56: 4:58 error: copying a noncopyable value | |
foo.rs:4 let fail_: fn~(~str) -> ! = fn~(x: ~str) -> ! { wr.send(x); fail; }; | |
^~ | |
foo.rs:4:56: 4:58 note: non-copyable value cannot be copied into a ~fn closure | |
foo.rs:4 let fail_: fn~(~str) -> ! = fn~(x: ~str) -> ! { wr.send(x); fail; }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment