Rust trait black magic: force equality / unification?

Suppose we have:

pub trait Ten0Trait<EnvT: EnvTrait> {
    fn get_env(&self) -> EnvT;
}
pub trait Ten1Trait<EnvT: EnvTrait> {
    fn get_env(&self) -> EnvT;
}
pub trait Ten2Trait<EnvT: EnvTrait> {
    fn get_env(&self) -> EnvT;
}

pub trait EnvTrait {
    type Ten0: Ten0Trait<Self>;
    type Ten1: Ten1Trait<Self>;
    type Ten2: Ten2Trait<Self>;

    fn new_ten0(label: Label) -> Self::Ten0;
    fn new_ten1(label: Label, rows: usize) -> Self::Ten1;
    fn new_ten2(label: Label, cols: usize, rows: usize) -> Self::Ten2;
}

I want the additional constraint on Ten0Trait that
EnvT::Ten0 = Self

I want the additional constraint on Ten1Trait that
EnvT:Ten1 = Self

I want the additional constraint on Ten2Trait that
EnvT:Ten2 = Self

Is it possible to force the above?

I think trait Ten0Trait<EnvT: EnvTrait<Ten0 = Self>> etc. will do the trick.

Dumb question on my part:

I asked for

However, is the above already enforced by:

pub trait EnvTrait {
    type Ten0: Ten0Trait<Self>;

?

Ah, yes, I think your constraint may already be met that way. It might allow Ten0 to refer to some other Ten0Trait type though, not necessarily directly mutual.

But still, the syntax I gave is how you use equality on associated types in general.

@cuviper : Ah, thanks! Can you also point me at where you learned this?

I have looked at Rust by Example / Rust Book, but Traits seems to be a few paragraphs here & there, whereas I almost want a step by step understanding of how the type checker handles Traits.

Hmm, at a glance, I don’t see this in the normal documentation. I can’t recall where I learned this, as I’ve been absorbing Rust for several years now. :slightly_smiling_face:

That particular equality syntax is the OUTPUT_CONSTRAINT in the RFC for associated items, at least.