Discovering self-referential structs
What happened is that we created a self-referential struct, initialized it so that it took a pointer to itself, and then moved it. Let’s take a closer look:
- First, we received a future object as an argument to
block_on
. This is not a problem since the future isn’t self-referential yet, so we can move it around wherever we want to without issues (this is also why moving futures before they’re polled is perfectly fine using proper async/await). - Then, we polled the future once. The optimization we did made one essential change. The future was located on the stack (inside the stack frame of our
block_on
function) when we polled it the first time. - When we polled the future the first time, we initialized the variables to their initial state. Our
writer
variable took a pointer to ourbuffer
variable (stored as a part of our coroutine) and made it self-referential at this point. - The first time we polled the future...