Function block_on

Source
pub fn block_on<F, R>(future: F) -> R
where F: Future<Output = R> + 'static, R: 'static,
Expand description

Runs the given future to completion on the current worker.

This blocks until the given future is complete, then returns the result of the future.

ยงExamples

use my_ecs::ds::block_on;
use std::{
    future::Future,
    task::{Poll, Context},
    pin::Pin,
};

struct MyFuture {
    count: u32,
    result: u32,
}

impl Future for MyFuture {
    type Output = u32;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.get_mut();
        if this.count == 0 {
            Poll::Ready(this.result)
        } else {
            this.count -= 1;
            this.result += 1;
            cx.waker().wake_by_ref();
            Poll::Pending
        }
    }
}

let res = block_on(MyFuture { count: 2, result: 0 });
assert_eq!(res, 2);