diff options
| author | Yuval Adam <_@yuv.al> | 2022-07-05 13:56:12 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2022-07-05 13:56:12 +0300 |
| commit | 03161457e8c0f0ef657982b5796802f61cc78e31 (patch) | |
| tree | c996394abc9ba62fe9545bd78acd07fb855d939d /src | |
| parent | ac5e6af1f3c271ce3ae798ccd483c3aa82aa2c58 (diff) | |
Delayed streaming works
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index 55c435d..b6e9f06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,39 @@ use async_stream::stream; use futures_util::pin_mut; use futures_util::stream::StreamExt; +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; +use std::time::{Duration, Instant}; + +struct Delay { + when: Instant, +} + +impl Future for Delay { + type Output = &'static str; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<&'static str> { + if Instant::now() >= self.when { + println!("Hello world"); + Poll::Ready("done") + } else { + // Ignore this line for now. + cx.waker().wake_by_ref(); + Poll::Pending + } + } +} + #[tokio::main] async fn main() { let s = stream! { + let mut when = Instant::now(); for i in 0..3 { + let delay = Delay { when }; + delay.await; yield i; + when += Duration::from_millis(1000); } }; |
