我正在创建一个 actix_web 应用程序。我尝试生成一个新任务并使用已在 actix_web 部分中使用的功能。
但是,如果我编译,我会看到带有一些生命周期信息的以下错误。老实说,我不知道如何处理这些信息......
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>
这不起作用:
tokio::spawn(async move {
run(clone).await;
});
如果我删除 spawn 部分,一切都会编译:
//tokio::spawn(async move {
run(clone).await;
//});
要重现此问题,请参阅https://git.jacs-home.eu/juergen/acquire_problem.git
这是完整的代码:
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!");
}
这是依赖项
actix-web = "3"
tokio = { version = "0.2", features = ["macros"] }
sqlx = { version = "0.4", features = ["runtime-actix-rustls", "postgres"] }