作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用分页的 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?
Vec
不是吗?已经有了这个?
Stream
可能会更好。特征。
最佳答案
当您使用 async fn
,它会在将来自动包装返回值,因此有关 impl futures::Future
的错误.只需删除 async
关键字,它应该工作。
关于rust - 如何满足 `impl futures::Future: futures::TryStream` 的特征界限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65374834/
我有一个使用分页的 HTTP api,我想将它包装到一个通用的 Rust 流中,以便所有端点都可以使用相同的接口(interface),这样我就可以使用 Stream 附带的特征函数特征。 我收到了这
我是一名优秀的程序员,十分优秀!