I want to define a trait A
. I can fulfill A
on anything which is a std::default::Default
but I want a special implementation for u64
. The specialization feature is designed to do this, and the following code compiles fine:
#![feature(specialization)]
trait A {}
impl<T: std::default::Default> A for T {}
impl A for u64 {}
fn main() {}
Now I want to use a trait from another crate (protobuf::Message
) instead of std::default::Default
. The following code doesn't work:
#![feature(specialization)]
extern crate protobuf;
trait A {}
impl<T: protobuf::Message> A for T {}
impl A for u64 {}
fn main() {}
The compiler complains of conflicting implementations:
error[E0119]: conflicting implementations of trait `A` for type `u64`:
--> src/main.rs:6:1
|
5 | impl<T: protobuf::Message> A for T {}
| ------------------------------------- first implementation here
6 | impl A for u64 {}
| ^^^^^^^^^^^^^^^^^ conflicting implementation for `u64`
protobuf::Message
is not implemented for u64
, so there's no conflict, but that doesn't really seem relevant, because std::default::Default
is defined for u64
, and the specialization works fine for that case.
Since I specified the implementation for a concrete type, I would expect that to override implementations via traits.
Follow-up: I believe this is not a duplicate of Why do I get a conflicting implementations error when specializing a trait?, because this code works for traits defined in the standard library and fails for traits defined in external crates. In the other question, the mistake was in insufficiently strict trait bound specification. Here, the specializing trait is a primitive type, so that solution cannot apply.