Future API Reference

Stackless coroutine-based async/await with future combinators

Import

U std/future

Core Types

Poll

E Poll {
    Pending,
    Ready(i64)
}

Result type for polling futures.

MethodSignatureDescription
is_readyF is_ready(&self) -> i64Returns 1 if Ready, 0 otherwise
is_pendingF is_pending(&self) -> i64Returns 1 if Pending, 0 otherwise
unwrapF unwrap(&self) -> i64Get value (0 if Pending, should panic)

Future (trait)

W Future {
    F poll(&self, ctx: i64) -> Poll
}

Trait for asynchronous values. Types implementing this can be polled for completion.

Context

S Context {
    waker_ptr: i64,
    runtime_ptr: i64
}

Context passed to poll operations, contains waker and runtime information.

MethodSignatureDescription
newF new() -> ContextCreate empty context
with_runtimeF with_runtime(runtime_ptr: i64) -> ContextCreate context with runtime pointer
wakeF wake(&self) -> i64Wake up the task associated with this context

Waker

S Waker {
    task_ptr: i64,
    wake_fn: i64
}

Mechanism to wake up a suspended task.

MethodSignatureDescription
newF new(task_ptr: i64, wake_fn: i64) -> WakerCreate waker with task and wake function
wakeF wake(&self) -> i64Signal the runtime that this task is ready

Future Combinators

MapFuture

S MapFuture {
    inner_ptr: i64,     # Pointer to inner future
    inner_poll: i64,    # Poll function of inner future
    map_fn: i64,        # Mapping function pointer
    state: i64          # 0 = not polled, 1 = complete
}

Transforms the output of a future using a mapping function.

MethodSignatureDescription
newF new(inner_ptr: i64, inner_poll: i64, map_fn: i64) -> MapFutureCreate map combinator

AndThenFuture

S AndThenFuture {
    first_ptr: i64,     # First future
    first_poll: i64,
    second_fn: i64,     # Function that creates second future from first result
    state: i64,         # 0 = running first, 1 = running second
    second_ptr: i64,    # Created second future (when state = 1)
    second_poll: i64
}

Chains futures sequentially - runs second future after first completes.

MethodSignatureDescription
newF new(first_ptr: i64, first_poll: i64, second_fn: i64) -> AndThenFutureCreate sequential chain

JoinFuture

S JoinFuture {
    first_ptr: i64,
    first_poll: i64,
    second_ptr: i64,
    second_poll: i64,
    first_done: i64,
    second_done: i64,
    first_result: i64,
    second_result: i64
}

Runs two futures concurrently, completes when both finish.

MethodSignatureDescription
newF new(first_ptr: i64, first_poll: i64, second_ptr: i64, second_poll: i64) -> JoinFutureCreate join combinator

SelectFuture

S SelectFuture {
    first_ptr: i64,
    first_poll: i64,
    second_ptr: i64,
    second_poll: i64
}

Returns when either future completes (race condition).

MethodSignatureDescription
newF new(first_ptr: i64, first_poll: i64, second_ptr: i64, second_poll: i64) -> SelectFutureCreate select combinator

ReadyFuture

S ReadyFuture {
    value: i64
}

Future that immediately resolves to a value.

MethodSignatureDescription
newF new(value: i64) -> ReadyFutureCreate immediately-ready future

PendingFuture

S PendingFuture {
    _dummy: i64
}

Future that never resolves (always returns Pending).

MethodSignatureDescription
newF new() -> PendingFutureCreate never-resolving future

TimerFuture

S TimerFuture {
    deadline: i64,      # Target tick count
    started: i64
}

Future that completes after N iterations.

MethodSignatureDescription
newF new(ticks: i64) -> TimerFutureCreate timer future

YieldNow

S YieldNow {
    yielded: i64
}

Cooperative scheduling yield point. Returns Pending on first poll, Ready on second.

MethodSignatureDescription
newF new() -> YieldNowCreate yield point

AsyncDrop Support

AsyncDrop (trait)

W AsyncDrop {
    A F async_drop(&self) -> i64
}

Trait for types that need async cleanup when they go out of scope.

AsyncDropGuard

S AsyncDropGuard {
    value_ptr: i64,         # Pointer to the value
    drop_fn: i64,           # Async drop function pointer (poll-based)
    dropped: i64            # 1 if already dropped
}

Wraps a value implementing AsyncDrop and ensures async_drop is called.

MethodSignatureDescription
newF new(value_ptr: i64, drop_fn: i64) -> AsyncDropGuardCreate guard for value
drop_asyncA F drop_async(&self) -> i64Manually trigger async drop
is_droppedF is_dropped(&self) -> i64Check if already dropped
getF get(&self) -> i64Get wrapped value pointer

AsyncDropScope

S AsyncDropScope {
    head: i64,          # First guard in linked list
    count: i64          # Number of guards
}

Manages multiple AsyncDrop resources. All resources are dropped in reverse order (LIFO) when scope ends.

MethodSignatureDescription
newF new() -> AsyncDropScopeCreate empty scope
registerF register(&self, value_ptr: i64, drop_fn: i64) -> AsyncDropGuardRegister resource for async drop
drop_allF drop_all(&self) -> i64Drop all resources in reverse order, returns error count
lenF len(&self) -> i64Get number of registered resources

Helper Functions

FunctionSignatureDescription
readyF ready(value: i64) -> ReadyFutureCreate immediately-ready future
pendingF pending() -> PendingFutureCreate never-resolving future
joinF join(first_ptr: i64, first_poll: i64, second_ptr: i64, second_poll: i64) -> JoinFutureJoin two futures (both must complete)
selectF select(first_ptr: i64, first_poll: i64, second_ptr: i64, second_poll: i64) -> SelectFutureSelect first completing future
delayF delay(ticks: i64) -> TimerFutureCreate timer that completes after n iterations
yield_nowF yield_now() -> YieldNowCreate yield point for cooperative scheduling
async_drop_guardF async_drop_guard(value_ptr: i64, drop_fn: i64) -> AsyncDropGuardCreate async drop guard
async_drop_scopeF async_drop_scope() -> AsyncDropScopeCreate async drop scope

Usage

Basic Future

U std/future

F main() -> i64 {
    # Create immediately-ready future
    f := ready(42)

    # In async context: poll the future
    ctx := Context::new()
    result := f.poll(ctx)

    I result.is_ready() {
        value := result.unwrap()  # value = 42
    }
    0
}

Join Futures

U std/future

F main() -> i64 {
    f1 := ready(10)
    f2 := ready(20)

    # Join both futures
    joined := join(f1_ptr, f1_poll, f2_ptr, f2_poll)

    # Poll until both complete
    0
}

Async Drop

U std/future

# Example: Connection with async cleanup
S MyConn {
    handle: i64
}

X MyConn: AsyncDrop {
    A F async_drop(&self) -> i64 {
        # Close connection asynchronously
        close_connection(self.handle)
    }
}

F main() -> i64 {
    scope := async_drop_scope()

    # Register resource for async cleanup
    conn := MyConn { handle: open_connection() }
    guard := scope.register(conn_ptr, drop_fn)

    # Do work...

    # All resources cleaned up in reverse order
    scope.drop_all()
    0
}