I had my own error type, with lots of conversions to it. It was too non-standard and didn't do error propagatin, so I started to convert to "anyhow". (Good choice?)
So I tried this conversion, modeled on something I was doing with my own error type. I want to be able to convert errors of the form Result<something, &'static str>
. But Rust does not allow this conversion.
use anyhow;
impl std::convert::From<&'static str> for anyhow::Error {
fn from(msg: &'static str) -> Self {
anyhow::anyhow!("{}",msg)
}
}
Error message is:
error[E0119]: conflicting implementations of trait `std::convert::From<&'static str>` for type `anyhow::Error`:
--> src/common/errors.rs:17:1
|
17 | impl std::convert::From<&'static str> for anyhow::Error {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `anyhow`:
- impl<E> From<E> for anyhow::Error
where E: std::error::Error, E: Send, E: Sync, E: 'static;
= note: upstream crates may add a new impl of trait `std::error::Error` for type `&'static str` in future versions
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> src/common/errors.rs:17:1
|
17 | impl std::convert::From<&'static str> for anyhow::Error {
| ^^^^^--------------------------------^^^^^-------------
| | | |
| | | `anyhow::Error` is not defined in the current crate
| | `str` is not defined in the current crate
| impl doesn't use only types from inside the current crate
|
= note: define and implement a trait or new type instead
So is it possible to define an implicit conversion from str to anyhow::Error?