Generic wrappers and E0210


#1

Consider the following code:


struct B<U> {
 a : U
}

struct C<U>
{
   b : B<U>
}

struct E<U>
{
   b : B<U>
}

pub trait WrapsB
{
    type T;
    fn to_b(&self) -> &B<Self::T>;
    fn from_b(b: B<Self::T>) -> Self;
}

impl<U> WrapsB for C<U>
{
  type T = U;
  fn to_b(&self) -> &B<Self::T> { self.b }
  fn from_b(b: B<Self::T> ) -> Self {  C{ b : b } }
}

impl<U> WrapsB for E<U>
{
  type T = U;
  fn to_b(&self) -> &B<Self::T> { self.b }
  fn from_b(b: B<Self::T> ) -> Self {  E{ b : b } }
}

In the above code I have wrappers C and E around struct B<T> that implement the trait WrapsB which allows easy conversion to/ from struct B<T>. Now when I try to write a generic implementation of some other trait for all structs that implement WrapsB i run into E0210.

For example, in the following I tried implement From for for all types implementing WrapsB,


impl <ST> From<B<ST::T>> for ST
    where ST: WrapsB
{
    fn from(b : B<ST::T>) ->  ST{
        ST::from_b(b)
    }
}

and I get the error E02010.

error: type parameter `ST` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter [--explain E0210]
  --> <anon>:47:1
   |>
47 |> impl <ST> From<B<ST::T>> for ST
   |> ^

I could have an implementation for each wrapper (C and E) separately but I want to avoid this considering that hardwork of wrapping/unwrapping for each wrapper struct was already done when implementing trait WrapsB.
Is there a workaround for this that enables code reuse?.

https://doc.rust-lang.org/error-index.html#E0210