#waker #tasks #aid #async #wake #traits #own

yanked wakeful

Utilities to aid implementing wakers and working with tasks

0.1.1 Jan 2, 2020
0.1.0 Jan 2, 2020

#15 in #aid

MIT license

13KB
153 lines

Wakeful

Utilities to aid implementing Wakers and working with tasks.

Crates.io Documentation License Maintenance Build

Documentation

Please check out the documentation for details on what Wakeful can do and how to use it.

License

This library is licensed under the MIT license. See the LICENSE file for details.


lib.rs:

Utilities to aid implementing Wakers and working with tasks.

The highlight of this crate is Wake, which allows you to construct wakers from your own types by implementing this trait.

Examples

Implementing your own block_on function using this crate:

use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
    thread,
};
use wakeful::Wake;

fn block_on<F: Future>(mut future: F) -> F::Output {
    let waker = thread::current().into_waker();
    let mut context = Context::from_waker(&waker);
    let mut future = unsafe { Pin::new_unchecked(&mut future) };

    loop {
        match future.as_mut().poll(&mut context) {
            Poll::Ready(output) => return output,
            Poll::Pending => thread::park(),
        }
    }
}

No runtime deps