Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(dead_code)]
- macro_rules! fun {
- (
- $fname:ident $args:tt -> $ok_ty:ty => throws $err_ty:ty
- {
- $($fbody:tt)*
- }
- ) => (
- fn $fname $args -> ::std::result::Result< $ok_ty , $err_ty >
- {
- Ok({
- $($fbody)*
- })
- }
- );
- }
- macro_rules! throw {
- (
- $expr:expr
- ) => (
- return Err($expr)
- );
- }
- fun!{ foo(b: bool) -> i32 => throws String {
- if b {
- throw!("Got true".into())
- };
- 42
- }}
- fun!{ bar(b: bool) -> () => throws String {
- foo(b)?;
- }}
- #[test]
- fn foo_true ()
- {
- if let Err(s) = foo(true) {
- assert_eq!(s, "Got true");
- } else {
- unreachable!();
- }
- }
- #[test]
- fn foo_false ()
- {
- if let Ok(x) = foo(false) {
- assert_eq!(x, 42_i32);
- } else {
- unreachable!();
- }
- }
Add Comment
Please, Sign In to add comment