back to the future
cargo devel CI / cargo CI (push) Successful in 1m55s Details

This commit is contained in:
Christoph J. Scherr 2024-02-19 11:53:39 +01:00
parent df6204d7c4
commit 1e6f98595b
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
4 changed files with 55 additions and 0 deletions

8
Cargo.lock generated
View File

@ -517,6 +517,14 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "future_stream"
version = "0.1.0"
dependencies = [
"futures",
"tokio",
]
[[package]]
name = "futures"
version = "0.3.30"

View File

@ -13,6 +13,7 @@ members = [
"members/tokryon",
"members/tokio-send-sync",
"members/matchmatchmatch",
"members/future_stream",
]
default-members = [
".",
@ -28,6 +29,7 @@ default-members = [
"members/tokryon",
"members/tokio-send-sync",
"members/matchmatchmatch",
"members/future_stream",
]
[workspace.dependencies]
@ -42,6 +44,7 @@ tokio = { version = "1.35.1", features = [
"time",
"sync",
] }
futures = { version = "0.3.30", features = ["executor"] }
[package]
name = "rs-basic"

View File

@ -0,0 +1,10 @@
[package]
name = "future_stream"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio.workspace = true
futures.workspace = true

View File

@ -0,0 +1,34 @@
// see https://stackoverflow.com/questions/70774671/tokioselect-but-for-a-vec-of-futures
use futures::{stream::FuturesUnordered, StreamExt};
use std::time::Duration;
use tokio::time::{sleep, Instant};
async fn wait(millis: u64) -> u64 {
sleep(Duration::from_millis(millis)).await;
millis
}
#[tokio::main]
async fn main() {
let mut futures = FuturesUnordered::new();
futures.push(wait(500));
futures.push(wait(300));
futures.push(wait(100));
futures.push(wait(200));
let start_time = Instant::now();
let mut num_added = 0;
while let Some(wait_time) = futures.next().await {
println!("Waited {}ms", wait_time);
if num_added < 3 {
num_added += 1;
futures.push(wait(200));
}
}
println!(
"Completed all work in {}ms",
start_time.elapsed().as_millis()
);
}