< 返回版块

GopherJ 发表于 2019-08-10 22:09

Tags:future, reqwest, lifetime

我有一段递归调用elasticsearch scroll api的代码

fn scan(
    url: &'static str,
    mut hits: Vec<Value>,
    scroll_future: impl Future<Item = Response, Error = Error> + 'static,
) -> impl Future<Item = HttpResponse, Error = Error> + 'static {
    scroll_future.and_then(move |mut res| {
        res.json::<Value>()
            .map_err(error::ErrorInternalServerError)
            .and_then(move |results| {
                if let (Some(hits_array), Some(total)) = (
                    results["hits"]["hits"].as_array(),
                    results["hits"]["total"].as_u64(),
                ) {
                    Either::A(if total > hits.len() as u64 {
                        Either::A(scan(
                            url,
                            hits,
                            Client::new()
                                .post(&format!("{}/scroll", url))
                                .json(&json!({
                                        "scroll": "10s",
                                        "_scroll_id": results["_scroll_id"]
                                }))
                                .send()
                                .map_err(error::ErrorInternalServerError),
                        ))
                    } else {
                        Either::B(future::ok(HttpResponse::Ok().json(json!({
                            "success": true,
                            "results": hits
                        }))))
                    })
                } else {
                    Either::B(future::ok(HttpResponse::InternalServerError().finish()))
                }
            })
    })
}

目前遇到的错误是:

error[E0720]: opaque type expands to a recursive type
  --> src/main.rs:21:6
   |
21 | ) -> impl Future<Item = HttpResponse, Error = Error> + 'static {
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expands to self-referential type
   |
   = note: expanded type is `futures::future::and_then::AndThen<impl Future<Item = Response, Error = Error> + 'static, futures::future::and_then::AndThen<futures::future::map_err::MapErr<reqwest::async_impl::response::Json<serde_json::Value>, fn(reqwest::error::Error) -> actix_web::Error {actix_web::error::ErrorInternalServerError::<reqwest::error::Error>}>, futures::future::either::Either<futures::future::either::Either<impl futures::future::Future, futures::future::result_::FutureResult<actix_web::HttpResponse, actix_web::Error>>, futures::future::result_::FutureResult<actix_web::HttpResponse, actix_web::Error>>, [closure@src/main.rs:25:23: 52:14 hits:std::vec::Vec<serde_json::Value>, url:&'static str]>, [closure@src/main.rs:22:28: 53:6 hits:std::vec::Vec<serde_json::Value>, url:&'static str]>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0720`.
error: Could not compile `elastic_proxy`.

To learn more, run the command again with --verbose.

评论区

写评论
作者 GopherJ 2019-08-10 22:14

第30行少了如下代码:

                    hits.extend_from_slice(&hits_array);
1 共 1 条评论, 1 页