Skip to main content

task

Attribute Macro task 

Source
#[task]
Expand description

Wraps a function body in a [Task::spawn] call, turning the function into a task in the ll task tree.

The macro looks for a parameter whose type is Task (typically task: &Task) and uses it as the parent. Inside the function body, that same name refers to the child task created by the spawn — the parent is shadowed.

The task name defaults to the function name. The return type must be Result<T> (from anyhow).

§Spawn variants

AttributeMethodFunction must be
#[task][Task::spawn]async fn
#[task(sync)][Task::spawn_sync]fn

§Optional attributes

  • data(arg1, arg2, ...) — emit task.data("arg1", arg1) at the top of the task body. Only listed arguments are logged; the task parameter itself cannot be listed.

  • tags(l2, nostatus, ...) — append #-tags to the task name. Tags control reporter visibility: #l2/#l3 mute at lower log levels, #nostatus hides from the terminal status display, #dontprint suppresses text output entirely.

  • name = "custom_name" — override the task name (defaults to the function name). Can be combined with tags(...).

Attributes can be combined: #[task(sync, data(path), tags(l2))].

§Examples

Basic async task — the most common case:

#[task]
async fn build(task: &Task) -> Result<()> {
    task.data("compiler", "rustc 1.78");
    // ... do work ...
    Ok(())
}

// caller:
build(&parent_task).await?;

Sync task with automatic data logging:

#[task(sync, data(path))]
fn check_lockfile(path: &str, task: &Task) -> Result<()> {
    // `task.data("path", path)` is emitted automatically
    Ok(())
}

Muting with tags:

#[task(tags(l2))]
async fn verbose_step(task: &Task) -> Result<()> {
    // task name: "verbose_step #l2" — only shown at log level L2+
    Ok(())
}

Nested usage — macro-wrapped functions calling each other:

#[task]
async fn deploy(task: &Task) -> Result<()> {
    provision(&task).await?;   // another #[task] fn
    restart(&task).await?;
    Ok(())
}

#[task]
async fn provision(task: &Task) -> Result<()> {
    // task tree: deploy > provision
    Ok(())
}