Clean up AsyncIter

This commit is contained in:
Skye Jensen 2020-06-05 20:08:23 -04:00
parent 7131247e14
commit aec10c000d

View file

@ -1,24 +1,19 @@
use async_trait::async_trait; use async_trait::async_trait;
use futures::stream::BoxStream; use futures::stream::{self, BoxStream};
use futures::{stream };
#[async_trait] #[async_trait]
trait AsyncIter { pub trait AsyncIter {
type Item: Sized; type Item;
async fn next(&mut self) -> Option<Self::Item>; async fn next(&mut self) -> Option<Self::Item>;
fn into_stream(self) -> BoxStream<'static, Self::Item> fn into_stream<'a>(self) -> BoxStream<'a, Self::Item>
where where
Self: Sized + Send + 'static, Self: Sized + Send + 'a,
{
async fn helper<I>(mut iter: I) -> Option<(I::Item, I)>
where
I: AsyncIter,
{ {
Box::pin(stream::unfold(self, |mut iter| async {
let value = iter.next().await?; let value = iter.next().await?;
Some((value, iter)) Some((value, iter))
} }))
Box::pin(stream::unfold(self, helper))
} }
} }
@ -26,8 +21,7 @@ trait AsyncIter {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use futures::StreamExt; use futures::{executor, StreamExt};
use futures::executor;
struct Numbers(usize); struct Numbers(usize);
impl Numbers { impl Numbers {