How to solve "implementation of `sqlx::Acquire` is not general enough"

Viewed 210

I'm creating a actix_web app. I tried to spawn a new task and use functions, which are already in use in actix_web part.

But if I compile, I see following error with some life-time information. To be honest, I've no idea what to do with this info...

error: implementation of `sqlx::Acquire` is not general enough
  --> src/main.rs:29:5
   |
29 |     tokio::spawn(async move {
   |     ^^^^^^^^^^^^ implementation of `sqlx::Acquire` is not general enough
   |
   = note: `&Pool<Postgres>` must implement `sqlx::Acquire<'0>`, for any lifetime `'0`...
   = note: ...but `sqlx::Acquire<'static>` is actually implemented for the type `&Pool<Postgres>

This is not working:

tokio::spawn(async move {
    run(clone).await;
});

Everything compiles, if I remove the spawn part:

//tokio::spawn(async move {
    run(clone).await;
//});

To reproduce this issue, please see https://git.jacs-home.eu/juergen/acquire_problem.git

Here is the full code:

use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
use sqlx::{Acquire, PgPool, Postgres};

async fn take_pool_or_transaction<'c, C>(pool_or_transaction: C)
where
    C: Acquire<'c, Database = Postgres>,
{
    let mut connection = pool_or_transaction.acquire().await.unwrap();

    sqlx::query("select 1 from dual")
        .execute(&mut *connection)
        .await
        .unwrap();
}

async fn run(pool: PgPool) {
    take_pool_or_transaction(&pool).await;

    let mut t = pool.begin().await.unwrap();
    take_pool_or_transaction(&mut *t).await;
}

#[actix_web::main]
async fn main() {
    let options = PgConnectOptions::new();
    let pool = PgPoolOptions::new().connect_lazy_with(options);

    let clone = pool.clone();
    tokio::spawn(async move {
        run(clone).await;
    });

    sqlx::query("select 1 from dual")
        .execute(&pool)
        .await
        .unwrap();

    println!("Hello, world!");
}

And here are the dependencies

actix-web = "3"
tokio = { version = "0.2", features = ["macros"] }
sqlx = { version = "0.4", features = ["runtime-actix-rustls", "postgres"] }
0 Answers
Related