Last active
September 17, 2015 04:52
-
-
Save nixpulvis/da247877eb85511f5729 to your computer and use it in GitHub Desktop.
Try or Rust macro, for more generally treating Results as values.
This file contains 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
macro_rules! try_or { | |
($expr:expr => $handler:expr) => (match $expr { | |
::std::result::Result::Ok(v) => v, | |
::std::result::Result::Err(e) => { | |
warn!("{}", e); | |
$handler | |
} | |
}) | |
} | |
mod test { | |
mod fixtures { | |
pub fn err() -> Result<u32, u32> { | |
Err(1) | |
} | |
} | |
#[test] | |
fn test_try_or_divergent() { | |
try_or!(fixtures::err() => { assert!(true); return }); | |
} | |
#[test] | |
fn test_try_or_value() { | |
assert_eq!(try_or!(fixtures::err() => 7), 7) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment