diff --git a/src/async_iter.rs b/src/async_iter.rs new file mode 100644 index 0000000..cd85f32 --- /dev/null +++ b/src/async_iter.rs @@ -0,0 +1,57 @@ +use async_trait::async_trait; +use futures::stream::BoxStream; +use futures::{stream }; + +#[async_trait] +trait AsyncIter { + type Item: Sized; + async fn next(&mut self) -> Option; + + fn into_stream(self) -> BoxStream<'static, Self::Item> + where + Self: Sized + Send + 'static, + { + async fn helper(mut iter: I) -> Option<(I::Item, I)> + where + I: AsyncIter, + { + let value = iter.next().await?; + Some((value, iter)) + } + Box::pin(stream::unfold(self, helper)) + } +} + + +#[cfg(test)] +mod test { + use super::*; + use futures::StreamExt; + use futures::executor; + + struct Numbers(usize); + impl Numbers { + fn new() -> Self { + Numbers(0) + } + } + + #[async_trait] + impl AsyncIter for Numbers { + type Item = usize; + + async fn next(&mut self) -> Option { + self.0 = self.0.checked_add(1)?; + Some(self.0) + } + } + #[test] + fn test_numbers() { + let a = async { + let vec1: Vec<_> = Numbers::new().into_stream().take(10).collect().await; + let vec2: Vec<_> = (1..=10).into_iter().collect(); + assert_eq!(vec1, vec2); + }; + executor::block_on(a); + } +} diff --git a/src/main.rs b/src/main.rs index b55d438..4b0f33d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod async_iter; mod config; mod output; mod tile; diff --git a/src/tiles/time.rs b/src/tiles/time.rs index 686aa25..b523d4a 100644 --- a/src/tiles/time.rs +++ b/src/tiles/time.rs @@ -49,7 +49,7 @@ impl Stream for TimeStream { type Item = Result>; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let project = Pin::as_mut(&mut self).project(); - ready!(Future::poll(project.delay, cx)); + let () = ready!(Future::poll(project.delay, cx)); let now = Local::now(); Pin::as_mut(&mut self)