#Checking if the user provided future returns immediately before spawning it
6 messages · Page 1 of 1 (latest)
you could use FutureExt::now_or_never() if your futures are Unpin or boxed
it sounds like what you want is to poll it once and then spawn it if not immediately ready
so you can pin it and poll it once, and then spawn it if it returns pending
you probably want futures::poll! rather than now or never for that case
https://docs.rs/futures/latest/futures/macro.poll.html
A macro which returns the result of polling a future once within the current async context.
this is indeed a pattern i've used, for handling futures that call a backend service except may be cached on host and not need a sleep
it also is nice to make sure the remote i/o kicks off immediately (and then sleeps) instead of adding scheduling delay before the task is first polled
if your futures aren't already Unpin, like alphakeks said, you'll probably need to Box::pin them instead of just naked pinning - which does involve an allocation, so it's not zero cost (but also spawning has its own overhead)