Hi folks,
I'm new to rust and try to implement std::convert::From
to convert amongst all instances of my generic type MyType<T>
, i.e. MyType<u32>
to MyType<f32>
to MyType<f64>
and so on. The From
implementation should be as generic as possible. I know there are pitfalls, so I tried a less generic variant. I also put bounds on the From<OTHER>
impls, which in my understanding ensure that OTHER
is a distinct type for each impl, but still get conflicting implementation errors.
Could you help me to understand why rustc suspects conflicting implementation for WrappedX<f64>
?
Here's simplified code to reproduce my issue:
use std::convert::From;
trait GetX
{
type Output; // associated type, i.e. there can only be one impl per Self
fn x(&self) -> Self::Output;
}
// A simple generic type
struct MyType<T>(T);
impl<T> GetX for MyType<T> where T: Copy
{
type Output = T;
fn x(&self) -> Self::Output { self.0 }
}
// Construct a MyType<f64> from any f32 providing type.
impl<OTHER> From<OTHER> for MyType<f64> where OTHER: GetX<Output=f32>
{
fn from(item: OTHER) -> Self { MyType{0: item.x() as f64 } }
}
// Construct a MyType<f64> from any u32 providing type.
// Compiler says this conflicts with the above impl.
// Why? OTHER is ensured to be distinct by bounds,
// because associated type GetX::Output ensures there's only one GetX impl per type.
impl<OTHER> From<OTHER> for MyType<f64> where OTHER: GetX<Output=u32>
{
fn from(item: OTHER) -> Self { MyType{0: item.x() as f64 } }
}