1

我有一个向量,我想发出多个请求并获取值向量。就像是:

use actix_web::client;
let nums = vec![1, 2, 3];
let values = nums.map(|num| {
    client::ClientRequest::get("http://example.com".to_owned() + &num);
});

我会得到[1, 4, 9]

换一种说法:

fn question_data(id: &str) -> Box<Future<Item = Question, Error = actix_web::error::Error>> {
    let f = std::fs::read_to_string("auth_token").unwrap();
    let token = f.trim();
    Box::new(
        client::ClientRequest::get("https://example.com/api/questions/".to_owned() + id)
            .header(
                actix_web::http::header::AUTHORIZATION,
                "Bearer ".to_owned() + token,
            )
            .finish()
            .unwrap()
            .send()
            .timeout(Duration::from_secs(30))
            .map_err(actix_web::error::Error::from) // <- convert SendRequestError to an Error
            .and_then(|resp| {
                resp.body().limit(67_108_864).from_err().and_then(|body| {
                    let resp: QuestionResponse = serde_json::from_slice(&body).unwrap();
                    fut_ok(resp.data)
                })
            }),
    )
}

然后我使用它:

let question_ids = vec!["q1", "q2", "q3"];

let mut questions = questions_data().wait().expect("Failed to fetch questions");
let question = question_data("q2")
    .wait()
    .expect("Failed to fetch question");
println!("{:?}", question);

let data = question_ids.map(|id| Box::new(question_data(id)));

但我得到了错误:

245 |     let data = question_ids.map(|id| Box::new(question_data(id)));
    |                             ^^^
    |
    = note: the method `map` exists but the following trait bounds were not satisfied:
            `&mut std::vec::Vec<std::string::String> : futures::Future`
            `&mut std::vec::Vec<std::string::String> : std::iter::Iterator`
            `&mut [std::string::String] : futures::Future`
            `&mut [std::string::String] : std::iter::Iterator`

这些是相似的,但不为我编译,我也想使用 actix-web:

4

0 回答 0