C+
Systems · v0.0.13

Async / await

import "stdlib/future" as future;
import "stdlib/executor" as executor;

async fn inner() -> i32 { return 7; }

async fn outer() -> i32 {
    let x: i32 = await inner();
    return x +% 1;
}

fn main() -> i32 {
    let f: future::Future[i32] = outer();
    return executor::block_on::[i32](f);
}

Signature rules

  • An async fn returns Future[T], written as the bare T in the signature.
  • await EXPR suspends until EXPR (a future) resolves and yields its value.
  • Borrow-shaped parameters (str, T[], mut x: NonCopyT) are rejected in an async fn signature (E0900), because a suspension can outlive the caller's frame. Pass string and Vec[T] instead.

Reactor: concurrent I/O

The reactor (kqueue on macOS) makes async actually concurrent rather than just structured. It provides cooperative primitives over stdlib/executor, stdlib/reactor, and stdlib/time, so awaiting a timer or socket yields the executor to other ready tasks instead of blocking the thread.