Lyricdev
GitHub

class TaskScope

pub class TaskScope :: [Closeable]

Conforms to Closeable.

A scope whose tasks end before it does: structured concurrency.

A scope is Closeable — using let scope = TaskScope.new(); — and its TaskScope.close waits until every task it started has ended. The first of them that fails cancels the others, and close() throws that error once they have all ended; a task that ends with Cancelled is no failure of the scope. A task that panics does the same, and close() panics again with it. A task waiting in close() that is cancelled cancels the scope's tasks and still waits for them.

Methods

pub static fn new(): TaskScope

Creates a scope with no tasks yet.

pub mut fn spawn<T, E :: [Error]>(body: fn() -> T throws E): (Task<T> throws E)

Starts body as a task of this scope and returns its handle, as spawn does: close() waits for it, and a failure of it cancels the other tasks of the scope.

A task given while close() waits is a child like any, and the wait takes it in.

Panics

When the scope is closed, since nothing would wait for the task.

pub fn cancel(): void

Cancels every task of the scope that has not ended.

pub fn close(): void throws Error

Waits until every task of the scope has ended, then throws the first failure, if any.

A task of the scope that panicked makes this panic again, with that panic's code, message and trace.

Errors

Throws the error the first failed task of the scope ended with.