Skip to content

ACP: Implement Future for Option<F> #197

Closed as not planned
Closed as not planned
@nvzqz

Description

@nvzqz

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions