2 use crate::future::Future;
4 use crate::task::{Context, Poll};
6 /// Creates a future that wraps a function returning [`Poll`].
8 /// Polling the future delegates to the wrapped function.
13 /// # async fn run() {
14 /// use core::future::poll_fn;
15 /// use std::task::{Context, Poll};
17 /// fn read_line(_cx: &mut Context<'_>) -> Poll<String> {
18 /// Poll::Ready("Hello, World!".into())
21 /// let read_future = poll_fn(read_line);
22 /// assert_eq!(read_future.await, "Hello, World!".to_owned());
25 #[stable(feature = "future_poll_fn", since = "1.64.0")]
26 pub fn poll_fn<T, F>(f: F) -> PollFn<F>
28 F: FnMut(&mut Context<'_>) -> Poll<T>,
33 /// A Future that wraps a function returning [`Poll`].
35 /// This `struct` is created by [`poll_fn()`]. See its
36 /// documentation for more.
37 #[must_use = "futures do nothing unless you `.await` or poll them"]
38 #[stable(feature = "future_poll_fn", since = "1.64.0")]
39 pub struct PollFn<F> {
43 #[stable(feature = "future_poll_fn", since = "1.64.0")]
44 impl<F> Unpin for PollFn<F> {}
46 #[stable(feature = "future_poll_fn", since = "1.64.0")]
47 impl<F> fmt::Debug for PollFn<F> {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 f.debug_struct("PollFn").finish()
53 #[stable(feature = "future_poll_fn", since = "1.64.0")]
54 impl<T, F> Future for PollFn<F>
56 F: FnMut(&mut Context<'_>) -> Poll<T>,
60 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {