1 unstable release

0.1.0 Jul 26, 2021

#1662 in Asynchronous

Download history 2305/week @ 2023-10-31 1044/week @ 2023-11-07 7861/week @ 2023-11-14 3577/week @ 2023-11-21 4573/week @ 2023-11-28 8347/week @ 2023-12-05 4944/week @ 2023-12-12 3292/week @ 2023-12-19 424/week @ 2023-12-26 5296/week @ 2024-01-02 2131/week @ 2024-01-09 4935/week @ 2024-01-16 4260/week @ 2024-01-23 3682/week @ 2024-01-30 6099/week @ 2024-02-06 3493/week @ 2024-02-13

18,438 downloads per month
Used in 2 crates

MIT/Apache

7KB

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.

Example

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
    futures_lite::pin!(f);

    // 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;
        ctx.waker().wake_by_ref();
        Poll::Pending
    } else {
        Poll::Ready(7)
    }
});

assert_eq!(block_on(my_future), 7);

No runtime deps