summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-10-23 13:16:47 -0400
committerBotahamec <botahamec@outlook.com>2022-10-23 13:16:47 -0400
commitd53d540ce53a2b76233b40a05f9980a084146d92 (patch)
tree73501dc57e3b070851df951f235aaffda3c6170f
parent9d65dee04abb106afa8e374f18094e94658fcf24 (diff)
Implement source() on RawUnexpected
-rw-r--r--src/unexpected.rs37
1 files changed, 30 insertions, 7 deletions
diff --git a/src/unexpected.rs b/src/unexpected.rs
index 18d0a22..01ec82f 100644
--- a/src/unexpected.rs
+++ b/src/unexpected.rs
@@ -57,7 +57,7 @@ impl RawUnexpected {
/// ```
/// use exun::*;
///
- /// let err = RawUnexpected::new(core::fmt::Error);
+ /// let x = RawUnexpected::new(core::fmt::Error);
/// ```
#[cfg(feature = "std")]
pub fn new<E: Error + Send + Sync + 'static>(e: E) -> Self {
@@ -78,13 +78,40 @@ impl RawUnexpected {
/// ```
/// use exun::*;
///
- /// let err = UnexpectedError::msg("failed");
+ /// let x = UnexpectedError::msg("failed");
/// ```
pub fn msg<E: Display + Debug + Send + Sync + 'static>(e: E) -> Self {
Self {
internal: ErrorTy::Message(Box::new(e)),
}
}
+
+ /// Get the original error.
+ ///
+ /// This will return `None` if `self` was created using
+ /// [`RawUnexpected::msg`].
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// use exun::*;
+ ///
+ /// let x = RawUnexpected::new(core::fmt::Error);
+ /// assert!(err.source().is_some());
+ ///
+ /// let x = RawUnexpected::msg("failed");
+ /// assert!(err.source().is_none());
+ /// ```
+ #[must_use]
+ pub fn source(&self) -> Option<&(dyn Error + 'static)> {
+ match &self.internal {
+ ErrorTy::Message(m) => None,
+ #[cfg(feature = "std")]
+ ErrorTy::Error(e) => Some(&**e),
+ }
+ }
}
/// An error that isn't expected to occur.
@@ -109,10 +136,6 @@ impl Display for UnexpectedError {
#[cfg(feature = "std")]
impl Error for UnexpectedError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
- match &self.0.internal {
- ErrorTy::Message(_) => None,
- #[cfg(feature = "std")]
- ErrorTy::Error(e) => Some(&**e),
- }
+ self.0.source()
}
}