It is such a relief that GAT implementation has finally merged into stable branch of rust a few days ago. And before my question, I would express my appreciation to all members contributing to this giant leap. However, I have a little problem about GAT and associated types with lifetime.
One benefit of GAT is that we can use struct with reference field (i.e. struct with lifetime parameter) as an associated type. For example:
struct StructWithLifetimeParameter<'a> {
field_of_reference: &'a Foo,
}
trait GatTrait {
type CanBeGeneric<'a>;
}
impl GatTrait for SomeOtherStruct {
type CanBeGeneric<'a> = StructWithLifetimeParameter<'a>;
}
However, this also means that using struct with lifetime parameter as an associated type is intrusive. Unless the author of a crate uses GAT to rewrite the trait, the user of such crate will never use struct with lifetime parameter as an associated type.
The fact is that, in most cases, whether an associated type is a struct with or without lifetime parameter is not semantically relevant to the crate author. For example, an author may provide a trait with associated type Context
, and the user may use a reference in the corresponding struct to avoid coping large memories across the application. But whether Context
is generic over lifetime or not does not change anything in the author's side. While if the author does not provide a GAT version, the user cannot use this graceful feature, but to use RefCell
, Rc
or something else to achieve this.
So here is my wrong conclusion: If whether an associated type generic over lifetime or not does not change anything in author's side, then the author should use GAT, since users may use struct with reference field as that associated type.
I don't think such conclusion is appropriate, since GAT is very complicated and should not be abused. But I don't know what is the right way to solve this dilemma.