future_fn

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 std::task::Poll;
use zng_task as task;

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
}