Skip to main content

auralis_task/
lib.rs

1//! auralis-task: Scoped async task runtime with explicit [`TaskScope`]
2//! hierarchy, iterative cancellation, and priority scheduling.
3//!
4//! # Architecture
5//!
6//! - **Global executor** — a [`thread_local!`] singleton with
7//!   high- and low-priority queues.
8//! - **`TaskScope`** — owns spawned futures; dropping a scope cancels
9//!   all descendant scopes and their tasks **iteratively** (no
10//!   recursion), preventing stack overflows in deeply nested UI trees.
11//! - **`set_deferred`** — safe signal mutation from [`Drop`] contexts.
12//! - **Pluggable storage** — [`ScopeStore`] and [`Executor::new_instance`]
13//!   enable multi-request isolation for SSR or multi-threaded runtimes.
14//!
15//! # Quick example
16//!
17//! ```
18//! use auralis_task::{TaskScope, spawn_global};
19//!
20//! let scope = TaskScope::new();
21//! scope.spawn(async { /* ... */ });
22//! ```
23//!
24//! # Design decisions
25//!
26//! The crate shares `auralis_signal`'s single-threaded-by-default
27//! philosophy (`Rc` over `Arc`, `RefCell` over `Mutex`).  For
28//! multi-threaded use-cases, create an isolated [`Executor`] instance
29//! per thread or per request.  See the repository design docs for the
30//! full rationale.
31
32#![forbid(unsafe_code)]
33#![warn(missing_docs, clippy::all, clippy::pedantic)]
34#![allow(clippy::module_name_repetitions)]
35
36mod executor;
37mod scope;
38pub mod timer;
39
40#[cfg(feature = "debug")]
41mod debug;
42
43#[cfg(feature = "debug")]
44pub use debug::dump_task_tree;
45pub use executor::{
46    init_flush_scheduler, init_time_source, remove_panic_hook, reset_executor_for_test,
47    schedule_callback, set_deferred, set_global_time_budget, set_panic_hook, spawn_global,
48    spawn_global_with_priority, with_executor, yield_now, Executor, PanicInfo, ScheduleFlush,
49    TimeSource, YieldNow,
50};
51#[cfg(test)]
52pub use executor::{TestScheduleFlush, TestTimeSource};
53#[cfg(feature = "debug")]
54pub use scope::scope_debug_label;
55pub use scope::{
56    clear_scope_registry, current_scope, find_scope, set_scope_store, with_current_scope,
57    CallbackHandle, ScopeStore, TaskScope,
58};
59
60#[cfg(feature = "ssr-tokio")]
61pub use scope::init_scope_store_tokio;
62
63/// Task priority.
64///
65/// High-priority tasks are dequeued before low-priority ones within a
66/// single flush cycle.
67#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
68pub enum Priority {
69    /// Dequeued first during flush.
70    High,
71    /// Dequeued after all high-priority tasks.
72    Low,
73}