Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Last active September 17, 2015 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixpulvis/da247877eb85511f5729 to your computer and use it in GitHub Desktop.
Save nixpulvis/da247877eb85511f5729 to your computer and use it in GitHub Desktop.
Try or Rust macro, for more generally treating Results as values.
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