How Rust optimizes async/await II: Program analysis
In Part 1, we covered how async fns in Rust are compiled to state machines. We saw that the internal compiler implementation uses generators and the yield statement to facilitate this transformation. We also saw that the optimal way to lay out one of these state machines in memory is using an enum-like representation, similar to the following:
enum SumGenerator{Unresumed{xs: Vec<i32>},Suspend0{xs: Vec<i32>,iter0: Iter<'self,i32>,sum: i32 },Suspend1{xs: Vec<i32>,iter1: Iter<'self,i32>,sum: i32 },Returned}Each variable stored in this enum is a local variable of our original function, needed to track the internal state of our state machine.
Tyler Mandry