#![no_std]
#[cfg(not(feature = "no_joiner"))]
mod joiner;
#[cfg(not(feature = "no_joiner"))]
pub use joiner::{join, prep, BoxFuture};
use core::pin::Pin;
use core::{
future::Future,
ptr::null,
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};
fn empty(_: *const ()) {}
fn empty_clone(_: *const ()) -> RawWaker {
empty_raw_waker()
}
#[inline]
fn empty_raw_waker() -> RawWaker {
RawWaker::new(
null(),
&RawWakerVTable::new(empty_clone, empty, empty, empty),
)
}
#[inline]
fn empty_waker() -> Waker {
unsafe { Waker::from_raw(empty_raw_waker()) }
}
pub fn sync_with<T>(mut future: impl Future<Output = T>, poll_delay: u64) -> T {
let mut future = unsafe { Pin::new_unchecked(&mut future) };
loop {
let waker = empty_waker();
let context = &mut Context::from_waker(&waker);
if let Poll::Ready(content) = future.as_mut().poll(context) {
return content;
}
#[cfg(not(feature = "no_std"))]
{
extern crate std;
std::thread::sleep(std::time::Duration::from_millis(poll_delay));
}
}
}
pub fn sync<T>(future: impl Future<Output = T>) -> T {
sync_with(future, 1)
}