I have an error e and want to figure out if the root cause was an IO error and, if it is, do something (e.g., exit with the raw error code):
use std::io;
fn run() -> Result<(), Box<std::error::Error>> {
/* ... */
}
fn main() {
match run() {
Ok(_) => {}
Err(e) => {
let mut e = &*e as &std::error::Error;
while let Some(cause) = e.cause() {
e = cause;
}
if let Some(io_e) = e.downcast_ref::<io::Error>() {
// ...
}
}
}
}
Unfortunately, e.cause() returns an &std::error::Error not an &(std::error::Error + 'static) so I can’t call downcast_ref() on it. Is there any way to work around this? Not being able to reflect on the error chain is pretty annoying.
The error:
tmp.rs:11:26: 11:28 error: `*e` does not live long enough
tmp.rs:11 let mut e = &*e as &std::error::Error;
^~
tmp.rs:11:26: 11:28 note: reference must be valid for the static lifetime...
tmp.rs:8:5: 19:6 note: ...but borrowed value is only valid for the match at 8:4
tmp.rs:8 match run() {
^
error: aborting due to previous error
Also, does anyone know why Any: 'static?