I need to write a generic function which instantiates a value of a given type and passes it a reference. Thus the trait which the given type needs to implement is parametrized with a lifetime. I would like to achieve something like in case 1, so the type is passed before the reference I want exists. If I create that value before (case 2) all works well. For the completeness reason I also added case 3 but it doesn't compile as well. I read the answer here [1] , but I couldn't apply it to my case.
the trait bound `for<'a> AFooer<'_>: Fooer<'a>` is not satisfied
It becomes clear that the type T gets a lifetime elided in the main function while we want the 'a in the make_fooer function. I don't know any way to do this, so instead I'll consider it a limitation of the compiler and propose an alternative.
You seem to be trying to do a strategy pattern so why not simplify it to this:
Unfortunately the solution you sent doesn't solve the issue I have because the function make_fooer shouldn't mention concrete type (AFooer) but should use the type parameter. The whole idea is that I would have more than one implementer of the trait.