I have an error type that is coming from a template, which might (in fact probably will be) std::io::Error
. Then I return a std::io::Error
, if the original error is a std::io::Error
, I would like to just return it, but if not, I want to wrap it using std::io::Error::new
. I can do this using code that looks like:
let error: io::Error = match e.into().downcast::<io::Error>() {
Ok(err) => *err,
Err(err) => io::Error::new(io::ErrorKind::ConnectionAborted, err)
};
where e
is constrained to be Into<Box<dyn Error + Send + Sync>>
but, it feels a little wrong to me that I have to Box the error, only to immediately move it out of the box (and free the memory it just allocated).
I can't think of a better way to do this though with current Rust. Although, I think there is a way that it could be done with static dispatch once RFC 1210 lands.
Is there a better way to handle this?