Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit constructor from Box<dyn Error + Send + Sync> #83

Open
vorner opened this issue Apr 7, 2020 · 5 comments
Open

Explicit constructor from Box<dyn Error + Send + Sync> #83

vorner opened this issue Apr 7, 2020 · 5 comments

Comments

@vorner
Copy link

vorner commented Apr 7, 2020

Hello

I've noticed there's a (closed) issue about having a From<Box<dyn Error + Send + Sync>> impl, that refers to wanting that from std (#66)

But would it make sense to have an explicit constructor in the meantime? Eg. Error::from_box_error(..)? That way one would have to write some kind of map_error(Error::from_box_error)? instead of just ?, but that's probably better than nothing.

@dtolnay
Copy link
Owner

dtolnay commented Apr 7, 2020

anyhow! handles the conversion.

use anyhow::anyhow;

fn main() {
    let e: Box<dyn std::error::Error + Send + Sync> = unimplemented!();
    let e = anyhow!(e);
}

@vorner
Copy link
Author

vorner commented Apr 8, 2020

Reading the docs, doesn't this stringify the error? Won't it lose all the source chain, type information and such?

I'd hope it could somehow convert the existing dyn Error from 2-words trait object to the internal 1-word trait object. Or is it not possible?

@vorner
Copy link
Author

vorner commented Apr 8, 2020

Oh, I'm reading through the source code and it seems to be preserving it somehow. Would you mind if I send a PR making such usage clearer in the docs? Also, link from the Error type to the macro would make it little bit easier to find.

@rbtcollins
Copy link

rbtcollins commented Jul 1, 2020

Perhaps I'm missing something but anyhow! isn't very easy to use with ?.

fn main() -> anyhow::Result<()>{
   something_returning_boxed_error()?;
}

is a bit awkward - I mean:

fn main() -> anyhow::Result<()>{
  match something_returning_boxed_error(){
     Ok(v) => Ok(v),
     Err(e) => Err(anyhow!(e))
  }?
}

@vorner
Copy link
Author

vorner commented Jul 2, 2020

It could be shorter with map_err instead of explicit match, eg something like:

something_returning_boxed_error().map_err(|e| anyhow!(e))?

This would be even slightly more ergonomic if it existed as a function/constructor/method, eg:

something_returning_boxed_error().map_err(Anyhow::from_boxed)?

But as long as Rust doesn't allow implementing impl From<Box<std::error::Error + Send + Sync>> for Anyhow, this is probably the best that can be done :-(.

Repository owner locked and limited conversation to collaborators Feb 21, 2022
Repository owner deleted a comment from DanielJoyce Nov 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants