rust - 如何将向量映射到 actix-web 客户端请求并按顺序运行它们?
问题描述
我有一个向量,我想发出多个请求并获取值向量。就像是:
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:
解决方案
推荐阅读
- webrtc - 如何使用 simple-peer 添加视频轨道并删除它
- google-chrome - 谷歌浏览器“哇,快!” FATAL page_allocator_internals_posix.h 无法分配内存 (12)
- sql - 查询表达式中的语法错误(缺少运算符) - VBA
- python - 将大文件写入磁盘块 celery
- c# - 从 IEnumerable 隐式转换
给富 > 失败,但适用于 int[] 到 Foo > - spring - 我的 Spring Security 应用程序不接受从资源文件夹访问“新上传的图像”
- asynchronous - Python异步aiohttp客户端单个函数中的多个api调用:如何为每个响应设置一个外部对象?
- python - 即使“要求已经满足”,也找不到 gtts 模块
- c# - 无法加载文件或程序集 Microsoft.Build.Utilities.Core。为什么 DotNetCompilerPlatform 依赖于旧的 Build.Utilities.Core?
- python - 嵌套字典的问题