Skip to content
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

Add Future::poll_once #92116

Closed
wants to merge 1 commit 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
27 changes: 27 additions & 0 deletions library/core/src/future/future.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![stable(feature = "futures_api", since = "1.36.0")]

use crate::future::PollOnce;
use crate::marker::Unpin;
use crate::ops;
use crate::pin::Pin;
Expand Down Expand Up @@ -101,6 +102,32 @@ pub trait Future {
#[lang = "poll"]
#[stable(feature = "futures_api", since = "1.36.0")]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;

/// Poll a future once.
///
/// # Examples
///
/// ```rust
/// #![feature(future_poll_once)]
///
/// use std::future::{self, Future};
/// use std::task::Poll;
///
/// # let _ = async {
/// let f = future::ready(1);
/// assert_eq!(f.poll_once().await, Poll::Ready(1));
///
/// let mut f = future::pending::<()>();
/// assert_eq!(f.poll_once().await, Poll::Pending);
/// # };
/// ```
#[unstable(feature = "future_poll_once", issue = "92115")]
fn poll_once(self) -> PollOnce<Self>
where
Self: Sized,
{
PollOnce { future: self }
Copy link
Contributor

@danielhenrymantilla danielhenrymantilla Jan 31, 2022

Choose a reason for hiding this comment

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

Suggested change
PollOnce { future: self }
PollOnce { future: Some(self) }

}
}

#[stable(feature = "futures_api", since = "1.36.0")]
Expand Down
4 changes: 4 additions & 0 deletions library/core/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod future;
mod into_future;
mod pending;
mod poll_fn;
mod poll_once;
mod ready;

#[stable(feature = "futures_api", since = "1.36.0")]
Expand All @@ -29,6 +30,9 @@ pub use ready::{ready, Ready};
#[unstable(feature = "future_poll_fn", issue = "72302")]
pub use poll_fn::{poll_fn, PollFn};

#[unstable(feature = "future_poll_once", issue = "92115")]
pub use poll_once::PollOnce;

/// This type is needed because:
///
/// a) Generators cannot implement `for<'a, 'b> Generator<&'a mut Context<'b>>`, so we need to pass
Expand Down
32 changes: 32 additions & 0 deletions library/core/src/future/poll_once.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::fmt;
use crate::future::Future;
use crate::pin::Pin;
use crate::task::{Context, Poll};

/// Resolves to the output of polling a future once.
///
/// This `struct` is created by [`poll_once()`]. See its
/// documentation for more.
#[unstable(feature = "future_poll_once", issue = "92115")]
pub struct PollOnce<F> {
pub(crate) future: F,
Copy link
Contributor

@danielhenrymantilla danielhenrymantilla Jan 31, 2022

Choose a reason for hiding this comment

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

Idea to support ?Unpin:

Suggested change
pub(crate) future: F,
pub(crate) future: Option<F>,

(and a impl<F> Unpin for PollOnce<F>)

}

#[unstable(feature = "future_poll_once", issue = "92115")]
impl<F> fmt::Debug for PollOnce<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("PollOnce").finish()
}
}

#[unstable(feature = "future_poll_once", issue = "92115")]
impl<F> Future for PollOnce<F>
where
F: Future + Unpin,
{
type Output = Poll<F::Output>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(Pin::new(&mut self.future).poll(cx))
}
Comment on lines +25 to +31
Copy link
Contributor

@danielhenrymantilla danielhenrymantilla Jan 31, 2022

Choose a reason for hiding this comment

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

Suggested change
F: Future + Unpin,
{
type Output = Poll<F::Output>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(Pin::new(&mut self.future).poll(cx))
}
F: Future,
{
type Output = Poll<F::Output>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let future = pin!(self.future.take().expect("Polled after completion"));
Poll::Ready(future.poll(cx))
}

If the dependency on a macro is deemed unsatisfactory to begin with, we could always hand-roll a Pin::new_unchecked(). This is also a nice instance to showcase the safe .pinned() (🚲 🏠) closure-based combinator:

self.future
    .take()
    .expect("Polled after completion")
    .pinned(|future| Poll::Ready(future.poll(cx)))

}