是否可以指定两个类型参数是不同的类型?

Is it possible to specify that two type parameters are different types?

我有一个带有 map 方法的简单包装器结构。我还有一个错误枚举层次结构,我在其中实现了 From 以便能够将 Error1 转换为 Error2,允许 try! 宏自动为我转换:

struct Span<T>(T);

impl<T> Span<T> {
    fn map<F, U>(self, f: F) -> Span<U>
        where F: FnOnce(F) -> U
    {
        Span(f(self.0))
    }
}

enum Error1 { One }
enum Error2 { Two }

impl From<Error1> for Error2 {
    fn from(v: Error1) -> Error2 { Error2::Two }
}

我希望能够添加一个 From 实现,这样我也可以自动转换 Span 结构的内部:

impl<T,U> From<Span<T>> for Span<U>
    where U: From<T>
{
    fn from(v: Span<T>) -> Span<U> {
        v.map(|v| v.into())
    }
}

不幸的是,this fails

error[E0119]: conflicting implementations of trait `std::convert::From<Span<_>>` for type `Span<_>`:
  --> src/main.rs:18:1
   |
18 |   impl<T,U> From<Span<T>> for Span<U>
   |  _^ starting here...
19 | |     where U: From<T>
20 | | {
21 | |     fn from(v: Span<T>) -> Span<U> {
22 | |         v.map(|v| v.into())
23 | |     }
24 | | }
   | |_^ ...ending here
   |
   = note: conflicting implementation in crate `core`

错误消息没有指向 specific implementation of From,但我猜是这个:

impl<T> From<T> for T

如果我的 TU 碰巧是相同的具体类型,我的实现可能会发生冲突。有什么办法可以实现所有 TU 的特征,其中 T != U?

不幸的是,这还不可能,而且解决这个问题的最佳方法还没有真正确定。与这种情况略有相关的一项提议是 negative bounds (specifically equality bounds), but I think it has been deemed too complex. See the latest issue 关于该主题的想法以获取更多信息,其中团队成员正在考虑不同的想法,包括专业化。