Coroutines

Coroutines

Generators, futures and suspension.

Contents

NameSummary
FutureThe result type of spawn f(): a handle to a value of type T that is still being computed.
YieldPromiseThe coroutine promise behind a generator: C++ coroutine plumbing for Yield<T>.
YieldThe handle a generator function returns: the Yield lang item, the target of yield.
SuspendThe awaiter every suspension point uses: it always suspends and does nothing on suspend or resume.

Future

struct <T> Future

The result type of spawn f(): a handle to a value of type T that is still being computed.

Layout only for now: state points at the task's shared state.

YieldPromise

class <T> YieldPromise

The coroutine promise behind a generator: C++ coroutine plumbing for Yield<T>.

yield v is lowered to yield_value, which records the address of v in cur. The compiler and clang's coroutine machinery call the other members; user code does not.

YieldPromise.cur

var cur: *T

The address of the most recently yielded value; valid until the generator resumes.

YieldPromise.get_return_object

inline fn get_return_object(self) -> Yield<T>

Builds the Yield<T> handle for this coroutine's frame.

YieldPromise.initial_suspend

inline fn initial_suspend(self) -> Suspend

Suspends at the start, so nothing runs until the first next().

YieldPromise.yield_value

inline fn yield_value(self, @inout v: T) -> Suspend

Handles yield v: records the address of v and suspends.

Parameters

  • v: Taken @inout: the reader sees the generator's own value, not a copy.

YieldPromise.return_void

inline fn return_void(self)

Handles falling off the end of the generator body.

YieldPromise.unhandled_exception

inline fn unhandled_exception(self)

Traps (__builtin_trap): generators do not propagate exceptions.

Yield

class <T> Yield

The handle a generator function returns: the Yield lang item, the target of yield.

A function containing yield returns one; a for loop over it drives next() and reads current(). Move-only; destroying the handle destroys the coroutine frame.

See also

YieldPromise

Yield.promise_type

type promise_type = YieldPromise<T>

The promise type clang's coroutine machinery looks up.

Yield.Yield

inline fn Yield(self, f: unsafe *void)

Wraps a coroutine frame; called by YieldPromise::get_return_object.

Suspend

class Suspend

The awaiter every suspension point uses: it always suspends and does nothing on suspend or resume.

clang requires the awaiter of final_suspend to be non-throwing in all three calls, so all three are marked @no_unwind.

Generated from Kairo source, Apache-2.0 WITH AdditionRef-Kairo-Runtime-exception.