Created
October 6, 2012 22:20
-
-
Save erickt/3846368 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
foo.rs:5:56: 5:58 error: copying a noncopyable value | |
foo.rs:5 let fail_: fn~(~str) -> ! = fn~(x: ~str) -> ! { wr.send(x); fail; }; | |
^~ | |
foo.rs:5:56: 5:58 note: non-copyable value cannot be copied into a ~fn closure | |
foo.rs:5 let fail_: fn~(~str) -> ! = fn~(x: ~str) -> ! { wr.send(x); fail; }; |
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 task::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
warning: no debug symbols in executable (-arch x86_64) | |
rust: task failed at 'explicit failure', foo.rs:6 | |
Err(~"This didn\'t work at all!") |
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 fail_: fn~(~str) -> ! = fn~(x: ~str) -> ! { wr.send(x); fail; }; | |
f(fail_); | |
fail | |
} | |
fn main() { | |
let result: Result<int, ~str> = do try |fail_| { | |
fail_(~"This didn't work at all!"); | |
}; | |
io::println(fmt!("%?", result)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment