Skip to main content

Module task

Module task 

Source
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§

BaseTask
Base implementation for common task patterns to reduce redundancy.
BlockingResultWait
Zero-sized blocking wait policy: spins up to MAX_SPIN_ATTEMPTS then parks.
Chained
A task that chains two operations together.
Closure
A simple closure-based task implementation.
ContextualTask
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.
TaskBuilder
Builder for creating and configuring tasks.
TaskContext
Task execution context and metadata.
TaskFuture
A future adapter that executes a Task on first poll.
TaskHandle
A handle to a task that may be running on another thread.
TaskId
A unique identifier for tasks in the Moirai runtime.
TaskResultSender
Single-producer completion endpoint for a task result.

Enums§

Priority
Priority levels for task scheduling.

Traits§

ResultWaitPolicy
Compile-time wait policy for task result handoff.
Task
The core trait for executable tasks in the Moirai runtime.
TaskExt
Extension methods for tasks.