I'm trying to get the following to work but it runs into moving the req
to extract path and app data from it.
web::scope("/{entity_id}")
.wrap_fn(|req, srv| {
let entity_id = req
.match_info()
.get("entity_id")
.expect("Entity Id not found, invlaid url")
.to_string()
.parse::<uuid::Uuid>()
.expect("Invalid entity_id");
let db_pool = req
.app_data::<web::Data<PgPool>>()
.expect("Something went wrong extracting db pool");
// let db_pool = req.extensions().get::<web::Data<PgPool>>().unwrap();
let fut = srv.call(req);
Box::pin(async move {
// get entity
if entities_dal::EntityDAL::get_entity_by_id(
&entity_id,
db_pool.as_ref(),
)
.await
.is_err()
{
tracing::error!("create_transaction: Entity not found");
return Err(actix_web::error::ErrorBadRequest("Entity not found"));
}
Ok(fut.await?)
})
})
I get the following error
error[E0505]: cannot move out of `req` because it is borrowed
--> src/app/entities/mod.rs:173:44
|
159 | .wrap_fn(|req, srv| {
| - return type of closure `Pin<Box<[async block@src/app/entities/mod.rs:175:34: 189:26]>>` contains a lifetime `'1`
...
168 | let db_pool = req
| _______________________________________-
169 | | .app_data::<web::Data<PgPool>>()
| |____________________________________________________________- borrow of `req` occurs here
...
173 | let fut = srv.call(req);
| ^^^ move out of `req` occurs here
174 |
175 | / Box::pin(async move {
176 | | // get entity
177 | | if entities_dal::EntityDAL::get_entity_by_id(
178 | | &entity_id,
... |
188 | | Ok(fut.await?)
189 | | })
| |__________________________- returning this value requires that `req` is borrowed for `'1`
error[E0515]: cannot return value referencing function parameter `req`
--> src/app/entities/mod.rs:175:25
|
168 | let db_pool = req
| _______________________________________-
169 | | .app_data::<web::Data<PgPool>>()
| |____________________________________________________________- `req` is borrowed here
...
175 | / Box::pin(async move {
176 | | // get entity
177 | | if entities_dal::EntityDAL::get_entity_by_id(
178 | | &entity_id,
... |
188 | | Ok(fut.await?)
189 | | })
| |__________________________^ returns a value referencing data owned by the current function
Some errors have detailed explanations: E0505, E0515.
For more information about an error, try `rustc --explain E0505`.
I understand that req
is being moved and in a way consumed to it cannot be used, however it doesnt implement Clone/Copy so i dont know what to do.I'm new to rust and this is my first large project, i come from Nodejs background. Any help is appreciated.