如何使专业化成功编译?

问题描述

我有一个专业化编译错误

#![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

Playground

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)