use std::future::Future;
use std::pin::pin;
use std::task::{Context, Poll, Waker};
pub fn block_on<F: Future>(future: F) -> F::Output {
let mut future = pin!(future);
let waker = Waker::noop();
let mut cx = Context::from_waker(waker);
loop {
match future.as_mut().poll(&mut cx) {
Poll::Ready(out) => return out,
Poll::Pending => std::hint::spin_loop(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn drives_a_ready_future() {
assert_eq!(block_on(async { 7 }), 7);
}
#[test]
fn drives_chained_storage_futures() {
use crate::fs::{StdFs, Storage};
let exists = block_on(async {
StdFs
.try_exists(std::path::Path::new("/definitely/not/here"))
.await
});
assert!(!exists.unwrap());
}
}