]> git.proxmox.com Git - pxar.git/blame - src/poll_fn.rs
Entry, Metaedata: file_type() and file_mode() getters
[pxar.git] / src / poll_fn.rs
CommitLineData
6cd4f635
WB
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
5use std::future::Future;
6use std::pin::Pin;
7use std::task::{Context, Poll};
8
9pub struct PollFn<F> {
10 func: Option<F>,
11}
12
13pub fn poll_fn<F, R>(func: F) -> PollFn<F>
14where
15 F: FnMut(&mut Context) -> Poll<R>,
16{
17 PollFn { func: Some(func) }
18}
19
20impl<F, R> Future for PollFn<F>
21where
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}