use crate::Context;
pub trait ErrorContext {
fn with_context<C, F>(self, context: F) -> Self
where
C: Context,
F: FnOnce() -> C;
}
impl<T: ErrorContext> ErrorContext for Option<T> {
#[allow(clippy::manual_map)] #[track_caller]
#[inline]
fn with_context<C, F>(self, context: F) -> Self
where
C: Context,
F: FnOnce() -> C,
{
match self {
None => None,
Some(err) => Some(err.with_context(context)),
}
}
}
impl<T, E: ErrorContext> ErrorContext for Result<T, E> {
#[allow(clippy::manual_map)] #[track_caller]
#[inline]
fn with_context<C, F>(self, context: F) -> Self
where
C: Context,
F: FnOnce() -> C,
{
match self {
Ok(val) => Ok(val),
Err(err) => Err(err.with_context(context)),
}
}
}