Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_tasks/src/futures.rs
6604 views
1
//! Utilities for working with [`Future`]s.
2
use 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.
12
pub 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.
22
pub fn check_ready<F: Future + Unpin>(future: &mut F) -> Option<F::Output> {
23
now_or_never(future)
24
}
25
26