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

Rename core::future::poll_fn to core::future::from_fn #76789

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
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,48 @@ use crate::task::{Context, Poll};
/// # Examples
///
/// ```
/// #![feature(future_poll_fn)]
/// #![feature(future_from_fn)]
/// # async fn run() {
/// use core::future::poll_fn;
/// use core::future;
/// use core::task::{Context, Poll};
///
/// fn read_line(_cx: &mut Context<'_>) -> Poll<String> {
/// let fut = future::from_fn(|_cx: &mut Context<'_>| -> Poll<String> {
/// Poll::Ready("Hello, World!".into())
/// }
/// });
///
/// let read_future = poll_fn(read_line);
/// assert_eq!(read_future.await, "Hello, World!".to_owned());
/// assert_eq!(fut.await, "Hello, World!".to_owned());
/// # };
/// ```
#[unstable(feature = "future_poll_fn", issue = "72302")]
pub fn poll_fn<T, F>(f: F) -> PollFn<F>
#[unstable(feature = "future_from_fn", issue = "72302")]
pub fn from_fn<T, F>(f: F) -> FromFn<F>
where
F: FnMut(&mut Context<'_>) -> Poll<T>,
{
PollFn { f }
FromFn { f }
}

/// A Future that wraps a function returning `Poll`.
///
/// This `struct` is created by [`poll_fn()`]. See its
/// This `struct` is created by [`from_fn()`]. See its
/// documentation for more.
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[unstable(feature = "future_poll_fn", issue = "72302")]
pub struct PollFn<F> {
#[unstable(feature = "future_from_fn", issue = "72302")]
pub struct FromFn<F> {
f: F,
}

#[unstable(feature = "future_poll_fn", issue = "72302")]
impl<F> Unpin for PollFn<F> {}
#[unstable(feature = "future_from_fn", issue = "72302")]
impl<F> Unpin for FromFn<F> {}

#[unstable(feature = "future_poll_fn", issue = "72302")]
impl<F> fmt::Debug for PollFn<F> {
#[unstable(feature = "future_from_fn", issue = "72302")]
impl<F> fmt::Debug for FromFn<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PollFn").finish()
f.debug_struct("FromFn").finish()
}
}

#[unstable(feature = "future_poll_fn", issue = "72302")]
impl<T, F> Future for PollFn<F>
#[unstable(feature = "future_from_fn", issue = "72302")]
impl<T, F> Future for FromFn<F>
where
F: FnMut(&mut Context<'_>) -> Poll<T>,
{
Expand Down
6 changes: 3 additions & 3 deletions library/core/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::{
task::{Context, Poll},
};

mod from_fn;
mod future;
mod into_future;
mod pending;
mod poll_fn;
mod ready;

#[stable(feature = "futures_api", since = "1.36.0")]
Expand All @@ -26,8 +26,8 @@ pub use pending::{pending, Pending};
#[stable(feature = "future_readiness_fns", since = "1.48.0")]
pub use ready::{ready, Ready};

#[unstable(feature = "future_poll_fn", issue = "72302")]
pub use poll_fn::{poll_fn, PollFn};
#[unstable(feature = "future_from_fn", issue = "72302")]
pub use from_fn::{from_fn, FromFn};

/// This type is needed because:
///
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub use core::future::{from_generator, get_context, ResumeTy};
#[stable(feature = "future_readiness_fns", since = "1.48.0")]
pub use core::future::{pending, ready, Pending, Ready};

#[doc(inline)]
#[unstable(feature = "future_from_fn", issue = "72302")]
pub use core::future::{from_fn, FromFn};

#[doc(inline)]
#[unstable(feature = "into_future", issue = "67644")]
pub use core::future::IntoFuture;
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
#![feature(external_doc)]
#![feature(fn_traits)]
#![feature(format_args_nl)]
#![feature(future_from_fn)]
#![feature(gen_future)]
#![feature(generator_trait)]
#![feature(global_asm)]
Expand Down