pub struct IsResultType<T> {
marker: core::marker::PhantomData<fn() -> T>,
}
impl<T, E> IsResultType<::core::result::Result<T, E>> {
#[allow(dead_code)]
pub const VALUE: bool = true;
}
pub trait IsResultTypeFallback {
const VALUE: bool = false;
}
impl<T> IsResultTypeFallback for IsResultType<T> {}
#[macro_export]
#[doc(hidden)]
macro_rules! is_result_type {
( $T:ty $(,)? ) => {{
#[allow(unused_imports)]
use $crate::result_info::IsResultTypeFallback as _;
$crate::result_info::IsResultType::<$T>::VALUE
}};
}
pub struct IsResultErr<'lt, T>(pub &'lt T);
impl<T, E> IsResultErr<'_, ::core::result::Result<T, E>> {
#[inline]
#[allow(dead_code)]
pub fn value(&self) -> bool {
self.0.is_err()
}
}
pub trait IsResultErrFallback {
#[inline]
fn value(&self) -> bool {
false
}
}
impl<T> IsResultErrFallback for IsResultErr<'_, T> {}
#[macro_export]
#[doc(hidden)]
macro_rules! is_result_err {
( $e:expr $(,)? ) => {{
#[allow(unused_imports)]
use $crate::result_info::IsResultErrFallback as _;
$crate::result_info::IsResultErr(&$e).value()
}};
}
#[cfg(test)]
mod tests {
#[test]
fn is_result_type_works() {
assert!(!is_result_type!(bool));
assert!(!is_result_type!(String));
assert!(!is_result_type!(Option<i32>));
assert!(is_result_type!(Result<(), ()>));
assert!(is_result_type!(Result<i32, u32>));
assert!(is_result_type!(Result<(), String>));
assert!(is_result_type!(Result<String, ()>));
assert!(is_result_type!(Result<Result<(), ()>, ()>));
assert!(is_result_type!(Result<(), Result<(), ()>>));
assert!(is_result_type!(Result<Result<(), ()>, Result<(), ()>>));
type MyResult = Result<(), ()>;
assert!(is_result_type!(MyResult));
}
#[test]
fn is_result_err_works() {
assert!(!is_result_err!(true));
assert!(!is_result_err!(42));
assert!(!is_result_err!("Hello, World!"));
assert!(!is_result_err!(Ok::<(), ()>(())));
assert!(!is_result_err!(Ok::<i32, ()>(5)));
assert!(!is_result_err!(Ok::<bool, String>(true)));
assert!(is_result_err!(Err::<(), ()>(())));
assert!(is_result_err!(Err::<(), i32>(5)));
assert!(is_result_err!(Err::<i32, bool>(false)));
assert!(is_result_err!(Err::<i32, Result::<i32, String>>(Ok(42))));
{
type Result = Option<()>;
assert!(!is_result_type!(Result));
}
}
}