From bc5e22f58095f294333f49f12eeb7e504cda666c Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 6 Jun 2018 13:40:07 -0700 Subject: [PATCH] feat(error): add `Error::cause2` and `Error::into_cause` - 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 --- src/error.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/error.rs b/src/error.rs index 0eb064f2ef..ab65e75523 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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> { + self.inner.cause + } + pub(crate) fn new(kind: Kind, cause: Option) -> Error { Error { inner: Box::new(ErrorImpl {