-1

I am taking my first steps on Actix-Web. But this closure causes me errors

#[derive(Deserialize, Serialize, Debug, Copy, Clone)]
pub struct PaginationQuery {
    pub limit: Option<u32>,
    pub offset: Option<u32>,
}

pub fn get_all_trainings_2(
    query: web::Query<PaginationQuery>,
    pool: web::Data<Pool>,
) -> impl Future<Item = HttpResponse, Error = Error> {
    let mut pagination = query.0;

    // Thread Blocking
    web::block(move || database::get_exercises(pool, pagination)).then(|res| {
        match res {
            Ok((trainings_list, total)) => {
                // let mut list: Vec<TrainingsResponse> = Vec::new();

                let list: Vec<TrainingsResponse> = trainings_list
                    .into_iter()
                    .map(|tr| TrainingsResponse::from(tr))
                    .collect();

                Ok(HttpResponse::Ok().json(ListResult {
                    offset: pagination.offset.unwrap_or(0),
                    total: total as u32,
                    items: list,
                }))
            }
            Err(_) => Ok(HttpResponse::InternalServerError().into()),
        }
    })
}

Error:

error[E0597]: `pagination` does not live long enough
  --> src\handler.rs:66:29
   |
51 | ) -> impl Future<Item = HttpResponse, Error = Error> {
   |      ----------------------------------------------- opaque type requires that `pagination` is borrowed for `'static`
...
55 |     web::block(move || database::get_exercises(pool, pagination)).then(|res| {
   |                                                                        ----- value captured here
...
66 |                     offset: pagination.offset.unwrap_or(0),
   |                             ^^^^^^^^^^ borrowed value does not live long enough
...
74 | }
   | - `pagination` dropped here while still borrowed

I don't understand why I can't use the paging value a second time. What is the problem here?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
BoudBoud
  • 1
  • 1
  • I don't know what `web::block` is, but it looks like it returns a future, and therefore cannot borrow local variables. The first use of `pagination` works because you *move* it, the second use would likely be fixed by also moving it. – mcarton Dec 27 '19 at 23:22
  • @mcarton Thank you for your answer web :: block (Actix-Web) is not important it is to execute code in a thread pool (Diesel does not support tokio, so we have to run it in separate threads.) https://docs.rs/actix-web/1.0.5/actix_web/web/fn.block.html I don't understand how I can make a second move. Can you illustrate with an example? – BoudBoud Dec 28 '19 at 11:14

1 Answers1

1

The first use of pagination works because you move it, the second use would be fixed by also moving it:

web::block(move || database::get_exercises(pool, pagination)).then(move |res| { … })
//         ^^^^                                                    ^^^^

Since you are returning a Future, it cannot borrow local variables, as it could live longer.

You can move pagination twice because PaginationQuery is Copy.

mcarton
  • 27,633
  • 5
  • 85
  • 95