diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-11-17 01:17:47 +0100 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-11-16 19:17:47 -0500 |
commit | 8f9a942cb911ed017eb128e9fbeb6f9a48e69601 (patch) | |
tree | 4f56623262f84becac18546d9da3d5d9ce9c8735 /cli/tokio_util.rs | |
parent | cb00fd6e988184420f842b1e77ca4cf627d32773 (diff) |
Use futures 0.3 API (#3358)
Diffstat (limited to 'cli/tokio_util.rs')
-rw-r--r-- | cli/tokio_util.rs | 53 |
1 files changed, 13 insertions, 40 deletions
diff --git a/cli/tokio_util.rs b/cli/tokio_util.rs index c86748b55..050080b70 100644 --- a/cli/tokio_util.rs +++ b/cli/tokio_util.rs @@ -1,9 +1,9 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use deno::ErrBox; use futures; -use futures::Future; -use futures::Poll; -use std::ops::FnOnce; +use futures::future::FutureExt; +use futures::future::TryFutureExt; +use std::future::Future; use tokio; use tokio::runtime; @@ -16,18 +16,18 @@ pub fn create_threadpool_runtime( pub fn run<F>(future: F) where - F: Future<Item = (), Error = ()> + Send + 'static, + F: Future<Output = Result<(), ()>> + Send + 'static, { // tokio::runtime::current_thread::run(future) let rt = create_threadpool_runtime().expect("Unable to create Tokio runtime"); - rt.block_on_all(future).unwrap(); + rt.block_on_all(future.boxed().compat()).unwrap(); } pub fn run_on_current_thread<F>(future: F) where - F: Future<Item = (), Error = ()> + Send + 'static, + F: Future<Output = Result<(), ()>> + Send + 'static, { - tokio::runtime::current_thread::run(future); + tokio::runtime::current_thread::run(future.boxed().compat()); } /// THIS IS A HACK AND SHOULD BE AVOIDED. @@ -40,7 +40,7 @@ where /// main runtime. pub fn block_on<F, R>(future: F) -> Result<R, ErrBox> where - F: Send + 'static + Future<Item = R, Error = ErrBox>, + F: Send + 'static + Future<Output = Result<R, ErrBox>> + Unpin, R: Send + 'static, { use std::sync::mpsc::channel; @@ -48,7 +48,7 @@ where let (sender, receiver) = channel(); // Create a new runtime to evaluate the future asynchronously. thread::spawn(move || { - let r = tokio::runtime::current_thread::block_on_all(future); + let r = tokio::runtime::current_thread::block_on_all(future.compat()); sender .send(r) .expect("Unable to send blocking future result") @@ -72,36 +72,9 @@ where tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f()); } -/// `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> +pub fn panic_on_error<I, E, F>(f: F) -> impl Future<Output = Result<I, ()>> 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() - } -} - -pub fn panic_on_error<I, E, F>(f: F) -> impl Future<Item = I, Error = ()> -where - F: Future<Item = I, Error = E>, + F: Future<Output = Result<I, E>>, E: std::fmt::Debug, { f.map_err(|err| panic!("Future got unexpected error: {:?}", err)) @@ -112,9 +85,9 @@ pub fn run_in_task<F>(f: F) where F: FnOnce() + Send + 'static, { - let fut = futures::future::lazy(move || { + let fut = futures::future::lazy(move |_cx| { f(); - futures::future::ok(()) + Ok(()) }); run(fut) |