Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
Development · Evolving reference

TaskScope

Supplies structured ownership for child work created inside an async body. RunAsync stores the body's result, requests remaining-child cancellation and joins all terminal cleanup before publishing that result. The supplied scope cannot be copied, moved or independently constructed by user code. It borrows its entry frame; body-local borrows still require explicit joins before their own cleanup. Scope destruction releases already-drained state without blocking. Infrastructure exhaustion follows fatal panic semantics, never early return while borrowed child storage remains in use. Cancellation is cooperative.

Sn is under heavy development. This reference includes the intended 1.0 design; features may be incomplete in the current compiler. Read the status →

Module std.task

import std.task;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

TaskScope

pub final class TaskScope

Supplies structured ownership for child work created inside an async body. RunAsync stores the body's result, requests remaining-child cancellation and joins all terminal cleanup before publishing that result. The supplied scope cannot be copied, moved or independently constructed by user code. It borrows its entry frame; body-local borrows still require explicit joins before their own cleanup. Scope destruction releases already-drained state without blocking. Infrastructure exhaustion follows fatal panic semantics, never early return while borrowed child storage remains in use. Cancellation is cooperative.

Example

module example.taskscope;
from std.task import Task, TaskScope;
async fn Body(Scope: mut TaskScope&) -> Task<i32> {
    let First = async _ => { return 20; };
    let Second = async _ => { return 22; };
    var Left = First();
    var Right = Second();
    let Result = (await Left) + (await Right);
    Scope.RequestCancellation();
    return Result;
}
async fn NoWork(Scope: mut TaskScope&) -> Task<void> { Scope.RequestCancellation(); }
async fn Main() -> Task<i32> {
    let Answer = await TaskScope.RunAsync(Body);
    await TaskScope.RunAsync(NoWork);
    let Base = 42;
    var Producer = async (Scope: mut TaskScope&) => {
        Scope.RequestCancellation(); return Base;
    };
    let Captured = await TaskScope.RunAsync<i32>(move(Producer));
    return Answer + Captured - 84;
}

TaskScope.init

pub init(Source: readonly Self&) = delete;

Prohibits duplicating scope lifecycle authority. Source: Existing scope.

TaskScope.init

pub init(Source: mut Self&&) = delete;

Keeps the supplied scope at its stable entry-frame identity. Source: Existing scope.

TaskScope.operator

pub fn operator =(Source: readonly Self&) -> void = delete;

Prohibits replacing the entry's scope authority. Source: Existing scope. Return: No runtime result; this assignment is deleted and cannot be invoked.

TaskScope.operator

pub fn operator =(Source: mut Self&&) -> void = delete;

Prohibits moving another scope into this active entry. Source: Existing scope. Return: No runtime result; this assignment is deleted and cannot be invoked.

TaskScope.RequestCancellation

pub readonly fn RequestCancellation() -> void

Requests cancellation of current and subsequently admitted child work. Note: Does not block, force completion or close admission. Return: No value; cancellation is requested without waiting or closing child admission.

TaskScope.RunAsync

pub static async fn RunAsync<T>(Body: fn(mut Self&) -> Task<T>) -> Task<T> where T: Movable

Runs an async body and drains all owned children before publishing its value. T: Owned Movable body completion. Body: Safe body borrowing supplied scope. Return: Unique task preserving the exact body result, including ordinary Result errors. Warning: External/body-local backing must obey async lifetime rules; scope join cannot extend a body local beyond that body's return.

TaskScope.RunAsync

pub static async fn RunAsync(Body: fn(mut Self&) -> Task<void>) -> Task<void>

Runs a void async body and drains its children before completing. Body: Safe body borrowing supplied scope. Return: Unique void completion after terminal cleanup.

TaskScope.RunAsync

pub static async fn RunAsync<T, C>(Body: C) -> Task<T>
        where T: Movable where C: UnaryCallable<mut Self&, Task<T>>, Movable

Owns a concrete async callback through body execution and child drain. T: Owned Movable completion. C: Movable safe callback with exact mutable scope argument. Body: Owned callback. Return: Body result after cleanup.

TaskScope.RunAsync

pub static async fn RunAsync<C>(Body: C) -> Task<void>
        where C: UnaryCallable<mut Self&, Task<void>>, Movable

Owns a concrete void async callback through child drain. C: Movable safe callback. Body: Owned callback borrowing supplied scope. Return: Void completion after cleanup.

Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library