I'm trying to define a generic From
implementation for a struct like this (playground):
struct Wrapped<T: Clone>(T);
impl<T: Clone, __Into0: ::std::convert::Into<T>> ::std::convert::From<(__Into0)> for Wrapped<T> {
fn from(original: (__Into0)) -> Wrapped<T> {
Wrapped(original.into())
}
}
fn main() {
let _: Wrapped<i64> = 5i32.into();
}
However, I get the following error:
error[E0119]: conflicting implementations of trait `std::convert::From<Wrapped<_>>` for type `Wrapped<_>`:
--> src/main.rs:3:1
|
3 | / impl<T: Clone, __Into0: ::std::convert::Into<T>> ::std::convert::From<(__Into0)> for Wrapped<T> {
4 | | fn from(original: (__Into0)) -> Wrapped<T> {
5 | | Wrapped(original.into())
6 | | }
7 | | }
| |_^
|
= note: conflicting implementation in crate `core`
I'm guessing this is caused by the fact that From
is implemented for every type to itself and my new implementation also includes a way to solve that because of that.
I'm not sure how to solve this recursion-like behaviour. The only solution I can think of is to use a single element tuple instead (playground):
struct Wrapped<T: Clone>(T);
impl<T: Clone, __Into0: ::std::convert::Into<T>> ::std::convert::From<(__Into0,)> for Wrapped<T> {
fn from(original: (__Into0,)) -> Wrapped<T> {
Wrapped(original.0.into())
}
}
fn main() {
let _: Wrapped<i64> = (5i32,).into();
}
This works, but it makes the API more awkward than I would like. Are there any other ways to solve this compilation error?