New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lifetime issue when borrowing inside a transaction #27
Comments
This is essentially a duplicate of #23 As explained there this is a limitation of the current implementation of As a workaround: Your callback needs to own all relevant values. Use something like let user: User = conn
.transaction(move |conn| Box::pin(async move { users::table.find(user_id).first(conn).await }))
.await?; Closed as this is not actionable from our side. |
Thanks. I did not think to check the GH discussions, only the issues, sorry. Sadly the workaround is ... not that possible as I'm handed borrowed data directly. I'd need to clone it, which is not ideal at all. Oh well, that's what I'm getting for being on the bleeding edge 😄 |
I fear cloning stuff is currently the only possible solution. I'm happy to be proven wrong here, but I just do not see any other option to write such an API at all. |
Yep, in this case I cannot avoid cloning as I'm passed references. In the owned case, the usual lifetime joker that is Arc is a solution though. |
Setup
Versions
Feature Flags
postgres
postgres
Problem Description
When capturing data in the closure + future passed to
AsyncConnection::transaction()
, lifetimes makes the code not compile.The actual error given out of the box is the following:
The error is very confusing because, obviously, the closure and the future are consumed before the end of the
main
function. And the'static
lifetime makes no real sense.I originally though the issue was due to automatic lifetimes assigned by rustc to the Future in the
.transaction
function: give the same lifetime to the conn and to the future, so I tried to explicit them:With this, the compile error changes, but is as confusing as ever:
At least this time the lifetimes involved make sense:
'1
(i.e. the reborrowed conn) must outlive'2
(i.e. the future that uses said conn). But it makes no sense in this case that the future outlives the connection (because this is what the compiler effectively tells us, that it may outlive it).The full code is below.
Many thanks, this is the last hurdle for me to adopt diesel 2 + diesel-async!
What are you trying to accomplish?
Running a transaction that borrows data from around it.
What is the expected output?
A binary.
What is the actual output?
A lifetime error I cannot wrap my head around.
Are you seeing any additional errors?
Steps to reproduce
main.rs
:Cargo.toml
:Checklist
closed if this is not the case)
The text was updated successfully, but these errors were encountered: