Crates.io | speedracer |
lib.rs | speedracer |
version | 0.1.2 |
source | src |
created_at | 2022-11-17 11:03:43.253089 |
updated_at | 2022-12-11 08:21:38.189626 |
description | A crate for racing futures and ranking the results |
homepage | https://github.com/SeedyROM/speedracer |
repository | https://github.com/SeedyROM/speedracer |
max_upload_size | |
id | 717119 |
size | 9,691 |
A crate for racing Future
s and getting ranked results back.
use tokio::time::sleep;
use std::time::Duration;
use speedracer::RaceTrack;
let mut race_track = RaceTrack::disqualify_after(Duration::from_millis(500));
race_track.add_racer("Racer #1", async move {
println!("Racer #1 is starting");
sleep(std::time::Duration::from_millis(100)).await;
println!("Racer #1 is ending");
Ok(())
});
race_track.add_racer("Racer #2", async move {
println!("Racer #2 is starting");
sleep(std::time::Duration::from_secs(200)).await;
println!("Racer #2 is ending");
Ok(())
});
race_track.add_racer("Racer #3", async move {
println!("Racer #3 is starting");
sleep(std::time::Duration::from_secs(700)).await;
println!("Racer #3 is ending");
Ok(())
});
race_track.run().await;
let rankings = race_track.rankings();
println!("Rankings: {:?}", rankings);
assert_eq!(rankings[0].name, "Racer #1");
assert_eq!(rankings[1].name, "Racer #2");
assert_eq!(rankings[2].name, "Racer #3");
assert_eq!(rankings[2].disqualified, true);