futures_util/future/
poll_fn.rs1use super::assert_future;
4use core::fmt;
5use core::pin::Pin;
6use futures_core::future::Future;
7use futures_core::task::{Context, Poll};
8
9#[must_use = "futures do nothing unless you `.await` or poll them"]
11pub struct PollFn<F> {
12    f: F,
13}
14
15impl<F> Unpin for PollFn<F> {}
16
17pub fn poll_fn<T, F>(f: F) -> PollFn<F>
37where
38    F: FnMut(&mut Context<'_>) -> Poll<T>,
39{
40    assert_future::<T, _>(PollFn { f })
41}
42
43impl<F> fmt::Debug for PollFn<F> {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.debug_struct("PollFn").finish()
46    }
47}
48
49impl<T, F> Future for PollFn<F>
50where
51    F: FnMut(&mut Context<'_>) -> Poll<T>,
52{
53    type Output = T;
54
55    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
56        (&mut self.f)(cx)
57    }
58}