CancellationSource
Owns move-only cooperative cancellation authority with independently retained tokens. Creation allocates native request/binding state. Token access allocates nothing. Destruction releases authority without requesting cancellation. Requesting is thread-safe through distinct retained references; moving the same handle needs external serialization. Tokens do not imply completion or side-effect rollback.
Module std.task
import std.task;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
CancellationSource
pub final class CancellationSourceOwns move-only cooperative cancellation authority with independently retained tokens. Creation allocates native request/binding state. Token access allocates nothing. Destruction releases authority without requesting cancellation. Requesting is thread-safe through distinct retained references; moving the same handle needs external serialization. Tokens do not imply completion or side-effect rollback.
Example
module example.cancellation;
from std.task import CancellationSource, CancellationToken;
fn Main() -> i32 {
var Source = {
// Keep writable result storage through the explicit noncopy payload transfer.
var SourceCreation = CancellationSource.TryCreate();
switch SourceCreation {
case .Ok { Value = Value: mut CancellationSource& } => break move(Value);
case .Err { Error = _ } => return 1;
}
};
let Token = Source.Token;
var Copy = Token;
Copy = Token;
var Moved = move(Source);
if Source.RequestCancellation() || !Moved.RequestCancellation() { return 2; }
if Moved.RequestCancellation() || !Token.IsCancellationRequested || !Copy.CanBeCancelled { return 3; }
var Default = CancellationToken();
Default = move(Copy);
if Copy.CanBeCancelled || !Default.IsCancellationRequested { return 4; }
return 0;
}CancellationSource.init
pub init()Initializes inert authority without allocation.
CancellationSource.init
pub init(Source: readonly Self&) = delete;Prohibits duplicating authority. Source: Existing authority.
CancellationSource.init
pub init(Source: mut Self&&)Transfers authority and leaves Source inert. Source: Explicit moved source.
CancellationSource.operator
pub fn operator =(Source: readonly Self&) -> void = delete;Prohibits copying authority. Source: Existing authority. Return: No runtime result; this assignment is deleted and cannot be invoked.
CancellationSource.operator
pub fn operator =(Source: mut Self&&) -> voidReleases old authority then transfers Source, preserving exact self-move. Source: Moved authority. Return: No value after transferring cancellation authority; exact self-move is unchanged.
CancellationSource.TryCreate
pub static fn TryCreate() -> Result<Self, CancellationError>Creates checked cancellation authority. Return: Owned source or allocation/startup error.
CancellationSource.Token
pub Token: CancellationToken;Retains an independent token without allocation. Return: Inert token for default/moved authority.
CancellationSource.RequestCancellation
pub readonly fn RequestCancellation() -> boolPublishes cooperative cancellation synchronously. Return: True only for the first request; false for inert/repeated.