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

AsyncIterator

Defines an incremental, cancellation-aware task-producing cursor. Each mutable step is awaited before the next. Some yields the exact owned or borrowed item category; none is terminal EOF. Errors retain source-specific retry policy. Borrowed items retain normal origin/suspension/invalidation rules. Structural satisfaction adds no runtime box, collection or implicit executor. A safe ordinary method returning the exact task shape also satisfies this protocol. Implementations document retention, affinity and step costs.

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.iteration

import std.iteration;

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

AsyncIterator

pub concept AsyncIterator<Self>

Defines an incremental, cancellation-aware task-producing cursor. Each mutable step is awaited before the next. Some yields the exact owned or borrowed item category; none is terminal EOF. Errors retain source-specific retry policy. Borrowed items retain normal origin/suspension/invalidation rules. Structural satisfaction adds no runtime box, collection or implicit executor. A safe ordinary method returning the exact task shape also satisfies this protocol. Implementations document retention, affinity and step costs. Self: Concrete mutable traversal state.

Example

module example.async_iterator;
from std.sync import Channel, ChannelError;
from std.iteration import CountAsync, AsyncIterable;
from std.task import Task;
fn AcceptSource<S>(Source: readonly S&) -> bool where S: AsyncIterable { return true; }
async fn Main() -> Task<i32> {
    let Source = {
        // The scoped Result keeps its exact writable payload until transfer.
        var SourceCreation = Channel<i32>.TryCreate(2);
        switch SourceCreation {
            case .Ok { Value = Value: mut Channel<i32>& } => break move(Value);
            case .Err => return 1;
        }
    };
    if !AcceptSource(Source) { return 2; }
    if Source.TrySend(20) is .Err { Error = _ } { return 3; }
    if Source.TrySend(22) is .Err { Error = _ } { return 4; }
    if Source.Close() is .Err { Error = _ } { return 5; }
    var Cursor = Source.CreateAsyncIterator();
    switch await Cursor.NextAsync() {
        case .Ok { Value = PatternPayload1 } => {
            switch PatternPayload1 {
                case .Some { Value = Value } => { if Value != 20 { return 6; } }
                default => return 7;
            }
        }
        default => return 7;
    }
    switch await CountAsync<i32, ChannelError>(move(Cursor)) {
        case .Ok { Value = Count } => { if Count != 1 { return 8; } }
        case .Err => return 9;
    }
    return 0;
}

AsyncIterator.Item

associated type Item;

Exact owned or borrowed item category.

AsyncIterator.Error

associated type Error;

Exact source-specific step error.

AsyncIterator.NextAsync

requires fn NextAsync(Token: CancellationToken) -> Task<Result<Item?, Error>>;

Starts one incremental step without overlapping another mutable step. Token: Owned cancellation observation for this step. Return: Task yielding optional item/EOF or exact source-specific failure.

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