summaryrefslogtreecommitdiff
path: root/src/tokio_util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tokio_util.rs')
-rw-r--r--src/tokio_util.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/tokio_util.rs b/src/tokio_util.rs
new file mode 100644
index 000000000..de81620ef
--- /dev/null
+++ b/src/tokio_util.rs
@@ -0,0 +1,30 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+
+use futures;
+use futures::Future;
+use tokio;
+use tokio_executor;
+
+pub fn block_on<F, R, E>(future: F) -> Result<R, E>
+where
+ F: Send + 'static + Future<Item = R, Error = E>,
+ R: Send + 'static,
+ E: Send + 'static,
+{
+ let (tx, rx) = futures::sync::oneshot::channel();
+ tokio::spawn(future.then(move |r| tx.send(r).map_err(|_| unreachable!())));
+ rx.wait().unwrap()
+}
+
+// Set the default executor so we can use tokio::spawn(). It's difficult to
+// pass around mut references to the runtime, so using with_default is
+// preferable. Ideally Tokio would provide this function.
+pub fn init<F>(f: F)
+where
+ F: FnOnce(),
+{
+ let rt = tokio::runtime::Runtime::new().unwrap();
+ let mut executor = rt.executor();
+ let mut enter = tokio_executor::enter().expect("Multiple executors at once");
+ tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f());
+}