Closed as not planned
Description
Proposal
Problem statement
When working with an Option<F: Future>
in an async
context, one may want to defer the handling of None
. This is currently cumbersome/verbose to do.
Motivation, use-cases
let x = match f {
Some(f) => Some(f.await),
None => None,
};
It is not possible to shorten this to let x = f.map(|f| f.await)
because the async
context is not available in a closure.
Solution sketches
impl<F: Future> Future for Option<F> {
type Output = Option<F::Output>;
fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Self::Output> {
match self.as_pin_mut() {
Some(f) => f.poll(cx).map(Some),
None => Poll::Ready(None),
}
}
}
This is implemented in rust-lang/rust#109691.
Links and related work
Prior art: