Macro job_fn

Source
macro_rules! job_fn {
    (
        { $($setup_stmts:stmt);* $(;)? } // Setup block (optional contents)
        $main_block:block                 // Main logic block
    ) => { ... };
    (
        $main_block:block // Main logic block
    ) => { ... };
}
Expand description

Macro to simplify creating a BoxedExecFn compatible closure.

Takes an optional synchronous setup block and a mandatory async logic block. Handles the necessary boxing (Box::new, Box::pin).

§Usage

// With setup block:
let job_fn_1 = turnkey_job_fn! {
    // Optional setup block runs immediately when outer closure is created.
    {
        let job_counter = counter.clone(); // Clone Arcs here
        let local_msg = message;         // Move owned data here
        println!("Setup block executed");
    }
    // Main logic block (implicitly wrapped in `async move`)
    // Captures variables defined in the setup block.
    {
        let count = job_counter.fetch_add(1, Ordering::SeqCst) + 1;
        println!("Job executing (Count: {}): {}", count, local_msg);
        tokio::time::sleep(Duration::from_millis(10)).await;
        true // Must evaluate to bool
    }
};

// Without setup block:
let job_fn_2 = turnkey_job_fn! {
    // Main logic block only
    {
        println!("Simple job executing");
        tokio::time::sleep(Duration::from_millis(5)).await;
        true
    }
};