You can just write that as From::from(src.clone()) or even src.clone().into(). It will find the same receiver. Speaking of which, this is a looser restriction (which may not really matter here [1]):
impl<T> From<&T> for Single
where
T: Clone + Into<Self>,
{
fn from(src: &T) -> Self {
src.clone().into()
}
}
I can't think of a way to have both both generic implementations without overlapping.
it used to matter more when the orphan rules were more strict âŠī¸
Thanks, but that does not work for let a: Single<i32> = Single::from(&&&13):
error[E0277]: the trait bound `Single<i32>: From<&&&{integer}>` is not satisfied
--> src/main.rs:36:26
|
36 | let a: Single<i32> = Single::from(&&&13);
| ^^^^^^^^^^^^ the trait `From<&&&{integer}>` is not implemented for `Single<i32>`
|
= help: the following implementations were found:
<Single<E> as From<E>>
<Single<T> as From<&T>>
impl<T, E> From<&T> for Single<E>
where
T: Clone + Into<Self>,
{
fn from(src: &T) -> Self {
src.clone().into()
}
}
You can comment out the From<i32> version in place of the From<E> version to see it works with concrete From implementations but not the generic one (as the generic one includes all references too). I don't think there's a way around this.
In your latest playground:
impl<T> From<&T> for Single<T>
// ...
By definition this can only remove a single reference layer; you're going from &T to T (along with the latter being inside a Single).