Skip to content

Add MaybeImmediate future#3015

Open
richard-w wants to merge 1 commit into
rust-lang:mainfrom
richard-w:feature/maybe-immediate
Open

Add MaybeImmediate future#3015
richard-w wants to merge 1 commit into
rust-lang:mainfrom
richard-w:feature/maybe-immediate

Conversation

@richard-w

Copy link
Copy Markdown

MaybeImmediate<Fut> wraps a future via FutureExt::maybe_immediate but it can also be constructed directly from a Fut::Output value which is then returned on the first poll without ever creating a Fut. It behaves almost like MaybeDone<Fut> but the first poll after completion will take the output and return it.

This is particularly useful in situations where a function validates inputs and calls another function that returns a future when the input is valid. In case the input is not valid MaybeImmediate can be used to bail out with an error early without having to return a trait object via Box::pin.

For example we'd be able to write this (pseudo-code):

fn validate_then_do_something(inputs: Inputs) -> impl Future<Result<(), ()>> {
  if is_valid(&inputs) {
    do_something(inputs).maybe_immediate()
  } else {
    future::maybe_immediate(Err(()))
  }
}

fn do_something(inputs: Inputs) -> impl Future<Result<(), ()>> {
  // ...
}

instead of

fn validate_then_do_something(inputs: Inputs) -> Pin<Box<dyn Future<Output = Result<(), ()>>> {
  if is_valid(&inputs) {
    Box::pin(do_something(inputs))
  } else {
    Box::pin(std::future::ready(Err(())))
  }
}

fn do_something(inputs: Inputs) -> impl Future<Result<(), ()>> {
  // ...
}

`MaybeImmediate<Fut>` wraps a future via `FutureExt::maybe_immediate`
but it can also be constructed directly from a `Fut::Output` value which
is then returned on the first poll without ever creating a `Fut`. It
behaves almost like `MaybeDone<Fut>` but the first poll after completion
will take the output and return it.
@rustbot rustbot added A-future Area: futures::future S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 12, 2026

@taiki-e taiki-e left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. To be honest, I don't really understand why this is necessary when we already have MaybeDone.

View changes since this review

@taiki-e taiki-e removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 12, 2026
@richard-w

Copy link
Copy Markdown
Author

@taiki-e MaybeImmediate, when wrapping a Future<Output = T>, is itself a Future<Output = T> while MaybeDone will always be a Future<Output = ()>.

MaybeDone allows taking the value of its inner Future using a method but it will not itself resolve to that value. This makes MaybeDone only useful in situations where you know that you're dealing with a MaybeDone. It is not really usable as a trait object in particular. Adding MaybeImmediate solves that problem and doesn't break existing users.

If a breaking change is acceptable here then MaybeDone should probably be changed instead.

@rustbot

rustbot commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #3022) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-future Area: futures::future

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants