Future
async/await
Pin
Send
Future
Future
is something you can poll
, trying to make progress, and won’t block if stucked (yield control). And it will call wake_up
if progress can be made.
pub trait Future {
type Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
}
async/await
how it works?
- the executor will repeatedly fetch a
Task
from the queue, try to make progress on aFuture
by callingpoll
- if it returned
Pending
then save it back to theTask
- when the future can make any progress, the
wake
will be called, then theTask
itself will be pushed to the task queue again, so that the executor will be able topoll
it agagin in the near future.
Pin
use Pin
to avoid an object being moved, used here as the state-machine in the Future
may reference itself.
Send
Send
means transferring control between threads, not move
-ing objects from one place to another. So it is nothing related to Pin