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 | |
| parent | ac5e6af1f3c271ce3ae798ccd483c3aa82aa2c58 (diff) | |
Delayed streaming works
| -rw-r--r-- | Cargo.lock | 66 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/main.rs | 28 |
3 files changed, 95 insertions, 1 deletions
@@ -24,6 +24,22 @@ dependencies = [ ] [[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "bytes" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" +dependencies = [ + "byteorder", + "iovec", +] + +[[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -31,6 +47,12 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "futures" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" + +[[package]] +name = "futures" version = "0.3.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e" @@ -139,12 +161,30 @@ dependencies = [ ] [[package]] +name = "iovec" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" +dependencies = [ + "libc", +] + +[[package]] name = "libc" version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" [[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] name = "memchr" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -237,10 +277,12 @@ name = "rust-async-playground" version = "0.1.0" dependencies = [ "async-stream", - "futures", + "futures 0.3.21", + "futures-core", "futures-util", "rand", "tokio", + "tokio-codec", "tokio-stream", ] @@ -274,6 +316,28 @@ dependencies = [ ] [[package]] +name = "tokio-codec" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25b2998660ba0e70d18684de5d06b70b70a3a747469af9dea7618cc59e75976b" +dependencies = [ + "bytes", + "futures 0.1.31", + "tokio-io", +] + +[[package]] +name = "tokio-io" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674" +dependencies = [ + "bytes", + "futures 0.1.31", + "log", +] + +[[package]] name = "tokio-macros" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -6,7 +6,9 @@ edition = "2021" [dependencies] async-stream = "0.3.3" futures = "0.3" +futures-core = "0.3.21" futures-util = "0.3.21" rand = "0.8" tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] } +tokio-codec = "0.1.2" tokio-stream = "0.1.9" 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); } }; |
