Skip to content

Instantly share code, notes, and snippets.

@ben0x539
Created October 6, 2012 20:31
Show Gist options
  • Save ben0x539/3846024 to your computer and use it in GitHub Desktop.
Save ben0x539/3846024 to your computer and use it in GitHub Desktop.
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));
}
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