]> git.proxmox.com Git - pxar.git/blob - src/poll_fn.rs
update d/control
[pxar.git] / src / poll_fn.rs
1 //! `poll_fn` reimplementation as it is otherwise the only thing we need from the futures crate.
2 //!
3 //! Our `futures` crate dependency is optional.
4
5 use std::future::Future;
6 use std::pin::Pin;
7 use std::task::{Context, Poll};
8
9 pub struct PollFn<F> {
10 func: Option<F>,
11 }
12
13 pub fn poll_fn<F, R>(func: F) -> PollFn<F>
14 where
15 F: FnMut(&mut Context) -> Poll<R>,
16 {
17 PollFn { func: Some(func) }
18 }
19
20 impl<F, R> Future for PollFn<F>
21 where
22 F: FnMut(&mut Context) -> Poll<R>,
23 {
24 type Output = R;
25
26 fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
27 let this = unsafe { self.get_unchecked_mut() };
28 match &mut this.func {
29 None => panic!("poll() after Ready"),
30 Some(func) => {
31 let res = func(cx);
32 if res.is_ready() {
33 this.func = None;
34 }
35 res
36 }
37 }
38 }
39 }