-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
Have anyhow::Error implement std::error::Error? #25
Comments
That impl would be incoherent with the existing pair of impls error[E0119]: conflicting implementations of trait `std::convert::From<anyhow::Error>` for type `anyhow::Error`:
--> src/error.rs:313:1
|
313 | / impl<E> From<E> for Error
314 | | where
315 | | E: std::error::Error + Send + Sync + 'static,
316 | | {
... |
320 | | }
321 | | }
| |_^
|
= note: conflicting implementation in crate `core`:
- impl<T> std::convert::From<T> for T; Can you give a minimal example of code that would want this impl to exist? |
There's a (short) story behind that. A new rustacean colleague had written something like this, with a function panicking with use anyhow::Context;
fn with_expect() {
let foo = std::env::var("FOO")
.expect("Env var FOO must be set to provide the foo");
let data = std::fs::read(foo) // Result<_, io::Error>
.expect("Failed to read data");
println!("{:?}", data);
}
fn do_something() -> std::io::Result<()> {
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
with_expect();
do_something()?;
Ok(())
} I explained that fn with_anyhow() -> anyhow::Result<()> {
let foo = std::env::var("FOO")
.context("Env var FOO must be set to provide the foo")?;
let data = std::fs::read(foo)
.context("Failed to read data")?;
println!("{:?}", data);
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
with_anyhow()?;
do_something()?;
Ok(())
} The solution of course is to change the return type of There was an implicit assumption that an Error handling is still a hard thing for people new to Rust because of lack of standardization around |
Ah that's somewhat different, that looks like a missing |
Awesome! Indeed this was actually more about having a |
anyhow::Error
does not implementstd::error::Error
, which leads to confusion when using anyhow with code that expects aResult<_, std::error::Error>
.I noticed that the same applies to
failure::Error
in the failure crate.What is the reason for that? What prevents
anyhow::Error
from implementingstd::error::Error
?... and thanks for all your awesome Rust crates!
The text was updated successfully, but these errors were encountered: