Awaiting OptionFuture: strange error

This simple code (playground):

#[tokio::main]
async fn main() {
    let f: futures::future::OptionFuture<()> = None.into();
    let () = f.await;
}

produces:

error[E0277]: `()` is not a future
 --> src/main.rs:4:15
  |
4 |     let () = f.await;
  |               ^^^^^^ `()` is not a future
  |
  = help: the trait `futures::Future` is not implemented for `()`
  = note: () must be a future or must implement `IntoFuture` to be awaited
  = note: required because of the requirements on the impl of `futures::Future` for `OptionFuture<()>`
  = note: required because of the requirements on the impl of `std::future::IntoFuture` for `OptionFuture<()>`
help: remove the `.await`
  |
4 -     let () = f.await;
4 +     let () = f;

But f has type OptionFuture<()>, not (), and OptionFuture is Future!

OptionFuture<F> is only a Future when F is a Future. () is not a Future and thus OptionFuture<()> isn't either. Also .await on an OptionFuture<F> produces a value of type Option<<F as Future>::Output>, not a value of type F.

Got it, thanks. Why doesn't OptionFuture require it's type parameter to be Future?

Only putting the trait bounds on the impl blocks that require them is more convenient because it reduces the number of times you have to repeat the same where clause.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.