I'm banging my head against #Rust borrow checker. If somebody can save my brain that would be cool.
@kmk Remove the 'a in fn data_source like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=95abc8dad2ca69890b4894f3547f9471
@teromene That works for the trait. But now I can not implement it for Postgres (the second link in the question): error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements (https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=2880ba2804aefb47685983e13403b3b0)
@kmk Well now you can overshadow it:
@teromene I don't get it: your example works because you have not put in the `generate` generic function. If I put it back in, it does not compile: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=df5fc7fe86a359ed5a88cdbd9b701af4
@kmk Sorry, didn't see your message.
The problem you have here is that you have a cyclic dependency on lifetimes:
You need to get the source, which borrows self for a.
Then, with the source, you call another function that must act on a, but self is already borrowed. You will need to make an allocation somewhere or use a "dynamic" borrow tool like an Rc
@teromene No need to be sorry, that's nice of you to be looking into this.
I'll try with boxing.
But what's puzzling me is that it works if I handwrite the specialized version (https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=00aba7a0a827b5bbeec46666af1b8e63) while the generic version does not. Shouldn't the problem be the same?
@kmk Not really.
Here you are creating "real" objects. So rust does the borrow check on the actual functions.
With the other one, rust cannot know in advance what your functions will look like, so it must assume the most liberal approach corresponding to your traits
@teromene And can't I limit the trait enough that it will fit like the "real" objects?
@kmk One of the thing you could do is to remove the `postgres::Transaction<'a>` and to replace it with a getter.
You would then be able to get it matching your local lifetime scope
Le réseau social de l'avenir : Pas d'annonces, pas de surveillance institutionnelle, conception éthique et décentralisation ! Possédez vos données avec Mastodon !