Hello.
I have an error type that internally stores a lower-level error as a Option<Box<dyn Error + Send + 'static>>
. I am looking to implement the Error
trait for it. It works when I just try to add an empty impl (i.e. leaving the trait methods as their defaults), but if I try to override the source
method to return the inner error I run into trouble:
#[derive(Debug)]
struct MyError(Option<Box<dyn Error + Send + 'static>>);
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "my error")
}
}
impl Error for MyError {
// Fails to compile, but works if I remove the Send bound from the trait object in MyError
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.0.as_ref().map(|v| &**v)
}
}
The error:
error[E0308]: mismatched types
--> src/lib.rs:16:9
|
16 | self.0.as_ref().map(|v| &**v)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait `std::error::Error`, found trait `std::error::Error + Send`
|
= note: expected enum `Option<&(dyn std::error::Error + 'static)>`
found enum `Option<&dyn std::error::Error + Send>`
Is there a way to resolve this?