Skip to main content

taskgraph/
lib.rs

1//! # taskgraph-rs
2//! A NASA-grade, zero-deps, no_std DAG task orchestrator in Rust.
3//!
4//! Features:
5//! - **Zero-Unsafe**: `#[forbid(unsafe_code)]` for maximum safety.
6//! - **No-Std**: Fully compatible with embedded systems and bare-metal.
7//! - **Scalable**: From 1KB RAM static graphs to dynamic allocation via feature tags.
8
9#![cfg_attr(not(feature = "std"), no_std)]
10#![forbid(unsafe_code)]
11
12pub mod error;
13pub mod core;
14pub mod storage;
15pub mod executor;
16pub mod utils;
17
18pub use crate::error::{TaskError, Result};
19pub use crate::core::task::{Task, TaskStatus, TaskStore, TaskBody};
20pub use crate::storage::static_store::StaticStore;
21
22#[cfg(feature = "alloc")]
23pub use crate::storage::dynamic_store::DynamicStore;
24
25pub use crate::executor::sync::SyncExecutor;
26
27#[cfg(feature = "async")]
28pub use crate::executor::async_exec::AsyncExecutor;
29
30pub use crate::core::scheduler::validate_graph;
31pub use crate::core::events::{TaskEvent, TaskSubscriber};
32
33/// The main entry point for creating a task graph.
34pub struct TaskGraph<S: TaskStore> {
35    storage: S,
36}
37
38impl<S: TaskStore> TaskGraph<S> {
39    /// Create a new task graph with a specific storage backend.
40    pub fn new(storage: S) -> Self {
41        Self { storage }
42    }
43
44    /// Add a task to the graph.
45    pub fn task(&mut self, task: Task, deps: &[usize]) -> Result<usize> {
46        self.storage.add_task(task, deps)
47    }
48
49    /// Run the graph (Sync implementation).
50    pub fn run_sync(&mut self) -> Result<()> {
51        validate_graph(&mut self.storage)?;
52        let mut executor = SyncExecutor::new(&mut self.storage);
53        executor.run()
54    }
55
56    /// Run the graph (Async implementation).
57    #[cfg(feature = "async")]
58    pub async fn run_async(self) -> Result<()> 
59    where S: Send + Sync + 'static
60    {
61        let mut storage = self.storage;
62        validate_graph(&mut storage)?;
63        let executor = AsyncExecutor::new(storage);
64        executor.run().await
65    }
66}
67
68/// Convenience type for a static task graph.
69pub type StaticGraph<const N: usize> = TaskGraph<StaticStore<N>>;
70
71#[cfg(feature = "alloc")]
72/// Convenience type for a dynamic task graph.
73pub type DynamicGraph = TaskGraph<DynamicStore>;