From 0f538ea7fcffdbcfcdbc0904c1c02dbf9ddb3eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Krzy=C5=BCanowski?= Date: Mon, 11 Nov 2024 10:22:57 +0100 Subject: [PATCH] Add FuturesUnordered example --- Cargo.lock | 13 +++++++++++-- Cargo.toml | 2 +- futures-unordered/Cargo.toml | 9 +++++++++ futures-unordered/src/main.rs | 22 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 futures-unordered/Cargo.toml create mode 100644 futures-unordered/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 9dee7c2..5502530 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -146,6 +146,15 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" +[[package]] +name = "futures-unordered" +version = "0.1.0" +dependencies = [ + "futures", + "kameo", + "tokio", +] + [[package]] name = "futures-util" version = "0.3.31" @@ -446,9 +455,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.40.0" +version = "1.41.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" dependencies = [ "backtrace", "bytes", diff --git a/Cargo.toml b/Cargo.toml index d830d5b..8d121a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] resolver = "2" -members = ["alpha", "kameo-actors", "my-actors"] +members = ["alpha", "futures-unordered", "kameo-actors", "my-actors"] diff --git a/futures-unordered/Cargo.toml b/futures-unordered/Cargo.toml new file mode 100644 index 0000000..7aa3530 --- /dev/null +++ b/futures-unordered/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "futures-unordered" +version = "0.1.0" +edition = "2021" + +[dependencies] +futures = "0.3.31" +kameo = "0.12.2" +tokio = { version = "1.41.1", features = ["full"] } diff --git a/futures-unordered/src/main.rs b/futures-unordered/src/main.rs new file mode 100644 index 0000000..3d82473 --- /dev/null +++ b/futures-unordered/src/main.rs @@ -0,0 +1,22 @@ +async fn func_wait(name: &str, millis: u64) { + println!("{name} started"); + tokio::time::sleep(Duration::from_millis(millis)).await; + println!("{name} finished"); +} + +#[tokio::main] +async fn main() { + let actor_a = kameo::spawn(ActorA {}); + + let first = actor_a.ask(AMessage).send(); + let second = actor_a.ask(AMessage).send(); + + let mut waiter = futures_unordered::FuturesUnordered::new(); + waiter.push(func_wait("first", 2000)); + waiter.push(func_wait("second", 3000)); + waiter.push(func_wait("third", 1000)); + + while let Some(res) = waiter.next().await { + println!("{:?}", res); + } +}