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

anyhow::Error to Box<dyn Error> isn't compatible with other libraries #356

Open
rkuklik opened this issue Mar 13, 2024 · 2 comments
Open

Comments

@rkuklik
Copy link

rkuklik commented Mar 13, 2024

Hello,

I have to use multiple internal crates which mix and match anyhow and eyre. When trying to convert between the two, I have convert anyhow::Error to Box<dyn Error>, which then complains that conversion requires dyn Error to be Sized (in order for Box to implement Error). This results in this ugly solution when using Result<Something, ThinError>:

result
    // Both `anyhow::Error` and `eyre::Report` support conversion into `Box`
    .map_err(Into::<Box<dyn Error + Send + Sync>>::into)
    // However, I have to stuff it into `Arc` which implements `Error` for `?Sized`
    .map_err(Into::<Arc<dyn Error + Send + Sync>>::into)?

Is there a better solution to this? If not, would it be possible to add conversion implementation from anyhow::Error into Arc or something equivalent? Unfortunately, I am unable to implement it, as it would require use of unsafe (with which I do not have enough experience) and because I am unfamiliar with techniques used in anyhow thinning.

Thank you very much in advance.

@AH-dark
Copy link

AH-dark commented Apr 14, 2024

Same question, why doesn't anyhow implement std::error:Error?

@rkuklik
Copy link
Author

rkuklik commented Apr 14, 2024

Because it implements From<E> where E: Error, which would clash with blanket implementation (you can convert any type into itself). It's the same reason for Box complains about Sized constraint to implement Error.

I solved this by wrapping anyhow::Error in newtype and implementing Error for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants