From 8270965459f0d696aca72714bf3e92c11e874503 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Sun, 13 May 2018 14:56:30 -0700 Subject: [PATCH] process: Remove dependency on `tokio-core` --- Cargo.toml | 2 +- src/lib.rs | 62 ++++++++++++++++++++++---------------------- tests/smoke.rs | 13 ++++++---- tests/stdio.rs | 46 ++++++++++++++++---------------- tests/support/mod.rs | 1 - 5 files changed, 63 insertions(+), 61 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 705290815..94953679c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,13 +18,13 @@ appveyor = { repository = "alexcrichton/tokio-process" } [dependencies] futures = "0.1.11" mio = "0.6.5" -tokio-core = "0.1.6" tokio-io = "0.1" tokio-reactor = "0.1" [dev-dependencies] env_logger = { version = "0.4", default-features = false } log = "0.4" +tokio = "0.1" [target.'cfg(windows)'.dependencies] mio-named-pipes = "0.1" diff --git a/src/lib.rs b/src/lib.rs index 0256ecd69..611cd88a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,31 +14,27 @@ //! //! ```no_run //! extern crate futures; -//! extern crate tokio_core; +//! extern crate tokio; //! extern crate tokio_process; //! //! use std::process::Command; //! //! use futures::Future; -//! use tokio_core::reactor::Core; //! use tokio_process::CommandExt; //! //! fn main() { -//! // Create our own local event loop -//! let mut core = Core::new().unwrap(); -//! //! // Use the standard library's `Command` type to build a process and //! // then execute it via the `CommandExt` trait. //! let child = Command::new("echo").arg("hello").arg("world") -//! .spawn_async_with_handle(core.handle().new_tokio_handle()); +//! .spawn_async(); //! -//! // Make sure our child succeeded in spawning -//! let child = child.expect("failed to spawn"); +//! // Make sure our child succeeded in spawning and process the result +//! let future = child.expect("failed to spawn") +//! .map(|status| println!("exit status: {}", status)) +//! .map_err(|e| panic!("failed to wait for exit: {}", e)); //! -//! match core.run(child) { -//! Ok(status) => println!("exit status: {}", status), -//! Err(e) => panic!("failed to wait for exit: {}", e), -//! } +//! // Send the future to the tokio runtime for execution +//! tokio::run(future) //! } //! ``` //! @@ -47,26 +43,27 @@ //! //! ```no_run //! extern crate futures; -//! extern crate tokio_core; +//! extern crate tokio; //! extern crate tokio_process; //! //! use std::process::Command; //! //! use futures::Future; -//! use tokio_core::reactor::Core; //! use tokio_process::CommandExt; //! //! fn main() { -//! let mut core = Core::new().unwrap(); -//! //! // Like above, but use `output_async` which returns a future instead of //! // immediately returning the `Child`. //! let output = Command::new("echo").arg("hello").arg("world") -//! .output_async_with_handle(core.handle().new_tokio_handle()); -//! let output = core.run(output).expect("failed to collect output"); +//! .output_async(); //! -//! assert!(output.status.success()); -//! assert_eq!(output.stdout, b"hello world\n"); +//! let future = output.map_err(|e| panic!("failed to collect output: {}", e)) +//! .map(|output| { +//! assert!(output.status.success()); +//! assert_eq!(output.stdout, b"hello world\n"); +//! }); +//! +//! tokio::run(future); //! } //! ``` //! @@ -74,18 +71,17 @@ //! //! ```no_run //! extern crate futures; -//! extern crate tokio_core; +//! extern crate tokio; //! extern crate tokio_process; //! extern crate tokio_io; //! //! use std::io; -//! use std::process::{Command, Stdio, ExitStatus}; +//! use std::process::{Command, Stdio}; //! -//! use futures::{BoxFuture, Future, Stream}; -//! use tokio_core::reactor::Core; +//! use futures::{Future, Stream}; //! use tokio_process::{CommandExt, Child}; //! -//! fn print_lines(mut cat: Child) -> BoxFuture { +//! fn print_lines(mut cat: Child) -> Box + Send + 'static> { //! let stdout = cat.stdout().take().unwrap(); //! let reader = io::BufReader::new(stdout); //! let lines = tokio_io::io::lines(reader); @@ -93,15 +89,20 @@ //! println!("Line: {}", l); //! Ok(()) //! }); -//! cycle.join(cat).map(|((), s)| s).boxed() +//! +//! let future = cycle.join(cat) +//! .map(|_| ()) +//! .map_err(|e| panic!("{}", e)); +//! +//! Box::new(future) //! } //! //! fn main() { -//! let mut core = Core::new().unwrap(); //! let mut cmd = Command::new("cat"); -//! let mut cat = cmd.stdout(Stdio::piped()); -//! let child = cat.spawn_async_with_handle(core.handle().new_tokio_handle()).unwrap(); -//! core.run(print_lines(child)).unwrap(); +//! cmd.stdout(Stdio::piped()); +//! +//! let future = print_lines(cmd.spawn_async().expect("failed to spawn command")); +//! tokio::run(future); //! } //! ``` //! @@ -120,7 +121,6 @@ #![doc(html_root_url = "https://docs.rs/tokio-process/0.1")] extern crate futures; -extern crate tokio_core; extern crate tokio_io; extern crate tokio_reactor; extern crate mio; diff --git a/tests/smoke.rs b/tests/smoke.rs index 69185bab9..8aff3877a 100644 --- a/tests/smoke.rs +++ b/tests/smoke.rs @@ -1,21 +1,24 @@ -extern crate tokio_core; +extern crate tokio; extern crate tokio_process; -use tokio_core::reactor::Core; +use tokio::executor::current_thread; use tokio_process::CommandExt; mod support; #[test] fn simple() { - let mut lp = Core::new().unwrap(); let mut cmd = support::cmd("exit"); cmd.arg("2"); - let mut child = cmd.spawn_async_with_handle(lp.handle().new_tokio_handle()).unwrap(); + + let mut child = cmd.spawn_async().unwrap(); + let id = child.id(); assert!(id > 0); - let status = lp.run(&mut child).unwrap(); + + let status = current_thread::block_on_all(&mut child).unwrap(); assert_eq!(status.code(), Some(2)); + assert_eq!(child.id(), id); drop(child.kill()); } diff --git a/tests/stdio.rs b/tests/stdio.rs index 6546cb44f..ea39bceb5 100644 --- a/tests/stdio.rs +++ b/tests/stdio.rs @@ -1,5 +1,5 @@ extern crate futures; -extern crate tokio_core; +extern crate tokio; extern crate tokio_io; extern crate tokio_process; #[macro_use] @@ -9,12 +9,14 @@ extern crate env_logger; use std::io; use std::process::{Stdio, ExitStatus, Command}; use std::time::Duration; +use std::time::Instant; use futures::future::Future; use futures::stream::{self, Stream}; +use tokio::executor::current_thread; use tokio_io::io::{read_until, write_all, read_to_end}; -use tokio_core::reactor::{Core, Timeout}; use tokio_process::{CommandExt, Child}; +use tokio::timer::Deadline; mod support; @@ -84,34 +86,35 @@ fn feed_cat(mut cat: Child, n: usize) -> Box assert!(status.success()), - Err(_) => panic!("failed to run futures"), - } + // NB: Deadline requires a timer registration which is provided by + // tokio's `current_thread::Runtime`, but isn't available by just using + // tokio's default CurrentThread executor which powers `current_thread::block_on_all`. + let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap(); + rt.block_on(Deadline::new(child, Instant::now() + Duration::from_secs(1))) + .expect("time out exceeded! did we get stuck waiting on the child?"); } diff --git a/tests/support/mod.rs b/tests/support/mod.rs index 878afb12a..0c2014628 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -1,6 +1,5 @@ extern crate env_logger; extern crate futures; -extern crate tokio_core; extern crate tokio_process; use std::env;