Convert errors to Box<Error + Send>


#1

With the help of ? operator most errors are easily convertible to Box<Error>, and I’ve used that type extensively. Now I need to return these errors from another thread, so I’ve changed error types to Box<Error + Send>.

However, Err("foo")? used to convert seamlessly to Box<Error>, but it doesn’t convert to Box<Error + Send>. In general I have a problem with Rust being picky about converting various types that have Error and I suppose should be Send to Box<Error + Send>.

Is Rust missing a From/Into implementations for the + Send versions? How can I easily covert sendable types that implement Error to to Result<(), Box<Error + Send>>?


#2

There are From impls for Box<Error> and Box<Error + Send + Sync>, but not for Box<Error + Send>. So, just add Sync :slight_smile:


#3

Thanks. What about compatibility of Box<Error + Send + Sync> with plain Box<Error>?

https://play.rust-lang.org/?gist=27f1812e2512e9b6a71d4860c5dd4f36&version=nightly


#4

This seems like an issue with impl<T: Error> From<T> for Box<Error> needing T to be Sized since ? ends up calling into() on the error. In fact, if you hand wrote the match code as follows, it works:

fn all() -> Result<(), Box<Error + Send + Sync>> {
    Ok(())
}

fn one() -> Result<(), Box<Error>> {
    match all() {
      Ok(()) => Ok(()),
      Err(e) => Err(e)
    }
}

But of course if you change Err(e) => Err(e) to Err(e) => Err(e.into()), you get the same compilation error.