jonhoo / rust-for-rustaceans.com

Source for https://rust-for-rustaceans.com/

Home Page:https://rust-for-rustaceans.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Questions about the notion ‘the something, that calls `Future::poll` again at a later time, is the executor’

joelposti opened this issue · comments

I am reading through the chapter 8 Asynchronous Programming. I have to say that the chapter is very well and thoroughly written. This chapter must have been quite difficult to write since the topic is so complex and has a lot of concepts to explain.

On page 133 a section called ‘Going to Sleep’ begins by saying that the something, that calls Future::poll at a later time, is the executor. This confused me. Upon arriving to this section the mental model I had in my head was that async fn is desugared to a generator function in listing 8-6 and await is desugared to code in listing 8-12. So, according to these listings it would seem like the something, that calls poll, is a loop in the generator function rather than the executor. In this model the executor would just be responsible for calling next/resume on the generator instance.

An hour or so of thinking about this, it occured to me that likely the generator function in listing 8-6 is further desugared to code in listing 8-4. Is listing 8-4 the source of truth? Is that the true Future whose poll method the executor calls? Some kind of refresher or clarification about what a Future truly is and which code listing should be referred to would be welcome in the ‘Going to Sleep’ section. As a reader I just had emerged from the Pin shrubbery, and frankly my brain had ran out of RAM and dropped earlier information in the chapter.

Sure, you can think of the problem as being "what calls next on the generator" — that's sort of synonymous. The point is more that something has to resume the async fn/generator fn when it yields (i.e., returns Poll::Pending for async fn). A generator is really just a general (i.e., not tied to Future) version of an async fn; it doesn't actually "handle" calls to poll.

Okay, good to know that ”what calls next on the generator" is somewhat synonymous. I found thinking of Futures as generator functions easier as those are familiar to me from other programming languages. Your answer clarified things for me.