I need an optional Future to get rid of tokio select.
I'm a big fan of rust async, but I really hate using tokio::select. I would rather write wait loops using poll_fn.
In many cases, however, this requires setting variables that hold pinned futures from async functions. Rust makes this annoying, because you can't extract the returned Future type from such a function to use it in new declarations. Also because Option<Pin<&mut...>> is difficult to deal with.
So I need a struct that supports something like:
// This doesn't call the lambda. It just uses the return type for inference
let mut f = pin!(OptFuture::none_like(|| some_async_fn()));
// But I can set or clear the OptFuture later
f.set(some_async_fn());
// And it implements Future<Output = Option<F::Output>>
poll_fn(|cx| {
match f.poll(cx) {
Poll::Pending => {... future not done ...}
Poll::Ready(None) => {... future not set ...}
Poll::Ready(Some(_)) => {... future finished...}
}
});
Before I go rolling my own, is there anything like this on crates that has some adoption already? I don't want to believe that everyone else just puts up with the `select` macro.
Posted Jun 17, 2026, 1:06 PM UTC
1129