Skip to content

Commit

Permalink
feat(error): add Error::cause2 and Error::into_cause
Browse files Browse the repository at this point in the history
- The `cause2` method adds a `'static` bound, allowing to downcast the error type.
- The `into_cause` method converts an `Error` into its optional cause.

Closes #1542
  • Loading branch information
sfackler authored and seanmonstar committed Jun 6, 2018
1 parent b7a0c2d commit bc5e22f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ impl Error {
self.inner.kind == Kind::Closed
}

/// Returns the error's cause.
///
/// This is identical to `Error::cause` except that it provides extra
/// bounds required to be able to downcast the error.
pub fn cause2(&self) -> Option<&(StdError + 'static + Sync + Send)> {
self.inner.cause.as_ref().map(|e| &**e)
}

/// Consumes the error, returning its cause.
pub fn into_cause(self) -> Option<Box<StdError + Sync + Send>> {
self.inner.cause
}

pub(crate) fn new(kind: Kind, cause: Option<Cause>) -> Error {
Error {
inner: Box::new(ErrorImpl {
Expand Down

0 comments on commit bc5e22f

Please sign in to comment.