process: Fix clippy warnings

This commit is contained in:
Ivan Petkov 2019-05-29 20:30:29 -07:00
parent caf43221b5
commit f16725ea9f
No known key found for this signature in database
GPG Key ID: 0B431E9837056942
7 changed files with 60 additions and 46 deletions

View File

@ -10,10 +10,10 @@ fn main() {
loop { loop {
line.clear(); line.clear();
stdin.read_line(&mut line).unwrap(); stdin.read_line(&mut line).unwrap();
if line.len() == 0 { if line.is_empty() {
break; break;
} }
stdout.write(line.as_bytes()).unwrap(); stdout.write_all(line.as_bytes()).unwrap();
} }
stdout.flush().unwrap(); stdout.flush().unwrap();
} }

View File

@ -319,15 +319,21 @@ pub trait CommandExt {
fn output_async_with_handle(&mut self, handle: &Handle) -> OutputAsync; fn output_async_with_handle(&mut self, handle: &Handle) -> OutputAsync;
} }
struct SpawnedChild {
child: imp::Child,
stdin: Option<imp::ChildStdin>,
stdout: Option<imp::ChildStdout>,
stderr: Option<imp::ChildStderr>,
}
impl CommandExt for Command { impl CommandExt for Command {
fn spawn_async_with_handle(&mut self, handle: &Handle) -> io::Result<Child> { fn spawn_async_with_handle(&mut self, handle: &Handle) -> io::Result<Child> {
imp::Child::new(self.spawn()?, handle) imp::spawn_child(self, handle)
.map(|(child, stdin, stdout, stderr)| Child { .map(|spawned_child| Child {
child, child: spawned_child.child,
stdin: stdin.map(|inner| ChildStdin { inner }), stdin: spawned_child.stdin.map(|inner| ChildStdin { inner }),
stdout: stdout.map(|inner| ChildStdout { inner }), stdout: spawned_child.stdout.map(|inner| ChildStdout { inner }),
stderr: stderr.map(|inner| ChildStderr { inner }), stderr: spawned_child.stderr.map(|inner| ChildStderr { inner }),
kill_on_drop: true, kill_on_drop: true,
}) })
} }
@ -353,7 +359,7 @@ impl CommandExt for Command {
let inner = self.spawn_async_with_handle(handle) let inner = self.spawn_async_with_handle(handle)
.into_future() .into_future()
.and_then(|c| c.wait_with_output()); .and_then(Child::wait_with_output);
OutputAsync { OutputAsync {
inner: Box::new(inner), inner: Box::new(inner),
@ -445,9 +451,9 @@ impl Child {
WaitWithOutput { WaitWithOutput {
inner: Box::new(self.join3(stdout, stderr).map(|(status, stdout, stderr)| { inner: Box::new(self.join3(stdout, stderr).map(|(status, stdout, stderr)| {
Output { Output {
status: status, status,
stdout: stdout, stdout,
stderr: stderr, stderr,
} }
})) }))
} }

View File

@ -38,6 +38,7 @@ use std::fmt;
use std::io; use std::io;
use std::os::unix::io::{AsRawFd, RawFd}; use std::os::unix::io::{AsRawFd, RawFd};
use std::process::{self, ExitStatus}; use std::process::{self, ExitStatus};
use super::SpawnedChild;
use tokio_io::IoFuture; use tokio_io::IoFuture;
use tokio_reactor::{Handle, PollEvented}; use tokio_reactor::{Handle, PollEvented};
@ -66,21 +67,24 @@ impl fmt::Debug for Child {
} }
} }
pub(crate) fn spawn_child(cmd: &mut process::Command, handle: &Handle) -> io::Result<SpawnedChild> {
let mut child = cmd.spawn()?;
let stdin = stdio(child.stdin.take(), handle)?;
let stdout = stdio(child.stdout.take(), handle)?;
let stderr = stdio(child.stderr.take(), handle)?;
let signal = Signal::with_handle(libc::SIGCHLD, handle).flatten_stream();
Ok(SpawnedChild {
child: Child {
inner: Reaper::new(child, signal),
},
stdin,
stdout,
stderr,
})
}
impl Child { impl Child {
pub fn new(mut inner: process::Child, handle: &Handle)
-> io::Result<(Child, Option<ChildStdin>, Option<ChildStdout>, Option<ChildStderr>)>
{
let stdin = stdio(inner.stdin.take(), handle)?;
let stdout = stdio(inner.stdout.take(), handle)?;
let stderr = stdio(inner.stderr.take(), handle)?;
let signal = Signal::with_handle(libc::SIGCHLD, handle).flatten_stream();
let child = Child {
inner: Reaper::new(inner, signal),
};
Ok((child, stdin, stdout, stderr))
}
pub fn id(&self) -> u32 { pub fn id(&self) -> u32 {
self.inner.id() self.inner.id()

View File

@ -124,7 +124,7 @@ mod test {
impl Wait for MockWait { impl Wait for MockWait {
fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> { fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
let ret = if self.num_wait_until_status == self.total_waits { let ret = if self.num_wait_until_status == self.total_waits {
Some(self.status.clone()) Some(self.status)
} else { } else {
None None
}; };
@ -171,7 +171,7 @@ mod test {
#[test] #[test]
fn reaper() { fn reaper() {
let exit = ExitStatus::from_raw(0); let exit = ExitStatus::from_raw(0);
let mock = MockWait::new(exit.clone(), 3); let mock = MockWait::new(exit, 3);
let mut grim = Reaper::new(mock, MockStream::new(vec!( let mut grim = Reaper::new(mock, MockStream::new(vec!(
None, None,
Some(()), Some(()),

View File

@ -23,6 +23,7 @@ use std::io;
use std::os::windows::prelude::*; use std::os::windows::prelude::*;
use std::os::windows::process::ExitStatusExt; use std::os::windows::process::ExitStatusExt;
use std::process::{self, ExitStatus}; use std::process::{self, ExitStatus};
use std::ptr;
use futures::future::Fuse; use futures::future::Fuse;
use futures::sync::oneshot; use futures::sync::oneshot;
@ -36,6 +37,7 @@ use self::winapi::um::synchapi::*;
use self::winapi::um::threadpoollegacyapiset::*; use self::winapi::um::threadpoollegacyapiset::*;
use self::winapi::um::winbase::*; use self::winapi::um::winbase::*;
use self::winapi::um::winnt::*; use self::winapi::um::winnt::*;
use super::SpawnedChild;
use tokio_reactor::{Handle, PollEvented}; use tokio_reactor::{Handle, PollEvented};
#[must_use = "futures do nothing unless polled"] #[must_use = "futures do nothing unless polled"]
@ -63,22 +65,24 @@ struct Waiting {
unsafe impl Sync for Waiting {} unsafe impl Sync for Waiting {}
unsafe impl Send for Waiting {} unsafe impl Send for Waiting {}
impl Child { pub(crate) fn spawn_child(cmd: &mut process::Command, handle: &Handle) -> io::Result<SpawnedChild> {
pub fn new(mut inner: process::Child, handle: &Handle) let mut child = cmd.spawn()?;
-> io::Result<(Child, Option<ChildStdin>, Option<ChildStdout>, Option<ChildStderr>)> let stdin = stdio(child.stdin.take(), handle)?;
{ let stdout = stdio(child.stdout.take(), handle)?;
let stdin = stdio(inner.stdin.take(), handle)?; let stderr = stdio(child.stderr.take(), handle)?;
let stdout = stdio(inner.stdout.take(), handle)?;
let stderr = stdio(inner.stderr.take(), handle)?;
let child = Child { Ok(SpawnedChild {
child: inner, child: Child {
child,
waiting: None, waiting: None,
}; },
stdin,
Ok((child, stdin, stdout, stderr)) stdout,
} stderr,
})
}
impl Child {
pub fn id(&self) -> u32 { pub fn id(&self) -> u32 {
self.child.id() self.child.id()
} }
@ -103,7 +107,7 @@ impl Child {
} }
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let ptr = Box::into_raw(Box::new(Some(tx))); let ptr = Box::into_raw(Box::new(Some(tx)));
let mut wait_object = 0 as *mut _; let mut wait_object = ptr::null_mut();
let rc = unsafe { let rc = unsafe {
RegisterWaitForSingleObject(&mut wait_object, RegisterWaitForSingleObject(&mut wait_object,
self.child.as_raw_handle(), self.child.as_raw_handle(),
@ -120,7 +124,7 @@ impl Child {
} }
self.waiting = Some(Waiting { self.waiting = Some(Waiting {
rx: rx.fuse(), rx: rx.fuse(),
wait_object: wait_object, wait_object,
tx: ptr, tx: ptr,
}); });
} }
@ -142,7 +146,7 @@ impl Drop for Waiting {
unsafe extern "system" fn callback(ptr: PVOID, unsafe extern "system" fn callback(ptr: PVOID,
_timer_fired: BOOLEAN) { _timer_fired: BOOLEAN) {
let complete = &mut *(ptr as *mut Option<oneshot::Sender<()>>); let complete = &mut *(ptr as *mut Option<oneshot::Sender<()>>);
drop(complete.take().unwrap().send(())); let _ = complete.take().unwrap().send(());
} }
pub fn try_wait(child: &process::Child) -> io::Result<Option<ExitStatus>> { pub fn try_wait(child: &process::Child) -> io::Result<Option<ExitStatus>> {

View File

@ -16,7 +16,7 @@ fn run_test() {
let finished_clone = finished.clone(); let finished_clone = finished.clone();
thread::spawn(move || { thread::spawn(move || {
let _ = stream::iter_ok((0..2).into_iter()) let _ = stream::iter_ok(0..2)
.map(|i| Command::new("echo") .map(|i| Command::new("echo")
.arg(format!("I am spawned process #{}", i)) .arg(format!("I am spawned process #{}", i))
.stdin(Stdio::null()) .stdin(Stdio::null())

View File

@ -36,7 +36,7 @@ fn feed_cat(mut cat: Child, n: usize) -> Box<Future<Item = ExitStatus, Error = i
// Try to read `n + 1` lines, ensuring the last one is empty // Try to read `n + 1` lines, ensuring the last one is empty
// (i.e. EOF is reached after `n` lines. // (i.e. EOF is reached after `n` lines.
let reader = io::BufReader::new(stdout); let reader = io::BufReader::new(stdout);
let expected_numbers = stream::iter_ok(0..n + 1); let expected_numbers = stream::iter_ok(0..=n);
let read = expected_numbers.fold((reader, 0), move |(reader, i), _| { let read = expected_numbers.fold((reader, 0), move |(reader, i), _| {
let done = i >= n; let done = i >= n;
debug!("starting read from child"); debug!("starting read from child");