-
Notifications
You must be signed in to change notification settings - Fork 24
ACP: Implement Future for Option<F> #197
Copy link
Copy link
Closed as not planned
Labels
T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard librariesA proposal to add or alter unstable APIs in the standard libraries
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:
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard librariesA proposal to add or alter unstable APIs in the standard libraries
Type
Fields
Give feedbackNo fields configured for issues without a type.