Associated type not resolved in the same way as "explicit" types

Hey Rustaceans :slight_smile:

I have this problem regarding traits - associated types - lifetimes, that I globally understand but don't know how to fix: I know my architecture is probably wrong but don't know why Rust wouldn't let me go with this one.

The idea is to implement the GenContainer trait (defined in crate A), and then each struct ("Container") that is produced by this process may (or may not if that's not possible) implement the DoIt trait, where DoIt is defined in crate B.

This exemple here shows that what I intend to do is supposed to work (without lifetimes). I can create a Container of callback, Container implements DoIt if &mut Foo is the given parameter of the callback, and the function returns a Result<(), ()> .

The problem is that in my case, some backends (defined in crate B) may require lifetimes on the Foo struct, which is no big deal as Foo is defined in crate B anyway. This example here is the same as the one before, but with a lifetime added to the Foo struct. For some reason, that does not compile.

AIUI, setting the explicit type Foo (as in my fn callback in the example) + letting the compiler infer the lifetime (Foo could be written as Foo<'_> or for<'a> Foo<'a> in that case), does not yield the same type as using an associated type on a trait. I've read tons of articles the past two weeks regarding GATs, or matklad's brilliant article on lifetime encapsulation to try to workaround the problem, but I can't find anything useful. Also my head is starting to hurt from banging it against the walls.

Thanks to anyone taking time to read / answer this :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.