Skip to content

Instantly share code, notes, and snippets.

@edmccard
Created May 8, 2017 00:09
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 edmccard/8898dd397eec0ff3595c28ada5221405 to your computer and use it in GitHub Desktop.
Save edmccard/8898dd397eec0ff3595c28ada5221405 to your computer and use it in GitHub Desktop.
trait Expectation<T> {
fn expected(self, msg: &str) -> T;
}
impl<T> Expectation<T> for Option<T> {
fn expected(self, msg: &str) -> T {
match self {
Some(val) => val,
_ => expectation_failed(msg),
}
}
}
impl<T,E> Expectation<T> for Result<T,E> {
fn expected(self, msg: &str) -> T {
match self {
Ok(val) => val,
_ => expectation_failed(msg),
}
}
}
#[inline(never)]
#[cold]
fn expectation_failed(msg: &str) -> ! {
panic!("expected {}", msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment