Function actix_utils::future::poll_fn

source ·
pub fn poll_fn<F, T>(f: F) -> PollFn<F> where
    F: FnMut(&mut Context<'_>) -> Poll<T>,
Expand description

Creates a future driven by the provided function that receives a task context.

Examples

let res = poll_fn(|_| Poll::Ready(42)).await;
assert_eq!(res, 42);

let mut i = 5;
let res = poll_fn(|cx| {
    i -= 1;

    if i > 0 {
        cx.waker().wake_by_ref();
        Poll::Pending
    } else {
        Poll::Ready(42)
    }
})
.await;
assert_eq!(res, 42);