#![warn(missing_docs)]
use std::fmt::Debug;
pub trait ExpectWith<T> {
#[track_caller]
fn expect_with<F, S>(self, f: F) -> T
where
S: AsRef<str>,
F: FnOnce() -> S;
}
impl<T, E> ExpectWith<T> for Result<T, E>
where
E: Debug,
{
#[inline]
#[track_caller]
fn expect_with<F, S>(self, f: F) -> T
where
S: AsRef<str>,
F: FnOnce() -> S,
{
match self {
Ok(t) => t,
Err(e) => panic!("{}: {:?}", f().as_ref(), e),
}
}
}
impl<T> ExpectWith<T> for Option<T> {
#[inline]
#[track_caller]
fn expect_with<F, S>(self, f: F) -> T
where
S: AsRef<str>,
F: FnOnce() -> S,
{
match self {
Some(t) => t,
None => panic!("{:?}", f().as_ref()),
}
}
}