Skip to content

Implement Future for Option<F> #109691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,12 @@ use crate::marker::Destruct;
use crate::panicking::{panic, panic_str};
use crate::pin::Pin;
use crate::{
cmp, convert, hint, mem,
cmp, convert,
future::Future,
hint, mem,
ops::{self, ControlFlow, Deref, DerefMut},
slice,
task::Poll,
};

/// The `Option` type. See [the module level documentation](self) for more.
Expand Down Expand Up @@ -2293,6 +2296,19 @@ impl SpecOptionPartialEq for cmp::Ordering {
}
}

#[stable(feature = "option_future", since = "CURRENT_RUSTC_VERSION")]
Copy link
Member

Choose a reason for hiding this comment

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

Could this include a doctest?

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

#[inline]
fn poll(self: Pin<&mut Self>, cx: &mut crate::task::Context<'_>) -> Poll<Self::Output> {
match self.as_pin_mut() {
Some(f) => f.poll(cx).map(Some),
None => Poll::Ready(None),
}
}
}

/////////////////////////////////////////////////////////////////////////////
// The Option Iterators
/////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/async-await/drop-track-bad-field-in-fru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ fn main() {}

async fn foo() {
None { value: (), ..Default::default() }.await;
//~^ ERROR `Option<_>` is not a future
//~| ERROR variant `Option<_>::None` has no field named `value`
//~^ ERROR variant `Option<_>::None` has no field named `value`
}
18 changes: 2 additions & 16 deletions tests/ui/async-await/drop-track-bad-field-in-fru.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ error[E0559]: variant `Option<_>::None` has no field named `value`
LL | None { value: (), ..Default::default() }.await;
| ^^^^^ `Option<_>::None` does not have this field

error[E0277]: `Option<_>` is not a future
--> $DIR/drop-track-bad-field-in-fru.rs:7:45
|
LL | None { value: (), ..Default::default() }.await;
| ^^^^^^
| |
| `Option<_>` is not a future
| help: remove the `.await`
|
= help: the trait `Future` is not implemented for `Option<_>`
= note: Option<_> must be a future or must implement `IntoFuture` to be awaited
= note: required for `Option<_>` to implement `IntoFuture`

error: aborting due to 2 previous errors
error: aborting due to previous error

Some errors have detailed explanations: E0277, E0559.
For more information about an error, try `rustc --explain E0277`.
For more information about this error, try `rustc --explain E0559`.