Trait's associated-type's associated type bound

A simplified way to think about what I am trying to do is type conversions:

pub trait A where Self: Sized {
    type B;
    fn to_b(self) -> Self::B;
}

// Doesn't compile
pub trait B where Self: Sized, Self::A: A, <Self::A as A>::B = Self {
    type A;
    fn from_a(a: Self::A) -> Self {
        a.to_b()
    }
}

I want to make sure that anything that implements A has a pair that it can be turned into. That pair should be able to call this conversion function (to_b) as a constructor.

In order for this to make sense the associated type B must refer to the same type that implements B. I am not sure what the syntax is for specifying this.

This is very hard to explain in an easy to understand way but I hope the code helps a bit.

I've done it boys! :tada:

pub trait A where Self: Sized {
    type B;
    fn to_b(self) -> Self::B;
}

pub trait B where
    Self: Sized,
    Self::A: A,
    Self: std::convert::From<<<Self as B>::A as A>::B>
{
    type A;
    fn from_a(a: Self::A) -> Self {
        a.to_b().into()
    }
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.