futures-rs icon indicating copy to clipboard operation
futures-rs copied to clipboard

Add a combinator for converting an `Option<Future>` into a `Future<Option>`

Open notgull opened this issue 3 years ago • 0 comments

Consider the following code, where bar is an async function:

let foo = Some(24);

foo.map(|foo| bar(foo))

The end result of this is an Option<impl Future>. In order to await or poll() this future, it needs to be matched on or unwrapped, which may be messy or undesired.

I propose a combinator to the tune of into_future(), with a public API like this:

trait OptionExt<F> {
    fn into_future(self) -> IntoFuture<F>;
}

struct IntoFuture<F>(Option<F>);

impl<F: Future> Future for IntoFuture<F> {
    type Output = Option<F::Output>;
}

This may also be useful for Result.

notgull avatar Aug 16 '22 15:08 notgull