Say I have a
struct Vec3d< T >
where T:Float+Copy{
x:T, y:T, z:T
}
I want to
impl<T, U> From<Vec3d< U >> for Vec3d< T >
where T:From< U >{
fn from(rhs:Vec3d< U >) -> Vec3d< T >{
Vec3d{x:T::from(rhs.x), y:T::from(rhs.y), z:T::from(rhs.z)}
}
}
But the compiler complains there is conflict implementation.
Apparently, if T!=U, the impl should be OK, but From is automatically impled for T, which caused the conflict.
How can I archive my purpose?
It seems that I need a negative bound to ensure that T!=U, but how?
There’s no way to express negative bounds today. The formulation you’re shooting for can’t be done.
You can define your own trait for such a conversion and then base your generic code on it. Or you can manually impl the conversions you’re interested in for the specific types (a macro can cut down on the boilerplate).