我有一个使用OpenAPI Generator生成的(云)API的可用的Rust示例.
API表示资源(应用程序-服务-部署)的层次 struct ,因此函数的实现必须是嵌套的;代码需要迭代每个应用程序以获取应用程序的服务,然后迭代服务...
我无法将接口调用合并到async fn
‘S,至少有两个原因:
- 加入期货市场
- 收集错误
我希望定义3个具有公共返回类型的函数;每个函数都有不同的参数:
async fn fetch_and_transform_apps() -> Result<Vec<Foo>, anyhow::Error> {...}
async fn fetch_and_transform_services() -> Result<Vec<Foo>, anyhow::Error> {...}
async fn fetch_and_transform_deployments() -> Result<Vec<Foo>, anyhow::Error> {...}
我想不出如何实现上面的#1和#2(#3正在工作).这就是我所拥有的:
async fn fetch_and_transform_services(...) -> Result<vec<Foo>, anyhow::Error> {
let list_services_reply = list_services(...).await?;
let services = list_services_reply
.services.ok_or_else(|| anyhow::anyhow!("No services found"))?;
let items_futures = services
.iter()
.map(|service| async move {
let items: Result<Vec<Foo>, Error> = fetch_and_transform_deployments(...).await;
items
});
let items = join_all(items_futures)
.await
// The type here is: Vec<Result<Vec<Foo>, anyhow::Error>>
.into_iter()
// The collect type results in the error
.collect::<Result<Vec<koyeb::Koyeb>, anyhow::Error>>();
items
}
错误:
不能从对类型为
std::vec::Vec<Foo>
的元素的迭代器生成类型为std::vec::Vec<Foo>
的值 特性FromIterator<std::vec::Vec<Foo>>
不是针对std::vec::Vec<Foo>
实现的,这是Result<std::vec::Vec<Foo>, anyhow::Error>: FromIterator<Result<std::vec::Vec<Foo>, anyhow::Error>>
所需的 特征FromIterator<Foo>
是针对std::vec::Vec<Foo>
实现的 对于该特征的实现,预期为Foo
,结果为std::vec::Vec<Foo>
Result<std::vec::Vec<Foo>, anyhow::Error>
实施FromIterator<Result<std::vec::Vec<Foo>, anyhow::Error>>
所需