use std::fmt;
#[allow(clippy::module_name_repetitions)]
pub trait ResultInspector<T, E> {
fn inspect<F>(self, f: F) -> Result<T, E>
where
F: FnMut(&T);
fn inspect_err<F>(self, f: F) -> Result<T, E>
where
F: FnMut(&E);
fn debug(self) -> Result<T, E>;
}
impl<T, E> ResultInspector<T, E> for Result<T, E>
where
T: fmt::Debug,
{
#[inline]
fn inspect<F>(self, mut f: F) -> Self
where
F: FnMut(&T),
{
if cfg!(any(debug_assertions, not(feature = "debug-only"))) {
if let Ok(ref item) = self {
f(item);
}
}
self
}
#[inline]
fn inspect_err<F>(self, mut f: F) -> Self
where
F: FnMut(&E),
{
if cfg!(any(debug_assertions, not(feature = "debug-only"))) {
if let Err(ref item) = self {
f(item);
}
}
self
}
#[inline]
fn debug(self) -> Self {
self.inspect(|item| println!("{:?}", item))
}
}