summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2022-07-05 13:37:52 +0300
committerYuval Adam <_@yuv.al>2022-07-05 13:37:52 +0300
commitac5e6af1f3c271ce3ae798ccd483c3aa82aa2c58 (patch)
treef26bda7e1ad3f756ffe047cd4d00c71327bec453
parentb968f5d67e49fdc754ab76640cd0bbd15d12d701 (diff)
Tokio async stream hello world
-rw-r--r--Cargo.lock84
-rw-r--r--Cargo.toml4
-rw-r--r--README.md5
-rw-r--r--src/main.rs45
4 files changed, 106 insertions, 32 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 12e2899..65f4be3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,6 +3,27 @@
version = 3
[[package]]
+name = "async-stream"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dad5c83079eae9969be7fadefe640a1c566901f05ff91ab221de4b6f68d9507e"
+dependencies = [
+ "async-stream-impl",
+ "futures-core",
+]
+
+[[package]]
+name = "async-stream-impl"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "10f203db73a71dfa2fb6dd22763990fa26f3d2625a6da2da900d23b87d26be27"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -109,6 +130,15 @@ dependencies = [
]
[[package]]
+name = "hermit-abi"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
+dependencies = [
+ "libc",
+]
+
+[[package]]
name = "libc"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -121,6 +151,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
+name = "num_cpus"
+version = "1.13.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1"
+dependencies = [
+ "hermit-abi",
+ "libc",
+]
+
+[[package]]
+name = "once_cell"
+version = "1.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
+
+[[package]]
name = "pin-project-lite"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -190,8 +236,12 @@ dependencies = [
name = "rust-async-playground"
version = "0.1.0"
dependencies = [
+ "async-stream",
"futures",
+ "futures-util",
"rand",
+ "tokio",
+ "tokio-stream",
]
[[package]]
@@ -212,6 +262,40 @@ dependencies = [
]
[[package]]
+name = "tokio"
+version = "1.19.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c51a52ed6686dd62c320f9b89299e9dfb46f730c7a48e635c19f21d116cb1439"
+dependencies = [
+ "num_cpus",
+ "once_cell",
+ "pin-project-lite",
+ "tokio-macros",
+]
+
+[[package]]
+name = "tokio-macros"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "tokio-stream"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df54d54117d6fdc4e4fea40fe1e4e566b3505700e148a6827e59b34b0d2600d9"
+dependencies = [
+ "futures-core",
+ "pin-project-lite",
+ "tokio",
+]
+
+[[package]]
name = "unicode-ident"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 4ab1e0e..fa1525e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,5 +4,9 @@ version = "0.1.0"
edition = "2021"
[dependencies]
+async-stream = "0.3.3"
futures = "0.3"
+futures-util = "0.3.21"
rand = "0.8"
+tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
+tokio-stream = "0.1.9"
diff --git a/README.md b/README.md
index e863d02..c039fc1 100644
--- a/README.md
+++ b/README.md
@@ -12,3 +12,8 @@ https://docs.rs/futures/0.3.6/futures/stream/trait.Stream.html
This is the relevant chapter in the Rust Async Book - https://rust-lang.github.io/async-book/05_streams/02_iteration_and_concurrency.html
+Meanwhile it seems some features aren't fully stable in Rust just yet. Some crates provide relevant functionality
+
+https://github.com/taiki-e/futures-async-stream
+
+https://github.com/tokio-rs/async-stream
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);
+ }
}