diff options
| author | Yuval Adam <_@yuv.al> | 2022-07-05 13:37:52 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2022-07-05 13:37:52 +0300 |
| commit | ac5e6af1f3c271ce3ae798ccd483c3aa82aa2c58 (patch) | |
| tree | f26bda7e1ad3f756ffe047cd4d00c71327bec453 /src/main.rs | |
| parent | b968f5d67e49fdc754ab76640cd0bbd15d12d701 (diff) | |
Tokio async stream hello world
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 45 |
1 files changed, 13 insertions, 32 deletions
diff --git a/src/main.rs b/src/main.rs index 642901b..55c435d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,38 +1,19 @@ -use futures::executor::block_on; -use rand::prelude::*; +use async_stream::stream; -trait SimpleFuture { - type Output; - fn poll(&mut self, wake: fn()) -> Poll<Self::Output>; -} - -enum Poll<T> { - Ready(T), - Pending, -} - -pub struct RandomFuture {} +use futures_util::pin_mut; +use futures_util::stream::StreamExt; -impl SimpleFuture for RandomFuture { - type Output = f64; - fn poll(&mut self, _wake: fn()) -> Poll<Self::Output> { - let mut rng = rand::thread_rng(); - let y: f64 = rng.gen(); - println!("y value {}", y); - if y > 0.9 { - Poll::Ready(y) - } else { - // probably need to utilize wake() here - Poll::Pending +#[tokio::main] +async fn main() { + let s = stream! { + for i in 0..3 { + yield i; } - } -} + }; -async fn hello_world() { - println!("hello, world!"); -} + pin_mut!(s); // needed for iteration -fn main() { - let future = hello_world(); - block_on(future); + while let Some(value) = s.next().await { + println!("got {}", value); + } } |
