bevy_platform/future.rs
1//! Platform-aware future utilities.
2
3crate::cfg::switch! {
4 #[cfg(feature = "async-io")] => {
5 pub use async_io::block_on;
6 }
7 #[cfg(feature = "futures-lite")] => {
8 pub use futures_lite::future::block_on;
9 }
10 _ => {
11 /// Blocks on the supplied `future`.
12 /// This implementation will busy-wait until it is completed.
13 /// Consider enabling the `async-io` or `futures-lite` features.
14 pub fn block_on<T>(future: impl Future<Output = T>) -> T {
15 use core::task::{Poll, Context};
16
17 // Pin the future on the stack.
18 let mut future = core::pin::pin!(future);
19
20 // We don't care about the waker as we're just going to poll as fast as possible.
21 let cx = &mut Context::from_waker(core::task::Waker::noop());
22
23 // Keep polling until the future is ready.
24 loop {
25 match future.as_mut().poll(cx) {
26 Poll::Ready(output) => return output,
27 Poll::Pending => core::hint::spin_loop(),
28 }
29 }
30 }
31 }
32}