I'm trying to create a trait and provide one implementation for all non-reference types, and another for all reference types.
This fails to compile:
trait Foo {}
impl<T> Foo for T {}
impl<'a, T> Foo for &'a mut T {}
This fails with the error
error[E0119]: conflicting implementations of trait `Foo` for type `&mut _`:
--> src/main.rs:3:1
|
2 | impl<T> Foo for T {}
| -------------------- first implementation here
3 | impl<'a, T> Foo for &'a mut T {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&mut _`
Strangely enough, this works:
trait Bar {}
impl<T> Bar for T
where
T: Clone,
{}
impl<'a, T> Bar for &'a mut T
where
T: Clone + 'static,
{}
Why does the version with the Clone
constraint work, and how can I make it work without it?