zng::task

Function future_fn

source
pub async fn future_fn<T, F>(fn_: F) -> T
where F: FnMut(&mut Context<'_>) -> Poll<T>,
Expand description

Implements a Future from a closure.

ยงExamples

A future that is ready with a closure returns Some(R).

use zng_task as task;
use std::task::Poll;

async fn ready_some<R>(mut closure: impl FnMut() -> Option<R>) -> R {
    task::future_fn(|cx| {
        match closure() {
            Some(r) => Poll::Ready(r),
            None => Poll::Pending
        }
    }).await
}