Я пытаюсь написать сервер аутентификации, используя actix
и tokio_postgres
таким же образом, как FrameworkBenchmarks , мой код выглядит так:
use tokio_postgres::{Client, Statement, NoTls, connect};
use actix::{Actor, Context, Addr, Message, Handler, ResponseFuture};
use std::io;
use futures::future::FutureExt;
pub struct PgConnection {
client: Client,
login: Statement,
}
impl Actor for PgConnection {
type Context = Context<Self>;
}
impl PgConnection {
pub async fn connect(db_url: &str) -> Result<Addr<PgConnection>, io::Error> {
let (cl, conn) = connect(db_url, NoTls)
.await
.expect("can not connect to postgresql");
actix_rt::spawn(conn.map(|_| ()));
let login = cl.prepare("SELECT id FROM User_ WHERE username='$1' AND password=crypt($2, gen_salt('bf'))").await.unwrap();
Ok(PgConnection::create(move |_| PgConnection {
client: cl,
login,
}))
}
}
pub struct LoginRequest;
impl Message for LoginRequest {
type Result = io::Result<String>;
}
impl Handler<LoginRequest> for PgConnection {
type Result = ResponseFuture<Result<String, io::Error>>;
fn handle(&mut self, _: LoginRequest, _: &mut Self::Context) -> Self::Result {
let fut = self.client
.query_one(&self.login, &[&"1", &"2"]);
Box::pin(async move {
let row = fut
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{:?}", e)))?;
Ok(row.get(0))
})
}
}
Но я встретил вверх с запутанной ошибкой времени жизни:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/db.rs:44:14
|
44 | .query_one(&self.login, &[&"1", &"2"]);
| ^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 42:5...
--> src/db.rs:42:5
|
42 | / fn handle(&mut self, _: LoginRequest, _: &mut Self::Context) -> Self::Result {
43 | | let fut = self.client
44 | | .query_one(&self.login, &[&"1", &"2"]);
45 | |
... |
52 | | })
53 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/db.rs:43:19
|
43 | let fut = self.client
| ^^^^^^^^^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
--> src/db.rs:46:9
|
46 | / Box::pin(async move {
47 | | let row = fut
48 | | .await
49 | | .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{:?}", e)))?;
50 | |
51 | | Ok(row.get(0))
52 | | })
| |__________^
= note: expected `std::pin::Pin<std::boxed::Box<(dyn core::future::future::Future<Output = std::result::Result<std::string::String, std::io::Error>> + 'static)>>`
found `std::pin::Pin<std::boxed::Box<dyn core::future::future::Future<Output = std::result::Result<std::string::String, std::io::Error>>>>`
Кто-нибудь может сказать мне, в чем проблема и как я могу ее исправить?
На всякий случай, версии моей зависимости:
[dependencies]
tokio-postgres = "0.5.4"
actix-web = "2.0.0"
actix-rt = "1.1.1"
actix = "0.9.0"
futures = "0.3.4"
И моя машина go и ржавчина c версия: машина go 1.43.0 (3532cf738 2020-03-17) ржавчина c 1.43.0 (4fb7144ed 2020-04-20)