Skip to content

now_or_never

Polls future exactly once and returns its output if it was already ready.

Useful to peek at a future that may have finished (a channel receive, a cached value) without waiting for it. The future is dropped if it was not ready, so use it on futures you can afford to abandon.

use helpers4::future::now_or_never;

Cargo feature future (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features future

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["future"] }
pub fn now_or_never<F: Future>(future: F) -> Option<F::Output>
ParameterTypeDescription
futureFThe future to poll once.

Option<F::Output>Some(output) when the first poll completed, None when it was still pending.

use helpers4::future::now_or_never;

assert_eq!(now_or_never(async { 1 + 1 }), Some(2));
assert_eq!(now_or_never(std::future::pending::<u32>()), None);

src/future/now_or_never.rs