gpt4 book ai didi

rust - 如何满足 `impl futures::Future: futures::TryStream` 的特征界限

转载 作者:行者123 更新时间:2023-12-03 11:36:33 26 4
gpt4 key购买 nike

我有一个使用分页的 HTTP api,我想将它包装到一个通用的 Rust 流中,以便所有端点都可以使用相同的接口(interface),这样我就可以使用 Stream 附带的特征函数特征。
我收到了这个我不太明白的错误:

error[E0599]: no method named `try_collect` found for opaque type `impl futures::Future` in the current scope
--> src/lib.rs:316:69
|
316 | let result: Result<Vec<Vec<NotificationEvent>>, _> = stream.try_collect().await;
| ^^^^^^^^^^^ method not found in `impl futures::Future`
|
= note: the method `try_collect` exists but the following trait bounds were not satisfied:
`impl futures::Future: futures::TryStream`
which is required by `impl futures::Future: futures::TryStreamExt`
`&impl futures::Future: futures::TryStream`
which is required by `&impl futures::Future: futures::TryStreamExt`
`&mut impl futures::Future: futures::TryStream`
which is required by `&mut impl futures::Future: futures::TryStreamExt`
这是我的实现:
    pub async fn v1_events_get_st(
&self,
params: &Option<GetEvents>,
) -> impl TryStream<Item = Result<Vec<NotificationEvent>, UniErr>> + '_ {

let x = stream::try_unfold((true, (*params).clone()), move |state: (bool, Option<GetEvents>)| async move {
let (remaining, mut thisParams) = state;

if !remaining {
return Ok(None);
}

return match self.v1_events_get(&thisParams).await {
Ok(res) => {

if let Some(ref mut p) = thisParams {
if let Some((_, last)) = res.get_from_to() {
p.set_after(last);
}
}

Ok(Some((res.data, (res.remaining, thisParams))))
},
Err(err) => Err(err),
}
});
return x;

}
        let stream = c.v1_events_get_st(&p);
let result: Result<Vec<Vec<NotificationEvent>>, _> = stream.try_collect().await;
这些是 try_collect 的边界。 :
    fn try_collect<C: Default + Extend<Self::Ok>>(self) -> TryCollect<Self, C>
where
Self: Sized,
{
assert_future::<Result<C, Self::Error>, _>(TryCollect::new(self))
}
为什么我需要实现 Default 和 Extend?
我不能轻易实现 Default,因为它不能用带有枚举的结构派生,我有很多枚举。
至于扩展, Vec 不是吗?已经有了这个?
问题:有没有办法在不满足的确切特征界限上获得更详细的错误?
问题:有没有更好的方法将其包装到 Stream 或其他通用接口(interface)中?我开始认为使用 for 循环而不实现 Stream 可能会更好。特征。
谢谢。

最佳答案

当您使用 async fn ,它会在将来自动包装返回值,因此有关 impl futures::Future 的错误.只需删除 async关键字,它应该工作。

关于rust - 如何满足 `impl futures::Future: futures::TryStream` 的特征界限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65374834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com