I'm following the tokio tutorial about implementig Future myself, it got me wondering, if a Future is suposed to wake itself when its ready, why poll at all ? Couldn't it be assumed to be pending by default until it calls wake ?
Was it done this way to suport other types of async implementations ?
After the runtime gets a notification that the Future is ready to do more work, it has to at some later point tell the Future to continue doing work. The poll call is how you tell a Future to continue doing work.
Yeah, I just edited my response because the terminology is a bit confusing. A waker tells the runtime that the Future is "ready to continue work" whereas Poll::Ready means "the Future is done working, the answer is ready".
I guess whats really confusing me is why would a future call wake when its not ready. I assume that some work is hapening on the background, the runtime "forgets" about the future until it calls wake, the future should wait until the work is done or goes wrong before calling wake, what would be the case to awake just to say you're not done yet ?
Another way to think about this is in terms of nested futures. A low-level future ( for example, one that says a network socket has more data available) will generally be the one that actually triggers the waker, and what really should happen is that control returns to the parent async block so that it can do something with the new data. For various technical reasons, that's tricky to do directly - instead, the top level task is polled so that it can work its way through all of the futures that it's waiting on until it finds the one that has new data.
Ah yes, corrected, thanks. Implicit drop on ; strikes again. An "unused" warning was there but I ignored "unused" warnings in my example code because the function wasn't used on purpose.