From 0cc460aae1fd4d836e1f4b095c0f05bde3120cdc Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 29 Aug 2019 11:16:15 +0200 Subject: [PATCH 1/2] add future::poll_fn Signed-off-by: Yoshua Wuyts --- src/future/mod.rs | 2 ++ src/future/poll_fn.rs | 57 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/future/poll_fn.rs diff --git a/src/future/mod.rs b/src/future/mod.rs index 29e2b047f..09474faa7 100644 --- a/src/future/mod.rs +++ b/src/future/mod.rs @@ -4,7 +4,9 @@ pub use std::future::Future; pub use pending::pending; +pub use poll_fn::poll_fn; pub use ready::ready; mod pending; +mod poll_fn; mod ready; diff --git a/src/future/poll_fn.rs b/src/future/poll_fn.rs new file mode 100644 index 000000000..2aecf45be --- /dev/null +++ b/src/future/poll_fn.rs @@ -0,0 +1,57 @@ +//! Definition of the `PollFn` adapter combinator + +use core::fmt; +use core::pin::Pin; +use std::future::Future; +use std::task::{Context, Poll}; + +/// Future for the [`poll_fn`] function. +#[must_use = "futures do nothing unless you `.await` or poll them"] +struct PollFn { + f: F, +} + +impl Unpin for PollFn {} + +/// Creates a new future wrapping around a function returning `Poll`. +/// +/// Polling the returned future delegates to the wrapped function. +/// +/// # Examples +/// +/// ``` +/// # fn main() { async_std::task::block_on(async { +/// # +/// use async_std::future::poll_fn; +/// use async_std::task::{Context, Poll}; +/// +/// fn read_line(_cx: &mut Context<'_>) -> Poll { +/// Poll::Ready("Hello, World!".into()) +/// } +/// +/// let read_future = poll_fn(read_line); +/// assert_eq!(read_future.await, "Hello, World!".to_owned()); +/// # +/// # }) } +/// ``` +pub async fn poll_fn(f: impl FnMut(&mut Context<'_>) -> Poll) -> T { + let fut = PollFn { f }; + fut.await +} + +impl fmt::Debug for PollFn { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("PollFn").finish() + } +} + +impl Future for PollFn +where + F: FnMut(&mut Context<'_>) -> Poll, +{ + type Output = T; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + (&mut self.f)(cx) + } +} From 4b0bebf852f0bfa744f3deb5f62a7113a9e40ead Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Fri, 30 Aug 2019 20:33:59 +0200 Subject: [PATCH 2/2] stjepan feedback Signed-off-by: Yoshua Wuyts --- src/future/poll_fn.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/future/poll_fn.rs b/src/future/poll_fn.rs index 2aecf45be..f2e110dca 100644 --- a/src/future/poll_fn.rs +++ b/src/future/poll_fn.rs @@ -1,6 +1,5 @@ //! Definition of the `PollFn` adapter combinator -use core::fmt; use core::pin::Pin; use std::future::Future; use std::task::{Context, Poll}; @@ -30,7 +29,7 @@ impl Unpin for PollFn {} /// } /// /// let read_future = poll_fn(read_line); -/// assert_eq!(read_future.await, "Hello, World!".to_owned()); +/// assert_eq!(read_future.await, "Hello, World!"); /// # /// # }) } /// ``` @@ -39,12 +38,6 @@ pub async fn poll_fn(f: impl FnMut(&mut Context<'_>) -> Poll) -> T { fut.await } -impl fmt::Debug for PollFn { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("PollFn").finish() - } -} - impl Future for PollFn where F: FnMut(&mut Context<'_>) -> Poll,