我正在尝试制作一个在连续失败Stream
时消耗并截断它的函数。max_consecutive_fails
然而,事情并不顺利(E0495)。我将Stream
s 更改为Iterator
s (并删除了async
s),它很有效。为什么会这样?我怎样才能重构这段代码(工作)?
use futures::stream::Stream;
pub fn max_fail<'a, T>(stream : impl Stream<Item = Option<T>> +'a , max_consecutive_fails: usize) -> impl Stream +'a where T : 'a
{
use futures::stream::StreamExt;
let mut consecutive_fails = 0;
stream.take_while(move |x| async {
if x.is_some(){
consecutive_fails = 0;
true
}
else{
consecutive_fails += 1;
consecutive_fails != max_consecutive_fails
}
})
}
下面是我试图指出问题所在的最小化示例,但我仍然无法理解 rustc 错误消息。
use futures::stream::Stream;
pub fn minified_example<'a>(stream: impl Stream<Item = bool> + 'a) -> impl Stream + 'a
{
use futures::stream::StreamExt;
stream.take_while( |x| async { *x })
}