Task
Owns one single-consumer asynchronous operation without copying its frame.
Module std.task
import std.task;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
Task
pub final class Task<T>Owns one single-consumer asynchronous operation without copying its frame. T: Owned completion value; void represents no-value completion. @details Empty and moved-from handles own no frame. Dropping the last visible owner requests cancellation without blocking; runtime/native pins retain work through terminal cleanup. Await consumes a completion once without resetting the handle. Reference completion specializations are outside this package.
Example
module example.task_ownership;
from std.task import Task, SharedTask, Share;
async fn AnswerAsync() -> Task<i32> { return 42; }
async fn Main() -> Task<i32> {
var Empty = Task<i32>();
var Moved = move(Empty);
Empty = move(Moved);
if Empty.IsCompleted || Moved.IsCompleted { return 1; }
var EmptyShared = SharedTask<i32>();
var EmptyCopy = EmptyShared;
EmptyCopy = EmptyShared;
var EmptyMoved = move(EmptyCopy);
EmptyCopy = move(EmptyMoved);
if EmptyCopy.IsCompleted || EmptyMoved.IsCompleted { return 2; }
var Pending = AnswerAsync();
let Shared = Share(move(Pending));
let Copy = Shared;
let First = await Shared;
let Again = await Copy;
if First != 42 || Again != 42 || !Shared.IsCompleted { return 3; }
return 0;
}Task.init
pub init()Initializes an empty handle without allocating.
Task.init
pub init(Source: readonly Self&) = delete;Rejects duplicating a unique result consumer. Source: Existing unique handle; copying is forbidden even when empty.
Task.init
pub init(Source: mut Self&&)Moves the existing owner and leaves Source empty. Source: Explicitly moved live owner.
Task.operator
pub fn operator =(Source: readonly Self&) -> void = delete;Rejects copy assignment of a unique result consumer. Source: Existing handle whose single-consumer ownership cannot be copied. Return: No runtime result; copy assignment is rejected at compile time.
Task.operator
pub fn operator =(Source: mut Self&&) -> voidReleases the old owner and moves Source, with harmless exact self-assignment. Source: Explicitly moved owner. Return: No value.
Task.IsCompleted
pub IsCompleted: bool;Observes completed result publication without consuming it. Return: False for empty/pending handles; true after terminal publication.