| Crates.io | when2task |
| lib.rs | when2task |
| version | 0.3.1 |
| created_at | 2025-09-29 00:10:44.355159+00 |
| updated_at | 2025-09-29 21:13:17.343766+00 |
| description | high-performance library for executing async tasks with automatic dependency resolution and optimal parallelization. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1858734 |
| size | 56,950 |
A lightweight, dependency-aware task execution library for Rust that allows you to define tasks with dependencies and execute them in the correct order.
use std::time::{Duration, Instant};
use when2task::{Dependency, ExecutionMode, Task, TaskExecutor, TaskExecutorBuilder};
#[tokio::main]
async fn main() {
let start = Instant::now();
// Independent tasks that run concurrently
let task_a = Task::new_independent(async {
tokio::time::sleep(Duration::from_millis(100)).await;
println!("Task A completed after 100ms");
Ok::<&str, ()>("A")
});
let task_b = Task::new_independent(async {
tokio::time::sleep(Duration::from_millis(150)).await;
println!("Task B completed after 150ms");
Ok::<&str, ()>("B")
});
let task_a_id = *task_a.id();
let task_b_id = *task_b.id();
// Dependent task that waits for both A and B
let task_c = Task::new(
async {
tokio::time::sleep(Duration::from_millis(50)).await;
println!("Task C completed after both A and B (50ms additional)");
Ok::<&str, ()>("C")
},
Dependency::from([task_a_id, task_b_id]),
);
let builder = TaskExecutorBuilder::new(ExecutionMode::true_async());
builder.insert(task_a).insert(task_b).insert(task_c);
let executor = builder.build().unwrap();
let result = executor.execute().await.unwrap();
println!("Total execution time: {:?}", start.elapsed());
println!("Tasks completed in {} steps", result.steps.len());
// Output shows ~200ms total (150ms for step 1 + 50ms for step 2)
// demonstrating concurrent execution of A & B, then C
}
Result<T, E>This project is licensed under the MIT License - see the LICENSE file for details.