Conflicting implementations of trait `std::convert::From`


#1

I’ve got struct Foo<T> and I’d like to make it possible to convert between all variants of this struct as long as T can be converted.

impl<T, U> From<Foo<U>> for Foo<T> where T: From<U> {
    fn from(other: Foo<U>) -> Self {
        Foo{
            foo:other.foo.into(),
        }
    }
}

However, this doesn’t compile:

error: conflicting implementations of trait std::convert::From<Foo<_>> for type Foo<_>
note: conflicting implementation in crate core

I guess it’s because core has a no-op version for T -> T conversion. How can I exclude that one from my implementation?


#2

For now, you can’t. If rust marks the From<T> for T impl “default” (specialization), you may be able to do this but I’m not positive (anyways, it will probably be several releases before rust even considers doing this). There have also been some RFCs like 1658 that would allow you to say T != U but it’s unlikely that any of them will be accepted any time soon.