The following MVE doesn't compile:
struct Left(usize);
struct Middle(usize);
struct Right(usize);
impl From<Middle> for Right {
fn from(m: Middle) -> Self {
Self(m.0)
}
}
impl From<Middle> for Left {
fn from(m: Middle) -> Self {
Self(m.0)
}
}
trait Autoconvert : std::marker::Sized {
type LeftType: From<Self>;
type RightType: From<Self>;
fn default_convert(left: Self::LeftType) -> Self::RightType {
Self::RightType::from(Self::from(left))
}
}
impl Autoconvert for Middle {
type LeftType = Left;
type RightType = Right;
}
It raises the following exception:
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/lib.rs:21:42
|
21 | Self::RightType::from(Self::from(left))
| ^^^^ expected type parameter `Self`, found associated type
|
= note: expected type parameter `Self`
found associated type `<Self as Autoconvert>::LeftType`
= note: you might be missing a type parameter or trait bound
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`.
Is there any way to use an associated type's from trait to perform this Self::from
conversion?
Outside of this MVE, I have two wrapper types that I convert to a common middle-man type, I'd rather not have to define another From trait just to be able to perform this trivial from conversion. (And it doesn't seem necessary.)