Skip to content
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

Compiler requires 'static when using transactions on generic AsyncConnection #104

Closed
3 tasks done
kmdreko opened this issue Sep 4, 2023 · 1 comment · Fixed by #106
Closed
3 tasks done

Compiler requires 'static when using transactions on generic AsyncConnection #104

kmdreko opened this issue Sep 4, 2023 · 1 comment · Fixed by #106
Labels
bug Something isn't working

Comments

@kmdreko
Copy link
Contributor

kmdreko commented Sep 4, 2023

The following reproducible sample triggers a compiler error:

[dependencies]
diesel = { version = "2.1.1", features = ["postgres"] }
diesel-async = { version = "0.4.1", features = ["postgres", "bb8"] }
use diesel::pg::Pg;
use diesel::result::Error;
use diesel_async::scoped_futures::ScopedFutureExt;
use diesel_async::{AsyncConnection, RunQueryDsl};

pub async fn demo<T: AsyncConnection<Backend = Pg>>(conn: &mut T) -> Result<(), Error> {
    conn.transaction::<(), Error, _>(|conn| async {
            diesel::sql_query("insert into posts (title, body) values ('title', 'body')").execute(conn).await?;
            Ok(())
        }.scope_boxed()).await?;
    Ok(())
}
error[E0310]: the parameter type `T` may not live long enough
  --> src/main.rs:7:45
   |
7  |       conn.transaction::<(), Error, _>(|conn| async {
   |  _____________________________________________^
8  | |             diesel::sql_query("insert into posts (title, body) values ('title', 'body')").execute(conn).await?;
9  | |             Ok(())
10 | |         }.scope_boxed()).await?;
   | |_______________________^ ...so that the type `T` will meet its required lifetime bounds
   |
help: consider adding an explicit lifetime bound...
   |
6  | pub async fn demo<T: AsyncConnection<Backend = Pg> + 'static>(conn: &mut T) -> Result<(), Error> {
   |                                                    +++++++++
  • I have already looked over the issue tracker for similar possible closed issues.
  • This issue can be reproduced on Rust's stable channel. (Your issue will be closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate

As far as I know, using .transaction() and .execute() in this way should not require a 'static bound.

I ended up investigating myself, created a StackOverflow post about it when I thought I was stuck, and ended up self-answering it as an odd interaction with GATs and Self-bounds.

While this isn't a major roadblock for using diesel-async (since workarounds exist) it would be nice to avoid this problem. The solution is to avoid where Self: 'conn on the associated types of AsyncConnection. This can be done (though a bit of a hack) via a dummy function as shown in this comment. I was able to modify diesel-async with this change and tested it myself. It does fix the above error and I didn't see any conflicts or downsides.

So let me know if that's worth a PR. Or if that doesn't sound worth it or would be problematic, that's fine too.

@kmdreko kmdreko added the bug Something isn't working label Sep 4, 2023
@kmdreko kmdreko changed the title Cannot use transaction on generic AsyncConnection due to compiler requiring 'static Compiler requires 'static when using transactions on generic AsyncConnection Sep 4, 2023
@weiznich
Copy link
Owner

weiznich commented Sep 5, 2023

I would be open for such a change. I cannot promise to merge without having looked at the changes in detail, but given that what's pointed out in the linked issue it should be ok. Try to include at least one test in the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants