Crate noop_waker[][src]

Expand description

A waker that does nothing when it is woken. Useful for “now or never” type scenarioes where a future is unlikely to be polled more than once, or for “spinning” executors.


A very inefficient implementation of the block_on function that polls the future over and over.

use core::{future::Future, hint::spin_loop, task::{Context, Poll}};
use futures_lite::future::poll_fn;
use noop_waker::noop_waker;
fn block_on<R>(f: impl Future<Output = R>) -> R {
    // pin the future to the stack
    // create the context
    let waker = noop_waker();
    let mut ctx = Context::from_waker(&waker);
    // poll future in a loop
    loop {
        match f.as_mut().poll(&mut ctx) {
            Poll::Ready(o) => return o,
            Poll::Pending => spin_loop(),
// this future returns pending 5 times before returning ready
let mut counter = 0;
let my_future = poll_fn(|ctx| {
    if counter < 5 {
        counter += 1;
    } else {
assert_eq!(block_on(my_future), 7);


The whole point. Returns a waker that does nothing.