-
-
Notifications
You must be signed in to change notification settings - Fork 136
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
IntoError trait #307
IntoError trait #307
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
impl<E: Into<anyhow::Error>> From<E> for SharedError { fn from(_: E) -> Self { ... } }
impl<E: anyhow::IntoError> From<E> for SharedError { fn from(_: E) -> Self { ... } }
Do you have a complete example where one of these is incoherent ("conflicting implementations") and the other is fine?
Sorry, I missed important bit of first example. The important part is that I also need Two examples which do not work. First example is where
Another example is where
both do not compile. Working example is included in this PR. |
Consider this scenario. There's another error type which should be constructable from either standard errors or `anyhow::Error`. So `?` operator can be used to return results with new error type. Example of such error is `SharedError` from buck2 project: https://github.com/facebook/buck2/blob/126049599e9d1b56c25c5fba7e6127294719dff0/app/buck2_common/src/result.rs#L46 It is not possible to implement such generic conversion in Rust. For example, this approach is compilation error: ``` impl<E: Into<anyhow::Error>> From<E> for SharedError { fn from(err: E) -> Self { ... } } = note: conflicting implementation in crate `core`: - impl<T> From<T> for T; ``` This approach is different compilation error: ``` impl<E: std::error::Error> From<E> for SharedError { fn from(err: E) -> Self { ... } } impl From<anyhow::Error> for SharedError { fn from(err: anyhow::Error) -> Self { ... } } = note: upstream crates may add a new impl of trait `std::error::Error` for type `anyhow::Error` in future versions ``` `IntoError` crate implemented inside `anyhow` crate allows implementation: ``` impl<E: anyhow::IntoError> From<E> for SharedError { fn from(value: E) -> Self { ... } } ```
Thought about this a bit, I do think the trick shown here is a fully sufficient alternative to this PR. Not sure if there's a downside I'm not seeing, but it's at least not obvious to me what advantage this would still bring |
If Jakob's solution is adequate for your use case, that's great and I'd like to close this PR. Otherwise I am on board with adding a conversion trait. My remaining hesitations are:
|
Jakob took over what I wanted to do. I'll close this PR, since it is controversial, and nobody else requested it. |
Consider this scenario. There's another error type which should be constructable from either standard errors or
anyhow::Error
. So?
operator can be used to return results with new error type.Example of such error is
SharedError
from buck2 project:https://github.com/facebook/buck2/blob/126049599e9d1b56c25c5fba7e6127294719dff0/app/buck2_common/src/result.rs#L46
It is not possible to implement such generic conversion in Rust.
For example, this approach is compilation error:
This approach is different compilation error:
IntoError
crate implemented insideanyhow
crate allows implementation: