About the `await!` macro and possible `await` keyword addition

While futures and Option and Result all have monadic structure, neither ? nor await! is a general do notation, they are both tailored to the semantics of Option/Result and futures respectively, and consequently they differ significantly from each other:

  • ? desguars to an early return
  • await turns the surrounding function into a state machine that returns and saves its state when encountering a “not ready” future, so it can resume in the same place when polled again.
  • Neither involves and_then, for a few reasons:
    • Simply wrapping the following lines into a closure would affect control flow in there (see pre-RFC: Allow return, continue, and break to affect the captured environment)
    • It’s not as simple as putting the following lines in a closure when the ?/await is, say, inside a loop. It would effectively require transforming the code into continuation passing style, or doing the aforementioned state machine transform. That’s more or less necessary with await, but for ? it’s unnecessary overhead.
    • Doing the state machine transform is simpler when you do it on MIR rather than trying to do CPS transforms on AST, it wouldn’t literally desugar to and_then.
8 Likes