how.rs/src/explain.rs

25 lines
589 B
Rust
Raw Normal View History

2022-07-21 20:40:44 -04:00
use crate::{How, IntoContext, IntoResultHow};
2022-07-21 20:39:47 -04:00
2022-07-21 20:40:44 -04:00
crate::seal!(pub(crate) private::Sealed);
2022-07-21 20:39:47 -04:00
2022-07-21 20:40:44 -04:00
pub trait Explain: Sealed {
type Output;
2022-07-21 20:39:47 -04:00
2022-07-21 20:40:44 -04:00
#[must_use]
fn context(self, context: impl IntoContext) -> Self::Output;
2022-07-21 20:39:47 -04:00
}
2022-07-21 20:40:44 -04:00
impl<T, E> Sealed for Result<T, E> where Result<T, E>: IntoResultHow {}
2022-07-21 20:39:47 -04:00
2022-07-21 20:40:44 -04:00
impl<T, E> Explain for Result<T, E>
where
Result<T, E>: IntoResultHow,
{
type Output = Result<<Self as IntoResultHow>::T, How>;
2022-07-21 20:39:47 -04:00
#[inline(always)]
2022-07-21 20:40:44 -04:00
fn context(self, context: impl IntoContext) -> Self::Output {
self.into_result_how().map_err(|e| e.context(context))
2022-07-21 20:39:47 -04:00
}
}