#rust #specialization
#Ржавчина #специализация
Вопрос:
У меня ошибка компиляции специализации.
#![feature(min_specialization)]
use anyhow::{Error as AnyError, anyhow};
pub enum HandleError {
AnyHandleError(i64),
}
impl<T> From<T> for HandleError where T: std::error::Error {
default fn from(err: T) -> Self {
HandleError::AnyHandleError(3)
}
}
// Question 1: Why compiler print conflicting implementations error?
// But the TestError next compile successfully.
impl From<AnyError> for HandleError {
fn from(err: AnyError) -> Self {
HandleError::AnyHandleError(6)
}
}
struct TestError;
impl From<TestError> for HandleError {
fn from(err: TestError) -> Self {
HandleError::AnyHandleError(3)
}
}
fn main() {
// Question 2: why this line can't complie?
// let anyhow: HandleError = anyhow!("create anyhow error").into();
let test: TestError = TestError {}.into();
}
Я думаю #![feature(min_specialization)]
, должно сработать, но я получил эту ошибку.
error[E0119]: conflicting implementations of trait `std::convert::From<anyhow::Error>` for type `HandleError`:
--> src/main.rs:16:1
|
8 | impl<T> From<T> for HandleError where T: std::error::Error {
| ---------------------------------------------------------- first implementation here
...
16 | impl From<AnyError> for HandleError {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `HandleError`
|
= note: upstream crates may add a new impl of trait `std::error::Error` for type `anyhow::Error` in future versions