Skip to content

Commit bc5e22f

Browse files
sfacklerseanmonstar
authored andcommitted
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
1 parent b7a0c2d commit bc5e22f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/error.rs

+13
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ impl Error {
125125
self.inner.kind == Kind::Closed
126126
}
127127

128+
/// Returns the error's cause.
129+
///
130+
/// This is identical to `Error::cause` except that it provides extra
131+
/// bounds required to be able to downcast the error.
132+
pub fn cause2(&self) -> Option<&(StdError + 'static + Sync + Send)> {
133+
self.inner.cause.as_ref().map(|e| &**e)
134+
}
135+
136+
/// Consumes the error, returning its cause.
137+
pub fn into_cause(self) -> Option<Box<StdError + Sync + Send>> {
138+
self.inner.cause
139+
}
140+
128141
pub(crate) fn new(kind: Kind, cause: Option<Cause>) -> Error {
129142
Error {
130143
inner: Box::new(ErrorImpl {

0 commit comments

Comments
 (0)