#![cfg_attr(not(test), no_std)]
pub trait UnwrapOrPanic<T> {
#[cfg_attr(feature = "track_caller", track_caller)]
fn unwrap_or_panic(self) -> T;
}
impl<T, E> UnwrapOrPanic<T> for Result<T, E> {
#[cfg_attr(feature = "never_inline", inline(never))]
fn unwrap_or_panic(self) -> T {
if let Ok(x) = self {
x
} else {
panic!("unwrapped an `Err`");
}
}
}
impl<T> UnwrapOrPanic<T> for Option<T> {
#[cfg_attr(feature = "never_inline", inline(never))]
fn unwrap_or_panic(self) -> T {
if let Some(x) = self {
x
} else {
panic!("unwrapped a `None`");
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unwrap_ok() {
assert_eq!(Ok::<i32, i32>(1).unwrap_or_panic(), 1);
}
#[test]
#[should_panic]
fn unwrap_err() {
Err::<i32, i32>(-1).unwrap_or_panic();
}
}