Expand description
§Task Abstraction Layer
This module provides the core task abstractions for the Moirai concurrency library. All task types are designed to be zero-cost abstractions that compile away to optimal code.
§Safety Guarantees
- Memory Safety: All task operations are memory-safe by construction
- Data Race Freedom: Rust’s ownership system prevents data races
- Resource Cleanup: Automatic resource cleanup on task completion or panic
- Type Safety: Generic type system ensures compile-time correctness
§Performance Characteristics
- Task Creation: O(1) constant time with zero allocations for simple closures
- Task Execution: Zero-cost abstractions compile to direct function calls
- Memory Overhead: < 64 bytes per task for metadata and context
- Cache Efficiency: Task data structures are cache-line aligned
§Examples
§Basic Task Creation
use moirai_core::{Task, TaskBuilder, Priority};
// Simple closure task
let task = TaskBuilder::new()
.priority(Priority::Normal)
.name("computation")
.build(|| {
(1..=100).sum::<i32>()
});
assert_eq!(task.execute(), 5050);§Task Chaining and Composition
ⓘ
use moirai_core::{TaskBuilder, TaskExt, Task};
let base_task = TaskBuilder::new().build(|| 21);
// Chain operations
let doubled = base_task.then(|x| x * 2);
let result = doubled.execute();
assert_eq!(result, 42);
// Map transformations
let mapped = TaskBuilder::new().build(|| "hello")
.map(|s| s.to_uppercase());
assert_eq!(mapped.execute(), "HELLO");§Error Handling
Task::execute has no error channel: fallible work returns a Result as
its Output and callers branch on the value. Panic recovery is the
executor’s responsibility (the hybrid executor catches unwinds at the job
boundary), not a task-combinator concern.
Structs§
- Base
Task - Base implementation for common task patterns to reduce redundancy.
- Blocking
Result Wait - Zero-sized blocking wait policy: spins up to
MAX_SPIN_ATTEMPTSthen parks. - Chained
- A task that chains two operations together.
- Closure
- A simple closure-based task implementation.
- Contextual
Task - A task with an explicit context.
- Group
- A collection of related tasks that can be executed as a group.
- Mapped
- A task that maps the output of another task.
- Parameterized
- A task that accepts parameters for customized execution.
- Spawner
- A task that can spawn other tasks during its execution.
- Task
Builder - Builder for creating and configuring tasks.
- Task
Context - Task execution context and metadata.
- Task
Future - A future adapter that executes a
Taskon first poll. - Task
Handle - A handle to a task that may be running on another thread.
- TaskId
- A unique identifier for tasks in the Moirai runtime.
- Task
Result Sender - Single-producer completion endpoint for a task result.
Enums§
- Priority
- Priority levels for task scheduling.
Traits§
- Result
Wait Policy - Compile-time wait policy for task result handoff.
- Task
- The core trait for executable tasks in the Moirai runtime.
- TaskExt
- Extension methods for tasks.