summaryrefslogtreecommitdiff
path: root/src/tokio_util.rs
diff options
context:
space:
mode:
authorF001 <changchun.fan@qq.com>2018-12-11 21:36:34 +0800
committerRyan Dahl <ry@tinyclouds.org>2018-12-11 08:36:34 -0500
commitc1de50b0ca9c6e1c8cd06347d19dd3db50db36d8 (patch)
tree8b1b21bee6630638016d4a7e1cd749c5cd887f16 /src/tokio_util.rs
parent9a960b9f5804f5e855163e7ec43327c28daef845 (diff)
Replace blocking! macro by generic function (#1305)
Diffstat (limited to 'src/tokio_util.rs')
-rw-r--r--src/tokio_util.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/tokio_util.rs b/src/tokio_util.rs
index 31a67574e..6d295453f 100644
--- a/src/tokio_util.rs
+++ b/src/tokio_util.rs
@@ -73,3 +73,30 @@ impl Future for Accept {
}
}
}
+
+/// `futures::future::poll_fn` only support `F: FnMut()->Poll<T, E>`
+/// However, we require that `F: FnOnce()->Poll<T, E>`.
+/// Therefore, we created our version of `poll_fn`.
+pub fn poll_fn<T, E, F>(f: F) -> PollFn<F>
+where
+ F: FnOnce() -> Poll<T, E>,
+{
+ PollFn { inner: Some(f) }
+}
+
+pub struct PollFn<F> {
+ inner: Option<F>,
+}
+
+impl<T, E, F> Future for PollFn<F>
+where
+ F: FnOnce() -> Poll<T, E>,
+{
+ type Item = T;
+ type Error = E;
+
+ fn poll(&mut self) -> Poll<T, E> {
+ let f = self.inner.take().expect("Inner fn has been taken.");
+ f()
+ }
+}