0

Consider the following:

struct Foo(f32);

impl<T> std::convert::From<T> for Foo
where f32: std::convert::From<T>
{
    fn from(value : T) -> Self {
        Self(value.into())
    }
}

fn main() {
    let a: Foo = 42.into();
}

This of course doesn't work because

the trait bound f32: From<i32> is not satisfied

So I'm trying to add another trait implementation with less constraints:

impl std::convert::From<i32> for Foo
{
    fn from(value : i32) -> Self {
        Self(value as f32)
    }
}

Which then gives me

conflicting implementations of trait From<i32> for type Foo

What's the issue here, shouldn't the generic instantiation be discard for i32 because the type requirement failed, thus allowing another explicit implementation for From<i32>?

Frederiko Ribeiro
  • 1,844
  • 1
  • 18
  • 30
Jaffa
  • 12,442
  • 4
  • 49
  • 101

0 Answers0