pub trait TaskManager:
Send
+ Sync
+ 'static {
// Required methods
fn cancel_task(&self, id: TaskId) -> ExecutorResult<()>;
fn task_status(&self, id: TaskId) -> Option<TaskStatus>;
fn wait_for_task(
&self,
id: TaskId,
timeout: Option<Duration>,
) -> impl Future<Output = ExecutorResult<()>> + Send;
fn task_stats(&self, id: TaskId) -> Option<TaskStats>;
}Expand description
Task management and monitoring capabilities.
This trait provides operations for managing and monitoring running tasks. It follows the Interface Segregation Principle by separating management concerns from spawning concerns.
§Behavior Guarantees
- All operations are thread-safe and non-blocking where possible
- Task state is eventually consistent across all observers
- Cancellation is cooperative and may not be immediate
- Statistics are updated atomically and consistently
§Performance Characteristics
- Status queries: O(1) lookup time, < 50ns typical latency
- Cancellation: O(1) operation, cooperative completion
- Statistics: Atomic operations, minimal overhead
Required Methods§
Sourcefn cancel_task(&self, id: TaskId) -> ExecutorResult<()>
fn cancel_task(&self, id: TaskId) -> ExecutorResult<()>
Cancels a running task by its ID.
§Arguments
id- The unique identifier of the task to cancel
§Returns
Ok(()) if the task was successfully cancelled or was already completed.
§Errors
Returns TaskError in the following cases:
NotFoundif no task with the given ID existsInvalidStateif the task cannot be cancelled (e.g., already completed)SystemErrorif the cancellation operation fails due to internal errors
Sourcefn task_status(&self, id: TaskId) -> Option<TaskStatus>
fn task_status(&self, id: TaskId) -> Option<TaskStatus>
Get the current status of a task.
§Behavior Guarantees
- Returns None if task ID is not found
- Status is eventually consistent across threads
- Completed tasks may be garbage collected after timeout
- Status transitions are monotonic (no backwards moves)
§Performance Characteristics
- O(1) lookup time using hash table
- Latency: < 50ns for status query
- Memory: Minimal overhead for status tracking
- Non-blocking: Never blocks calling thread
Sourcefn wait_for_task(
&self,
id: TaskId,
timeout: Option<Duration>,
) -> impl Future<Output = ExecutorResult<()>> + Send
fn wait_for_task( &self, id: TaskId, timeout: Option<Duration>, ) -> impl Future<Output = ExecutorResult<()>> + Send
Wait for a task to complete.
Returns a future that resolves when the task completes or the timeout expires. This enables async/await patterns for task coordination.
§Arguments
id: The task ID to wait fortimeout: Optional timeout duration
§Returns
A future that resolves to:
Ok(())when the task completes successfullyErr(TaskError::Timeout)if the timeout expiresErr(TaskError::NotFound)if the task doesn’t exist
§Performance
- Immediate return: < 10ns if already complete
- Waiting overhead: Event-driven, no busy polling
- Memory: Minimal waker chain overhead
Sourcefn task_stats(&self, id: TaskId) -> Option<TaskStats>
fn task_stats(&self, id: TaskId) -> Option<TaskStats>
Get statistics about task execution.
§Behavior Guarantees
- Returns None if task ID is not found or stats not enabled
- Statistics are eventually consistent
- Timing measurements use high-resolution monotonic clock
- Memory usage tracking depends on executor configuration
§Performance Characteristics
- Lookup: O(1) hash table access
- Overhead: ~100 bytes per task when metrics enabled
- Collection cost: < 5% runtime overhead when enabled
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".