Lifetime issue with GATs

I think it's still a sign there's something off or at least weird going on (I'm not thinking of how it could be useful, and again, you normally don't want lifetimes on traits). In your playground, you can just remove the lifetime parameter. Probably you don't need it.

You may be wondering why this still works then:

impl<'a> Viewer for SimpleWorldViewer<'a> {
    fn get_object(&self, world_object_id: u32) -> &Object {
    // Doesn't this mean I have to take a `&'b self` for
    // any `'b` at all?

And the reason is that the

  • &'b self has the type
  • &'b Self which is an alias for
  • &'b SimpleWorldViewer<'a>

And this introduces an implicit "well-formed (WF)" bound that 'a: 'b. It's ok if you can't implement something for &'static SimpleWorldViewer<'short>, because that even existing in the first place is instant UB.

for<'a> SimpleWorldViewer<'a>: Viewer still holds; that's basically what the implementation signature, which has no further where clauses or other bounds, says.

Emulating lifetime GATs on stable is one of the valid use-cases for traits-with-lifetimes; here it is for your playground. See also this great step-by-step by @steffahn.

GATs look nicer, hopefully play as nice or nicer once stabilized, and can support more exotic things, so you may or may not want to go with that depending on the larger situation (including how much you care about being on nightly / being stable).