bevy_tasks/
futures.rs

1//! Utilities for working with [`Future`]s.
2use core::{
3    future::Future,
4    pin::pin,
5    task::{Context, Poll, Waker},
6};
7
8/// Consumes a future, polls it once, and immediately returns the output
9/// or returns `None` if it wasn't ready yet.
10///
11/// This will cancel the future if it's not ready.
12pub fn now_or_never<F: Future>(future: F) -> Option<F::Output> {
13    let mut cx = Context::from_waker(Waker::noop());
14    match pin!(future).poll(&mut cx) {
15        Poll::Ready(x) => Some(x),
16        _ => None,
17    }
18}
19
20/// Polls a future once, and returns the output if ready
21/// or returns `None` if it wasn't ready yet.
22pub fn check_ready<F: Future + Unpin>(future: &mut F) -> Option<F::Output> {
23    now_or_never(future)
24}