From ac5e6af1f3c271ce3ae798ccd483c3aa82aa2c58 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Tue, 5 Jul 2022 13:37:52 +0300 Subject: Tokio async stream hello world --- src/main.rs | 45 +++++++++++++-------------------------------- 1 file changed, 13 insertions(+), 32 deletions(-) (limited to 'src') 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; -} - -enum Poll { - 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 { - 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); + } } -- cgit v1.3.1