Blanket impl constrained only by GAT generic constraint using Self?

Apparently you can blanket implement a trait such as

trait SelfConstrainedGAT {
    type WithGeneric<G: GenericTrait<Self>>;
}

for an unconstrained generic type U? playground

I think I see why it's not circular, as every possible GAT generic gives a different concrete trait implementation, and maybe why it's not unconstrained. But are there any potential issues I'm not seeing?

U is constrained -- it's part of the implementing type (100% of it).

Yeah, nothing wrong with impl<U> Useless for U {}.

Mine feels odd because it can only be implemented on U values which are implemented as a valid type in the trait bound of a generic associated type. But, I guess that's the bound, no matter how "loosely" it feels like it appears in the trait definition. So, just, odd but otherwise perfectly fine?

You got this backwards, the generic associated type can only be instantiated by users of the trait with a generic type parameter that satisfies GenericTrait<U>.

1 Like

Ahh, perhaps that's why it feels weird to have no bounds. So, blanket implementing on unbounded U means every type will implement SelfConstrainedGAT, but only where GenericTrait<Self/U> is valid can the trait type be successfully named... sneaky.

Well, that makes me more inclined to manually implement it per type than to try for the one-shot option. It seems unwise to implement SelfConstrainedGAT on everything, even if there's no chance for it to be abused later.

It's generally how bounds work. Here's an analogous situation with a method.


That said, bounds on GAT parameters do appear to be more lazily checked than method parameters, as demonstrated here. But AFAIK the plan is still to allow unsatisfiable bounds at some point, at which point presumably this example will succeed and so would the blanket implementation (where implicitly, U: Sized), just like it does with the GAT.

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.