• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![allow(dead_code)]
2 
3 //! Definition of the `PollFn` adapter combinator.
4 
5 use std::fmt;
6 use std::future::Future;
7 use std::pin::Pin;
8 use std::task::{Context, Poll};
9 
10 /// Future for the [`poll_fn`] function.
11 pub struct PollFn<F> {
12     f: F,
13 }
14 
15 impl<F> Unpin for PollFn<F> {}
16 
17 /// Creates a new future wrapping around a function returning [`Poll`].
poll_fn<T, F>(f: F) -> PollFn<F> where F: FnMut(&mut Context<'_>) -> Poll<T>,18 pub fn poll_fn<T, F>(f: F) -> PollFn<F>
19 where
20     F: FnMut(&mut Context<'_>) -> Poll<T>,
21 {
22     PollFn { f }
23 }
24 
25 impl<F> fmt::Debug for PollFn<F> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result26     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27         f.debug_struct("PollFn").finish()
28     }
29 }
30 
31 impl<T, F> Future for PollFn<F>
32 where
33     F: FnMut(&mut Context<'_>) -> Poll<T>,
34 {
35     type Output = T;
36 
poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T>37     fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
38         (&mut self.f)(cx)
39     }
40 }
41