use core::fmt::Debug;
pub fn expect_unwrap<T, E>(result: Result<T, E>) -> T
where
E: Debug,
{
match result {
Ok(t) => t,
Err(e) => panic!("{:?}", e),
}
}
#[cfg(test)]
mod tests {
use anyhow::bail;
use super::*;
fn try_example(
value: i32,
throw: bool,
) -> anyhow::Result<i32> {
if throw { bail!("throwing") } else { Ok(value) }
}
#[test]
fn test_expect_unwrap() {
assert_eq!(expect_unwrap(try_example(42, false)), 42);
}
#[should_panic(expected = "throwing")]
#[test]
fn test_expect_unwrap_panic() {
expect_unwrap(try_example(42, true));
}
}