I am trying to make a function that consumes Stream
and truncate it when there are max_consecutive_fails
consecutive fails. However, things didn't work well(E0495). I changed Stream
s to Iterator
s (and removed async
s) and it simply worked. Why does this happen? How can I refactor this code (to work)?
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
}
})
}
The below one is the minimized example I tried to point out what the problem is, but I still wasn't able to understand the rustc error message.
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 })
}