I made a generic Vector struct, used with different T types :
struct Vector<T> {
data: [T; 3],
}
I already implemented some generic traits for mathematical operands (Eq
, Index
, std::ops::Add
...) and it works fine.
But now, I am struggling with implementing a From
trait so I can easily convert, for instance, a Vector into a Vector.
impl<T, U> From<Vector<U>> for Vector<T> {
fn from(item: Vector<U>) > Self {
// [...]
}
}
Regardless of this from
function's implementation, I get the following error:
error[E0119]: conflicting implementations of trait `From<Vector<_>>` for type `Vector<_>`
> <source>:5:1

5  impl<T, U> From<Vector<U>> for Vector<T> {
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

= note: conflicting implementation in crate `core`:
 impl<T> From<T> for T;
If I understand correctly, it is because by default, for any type, From
is already declared. And my implementation conflicts with it if T
and U
are identical types.
So is there a way to use my implementation for any different types T
and U
, but not use it for identical types T
and U
?