0

I'm trying to create a marker trait to constrain the associated type inside another trait.

Here's something I tried:

pub trait A {
    type T;
}

pub trait B: A {}

impl<U: A<T = i32>> B for U {}

impl<U: A<T = u32>> B for U {}

However, the compiler reports errors:

error[E0119]: conflicting implementations of trait `B`
 --> src/main.rs:9:1
  |
7 | impl<U: A<T = i32>> B for U {}
  | --------------------------- first implementation here
8 |
9 | impl<U: A<T = u32>> B for U {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation

For more information about this error, try `rustc --explain E0119`.

I don't think this make any sense because the trait A can only be implemented once for each type, and the implementation falls into 3 categories: T is i32, T is u32, and T is neither i32 nor u32. Therefore, I don't think it will cause any conflict.

Am I missing some points thinking through it?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

0 Answers0