Skip to main content

SupervisedTask

Trait SupervisedTask 

Source
pub trait SupervisedTask: Send + 'static {
    // Required method
    fn run(&mut self) -> impl Future<Output = TaskResult> + Send;
}
Expand description

The trait users implement for tasks managed by the supervisor.

§Clone and restart semantics

The supervisor stores the original instance and clones it for each run. Mutations via &mut self only live in the clone and are lost on restart. Shared state (Arc<...>) survives because Clone just bumps the refcount.

Use owned fields for per-run state, Arc for cross-restart state.

§Example

use task_supervisor::{SupervisedTask, TaskResult};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

#[derive(Clone)]
struct MyTask {
    /// Reset to 0 on every restart (owned, cloned from original).
    local_counter: u64,
    /// Shared across restarts (Arc, cloned by reference).
    total_runs: Arc<AtomicUsize>,
}

impl SupervisedTask for MyTask {
    async fn run(&mut self) -> TaskResult {
        self.total_runs.fetch_add(1, Ordering::Relaxed);
        self.local_counter += 1;
        // local_counter is always 1 here — fresh clone each restart.
        Ok(())
    }
}

Required Methods§

Source

fn run(&mut self) -> impl Future<Output = TaskResult> + Send

Runs the task until completion or failure.

Mutations to &mut self are not preserved across restarts. See the trait-level docs for details.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§