diff options
| author | bors <bors@rust-lang.org> | 2019-11-30 12:42:44 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-11-30 12:42:44 +0000 |
| commit | d8bdb3fdcbd88eb16e1a6669236122c41ed2aed3 (patch) | |
| tree | 33974ee0e3d5976f284b056e03e6ef529d15e563 /src/libstd/process.rs | |
| parent | 8f1bbd69e13c9e04a4c2b75612bc0c31af972439 (diff) | |
| parent | b14d9c21203ea79035bf4a8a8a68ad34658a265f (diff) | |
| download | rust-d8bdb3fdcbd88eb16e1a6669236122c41ed2aed3.tar.gz rust-d8bdb3fdcbd88eb16e1a6669236122c41ed2aed3.zip | |
Auto merge of #66887 - dtolnay:rollup-uxowp8d, r=Centril
Rollup of 4 pull requests Successful merges: - #66818 (Format libstd/os with rustfmt) - #66819 (Format libstd/sys with rustfmt) - #66820 (Format libstd with rustfmt) - #66847 (Allow any identifier as format arg name) Failed merges: r? @ghost
Diffstat (limited to 'src/libstd/process.rs')
| -rw-r--r-- | src/libstd/process.rs | 219 |
1 files changed, 133 insertions, 86 deletions
diff --git a/src/libstd/process.rs b/src/libstd/process.rs index b1274a08cbe..3eee45d000c 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -186,7 +186,9 @@ pub struct Child { } impl AsInner<imp::Process> for Child { - fn as_inner(&self) -> &imp::Process { &self.handle } + fn as_inner(&self) -> &imp::Process { + &self.handle + } } impl FromInner<(imp::Process, imp::StdioPipes)> for Child { @@ -201,7 +203,9 @@ impl FromInner<(imp::Process, imp::StdioPipes)> for Child { } impl IntoInner<imp::Process> for Child { - fn into_inner(self) -> imp::Process { self.handle } + fn into_inner(self) -> imp::Process { + self.handle + } } #[stable(feature = "std_debug", since = "1.16.0")] @@ -228,7 +232,7 @@ impl fmt::Debug for Child { /// [dropped]: ../ops/trait.Drop.html #[stable(feature = "process", since = "1.0.0")] pub struct ChildStdin { - inner: AnonPipe + inner: AnonPipe, } #[stable(feature = "process", since = "1.0.0")] @@ -247,11 +251,15 @@ impl Write for ChildStdin { } impl AsInner<AnonPipe> for ChildStdin { - fn as_inner(&self) -> &AnonPipe { &self.inner } + fn as_inner(&self) -> &AnonPipe { + &self.inner + } } impl IntoInner<AnonPipe> for ChildStdin { - fn into_inner(self) -> AnonPipe { self.inner } + fn into_inner(self) -> AnonPipe { + self.inner + } } impl FromInner<AnonPipe> for ChildStdin { @@ -279,7 +287,7 @@ impl fmt::Debug for ChildStdin { /// [dropped]: ../ops/trait.Drop.html #[stable(feature = "process", since = "1.0.0")] pub struct ChildStdout { - inner: AnonPipe + inner: AnonPipe, } #[stable(feature = "process", since = "1.0.0")] @@ -299,11 +307,15 @@ impl Read for ChildStdout { } impl AsInner<AnonPipe> for ChildStdout { - fn as_inner(&self) -> &AnonPipe { &self.inner } + fn as_inner(&self) -> &AnonPipe { + &self.inner + } } impl IntoInner<AnonPipe> for ChildStdout { - fn into_inner(self) -> AnonPipe { self.inner } + fn into_inner(self) -> AnonPipe { + self.inner + } } impl FromInner<AnonPipe> for ChildStdout { @@ -331,7 +343,7 @@ impl fmt::Debug for ChildStdout { /// [dropped]: ../ops/trait.Drop.html #[stable(feature = "process", since = "1.0.0")] pub struct ChildStderr { - inner: AnonPipe + inner: AnonPipe, } #[stable(feature = "process", since = "1.0.0")] @@ -351,11 +363,15 @@ impl Read for ChildStderr { } impl AsInner<AnonPipe> for ChildStderr { - fn as_inner(&self) -> &AnonPipe { &self.inner } + fn as_inner(&self) -> &AnonPipe { + &self.inner + } } impl IntoInner<AnonPipe> for ChildStderr { - fn into_inner(self) -> AnonPipe { self.inner } + fn into_inner(self) -> AnonPipe { + self.inner + } } impl FromInner<AnonPipe> for ChildStderr { @@ -533,7 +549,9 @@ impl Command { /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn args<I, S>(&mut self, args: I) -> &mut Command - where I: IntoIterator<Item=S>, S: AsRef<OsStr> + where + I: IntoIterator<Item = S>, + S: AsRef<OsStr>, { for arg in args { self.arg(arg.as_ref()); @@ -560,7 +578,9 @@ impl Command { /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command - where K: AsRef<OsStr>, V: AsRef<OsStr> + where + K: AsRef<OsStr>, + V: AsRef<OsStr>, { self.inner.env_mut().set(key.as_ref(), val.as_ref()); self @@ -592,7 +612,10 @@ impl Command { /// ``` #[stable(feature = "command_envs", since = "1.19.0")] pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command - where I: IntoIterator<Item=(K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr> + where + I: IntoIterator<Item = (K, V)>, + K: AsRef<OsStr>, + V: AsRef<OsStr>, { for (ref key, ref val) in vars { self.inner.env_mut().set(key.as_ref(), val.as_ref()); @@ -794,7 +817,9 @@ impl Command { /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn output(&mut self) -> io::Result<Output> { - self.inner.spawn(imp::Stdio::MakePipe, false).map(Child::from_inner) + self.inner + .spawn(imp::Stdio::MakePipe, false) + .map(Child::from_inner) .and_then(|p| p.wait_with_output()) } @@ -819,8 +844,10 @@ impl Command { /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn status(&mut self) -> io::Result<ExitStatus> { - self.inner.spawn(imp::Stdio::Inherit, true).map(Child::from_inner) - .and_then(|mut p| p.wait()) + self.inner + .spawn(imp::Stdio::Inherit, true) + .map(Child::from_inner) + .and_then(|mut p| p.wait()) } } @@ -835,11 +862,15 @@ impl fmt::Debug for Command { } impl AsInner<imp::Command> for Command { - fn as_inner(&self) -> &imp::Command { &self.inner } + fn as_inner(&self) -> &imp::Command { + &self.inner + } } impl AsInnerMut<imp::Command> for Command { - fn as_inner_mut(&mut self) -> &mut imp::Command { &mut self.inner } + fn as_inner_mut(&mut self) -> &mut imp::Command { + &mut self.inner + } } /// The output of a finished process. @@ -871,17 +902,16 @@ pub struct Output { #[stable(feature = "process_output_debug", since = "1.7.0")] impl fmt::Debug for Output { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let stdout_utf8 = str::from_utf8(&self.stdout); let stdout_debug: &dyn fmt::Debug = match stdout_utf8 { Ok(ref str) => str, - Err(_) => &self.stdout + Err(_) => &self.stdout, }; let stderr_utf8 = str::from_utf8(&self.stderr); let stderr_debug: &dyn fmt::Debug = match stderr_utf8 { Ok(ref str) => str, - Err(_) => &self.stderr + Err(_) => &self.stderr, }; fmt.debug_struct("Output") @@ -943,7 +973,9 @@ impl Stdio { /// assert_eq!(String::from_utf8_lossy(&output.stdout), "!dlrow ,olleH"); /// ``` #[stable(feature = "process", since = "1.0.0")] - pub fn piped() -> Stdio { Stdio(imp::Stdio::MakePipe) } + pub fn piped() -> Stdio { + Stdio(imp::Stdio::MakePipe) + } /// The child inherits from the corresponding parent descriptor. /// @@ -980,7 +1012,9 @@ impl Stdio { /// io::stdout().write_all(&output.stdout).unwrap(); /// ``` #[stable(feature = "process", since = "1.0.0")] - pub fn inherit() -> Stdio { Stdio(imp::Stdio::Inherit) } + pub fn inherit() -> Stdio { + Stdio(imp::Stdio::Inherit) + } /// This stream will be ignored. This is the equivalent of attaching the /// stream to `/dev/null` @@ -1017,7 +1051,9 @@ impl Stdio { /// // Ignores any piped-in input /// ``` #[stable(feature = "process", since = "1.0.0")] - pub fn null() -> Stdio { Stdio(imp::Stdio::Null) } + pub fn null() -> Stdio { + Stdio(imp::Stdio::Null) + } } impl FromInner<imp::Stdio> for Stdio { @@ -1217,7 +1253,9 @@ impl ExitStatus { } impl AsInner<imp::ExitStatus> for ExitStatus { - fn as_inner(&self) -> &imp::ExitStatus { &self.0 } + fn as_inner(&self) -> &imp::ExitStatus { + &self.0 + } } impl FromInner<imp::ExitStatus> for ExitStatus { @@ -1449,11 +1487,7 @@ impl Child { } let status = self.wait()?; - Ok(Output { - status, - stdout, - stderr, - }) + Ok(Output { status, stdout, stderr }) } } @@ -1604,8 +1638,9 @@ pub fn id() -> u32 { #[cfg_attr(not(test), lang = "termination")] #[unstable(feature = "termination_trait_lib", issue = "43301")] #[rustc_on_unimplemented( - message="`main` has invalid return type `{Self}`", - label="`main` can only return types that implement `{Termination}`")] + message = "`main` has invalid return type `{Self}`", + label = "`main` can only return types that implement `{Termination}`" +)] pub trait Termination { /// Is called to get the representation of the value as status code. /// This status code is returned to the operating system. @@ -1615,7 +1650,9 @@ pub trait Termination { #[unstable(feature = "termination_trait_lib", issue = "43301")] impl Termination for () { #[inline] - fn report(self) -> i32 { ExitCode::SUCCESS.report() } + fn report(self) -> i32 { + ExitCode::SUCCESS.report() + } } #[unstable(feature = "termination_trait_lib", issue = "43301")] @@ -1630,7 +1667,9 @@ impl<E: fmt::Debug> Termination for Result<(), E> { #[unstable(feature = "termination_trait_lib", issue = "43301")] impl Termination for ! { - fn report(self) -> i32 { self } + fn report(self) -> i32 { + self + } } #[unstable(feature = "termination_trait_lib", issue = "43301")] @@ -1654,9 +1693,9 @@ impl Termination for ExitCode { mod tests { use crate::io::prelude::*; + use super::{Command, Output, Stdio}; use crate::io::ErrorKind; use crate::str; - use super::{Command, Output, Stdio}; // FIXME(#10380) these tests should not all be ignored on android. @@ -1702,15 +1741,12 @@ mod tests { fn signal_reported_right() { use crate::os::unix::process::ExitStatusExt; - let mut p = Command::new("/bin/sh") - .arg("-c").arg("read a") - .stdin(Stdio::piped()) - .spawn().unwrap(); + let mut p = + Command::new("/bin/sh").arg("-c").arg("read a").stdin(Stdio::piped()).spawn().unwrap(); p.kill().unwrap(); match p.wait().unwrap().signal() { - Some(9) => {}, - result => panic!("not terminated by signal 9 (instead, {:?})", - result), + Some(9) => {} + result => panic!("not terminated by signal 9 (instead, {:?})", result), } } @@ -1743,9 +1779,7 @@ mod tests { #[cfg_attr(any(windows, target_os = "android", target_os = "vxworks"), ignore)] fn set_current_dir_works() { let mut cmd = Command::new("/bin/sh"); - cmd.arg("-c").arg("pwd") - .current_dir("/") - .stdout(Stdio::piped()); + cmd.arg("-c").arg("pwd").current_dir("/").stdout(Stdio::piped()); assert_eq!(run_output(cmd), "/\n"); } @@ -1753,10 +1787,12 @@ mod tests { #[cfg_attr(any(windows, target_os = "android", target_os = "vxworks"), ignore)] fn stdin_works() { let mut p = Command::new("/bin/sh") - .arg("-c").arg("read line; echo $line") - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn().unwrap(); + .arg("-c") + .arg("read line; echo $line") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap(); drop(p.stdin.take()); let mut out = String::new(); @@ -1787,19 +1823,18 @@ mod tests { fn test_process_output_fail_to_start() { match Command::new("/no-binary-by-this-name-should-exist").output() { Err(e) => assert_eq!(e.kind(), ErrorKind::NotFound), - Ok(..) => panic!() + Ok(..) => panic!(), } } #[test] #[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] fn test_process_output_output() { - let Output {status, stdout, stderr} - = if cfg!(target_os = "windows") { - Command::new("cmd").args(&["/C", "echo hello"]).output().unwrap() - } else { - Command::new("echo").arg("hello").output().unwrap() - }; + let Output { status, stdout, stderr } = if cfg!(target_os = "windows") { + Command::new("cmd").args(&["/C", "echo hello"]).output().unwrap() + } else { + Command::new("echo").arg("hello").output().unwrap() + }; let output_str = str::from_utf8(&stdout).unwrap(); assert!(status.success()); @@ -1810,12 +1845,11 @@ mod tests { #[test] #[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)] fn test_process_output_error() { - let Output {status, stdout, stderr} - = if cfg!(target_os = "windows") { - Command::new("cmd").args(&["/C", "mkdir ."]).output().unwrap() - } else { - Command::new("mkdir").arg("./").output().unwrap() - }; + let Output { status, stdout, stderr } = if cfg!(target_os = "windows") { + Command::new("cmd").args(&["/C", "mkdir ."]).output().unwrap() + } else { + Command::new("mkdir").arg("./").output().unwrap() + }; assert!(status.code() == Some(1)); assert_eq!(stdout, Vec::new()); @@ -1854,7 +1888,7 @@ mod tests { Command::new("echo").arg("hello").stdout(Stdio::piped()).spawn().unwrap() }; - let Output {status, stdout, stderr} = prog.wait_with_output().unwrap(); + let Output { status, stdout, stderr } = prog.wait_with_output().unwrap(); let output_str = str::from_utf8(&stdout).unwrap(); assert!(status.success()); @@ -1862,11 +1896,11 @@ mod tests { assert_eq!(stderr, Vec::new()); } - #[cfg(all(unix, not(target_os="android")))] + #[cfg(all(unix, not(target_os = "android")))] pub fn env_cmd() -> Command { Command::new("env") } - #[cfg(target_os="android")] + #[cfg(target_os = "android")] pub fn env_cmd() -> Command { let mut cmd = Command::new("/system/bin/sh"); cmd.arg("-c").arg("set"); @@ -1897,8 +1931,11 @@ mod tests { let result = cmd.output().unwrap(); let output = String::from_utf8_lossy(&result.stdout).to_string(); - assert!(output.contains("RUN_TEST_NEW_ENV=123"), - "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); + assert!( + output.contains("RUN_TEST_NEW_ENV=123"), + "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", + output + ); } #[test] @@ -1907,8 +1944,11 @@ mod tests { let result = env_cmd().env("RUN_TEST_NEW_ENV", "123").output().unwrap(); let output = String::from_utf8_lossy(&result.stdout).to_string(); - assert!(output.contains("RUN_TEST_NEW_ENV=123"), - "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); + assert!( + output.contains("RUN_TEST_NEW_ENV=123"), + "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", + output + ); } #[test] @@ -1927,10 +1967,16 @@ mod tests { let output = String::from_utf8_lossy(&result.stdout).to_string(); - assert!(output.contains("RUN_TEST_NEW_ENV1=123"), - "didn't find RUN_TEST_NEW_ENV1 inside of:\n\n{}", output); - assert!(output.contains("RUN_TEST_NEW_ENV2=456"), - "didn't find RUN_TEST_NEW_ENV2 inside of:\n\n{}", output); + assert!( + output.contains("RUN_TEST_NEW_ENV1=123"), + "didn't find RUN_TEST_NEW_ENV1 inside of:\n\n{}", + output + ); + assert!( + output.contains("RUN_TEST_NEW_ENV2=456"), + "didn't find RUN_TEST_NEW_ENV2 inside of:\n\n{}", + output + ); } // Regression tests for #30858. @@ -2005,8 +2051,11 @@ mod tests { extern "system" { fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: DWORD) -> BOOL; - fn ContinueDebugEvent(dwProcessId: DWORD, dwThreadId: DWORD, - dwContinueStatus: DWORD) -> BOOL; + fn ContinueDebugEvent( + dwProcessId: DWORD, + dwThreadId: DWORD, + dwContinueStatus: DWORD, + ) -> BOOL; } const DEBUG_PROCESS: DWORD = 1; @@ -2015,15 +2064,12 @@ mod tests { let mut child = Command::new("cmd") .creation_flags(DEBUG_PROCESS) - .stdin(Stdio::piped()).spawn().unwrap(); + .stdin(Stdio::piped()) + .spawn() + .unwrap(); child.stdin.take().unwrap().write_all(b"exit\r\n").unwrap(); let mut events = 0; - let mut event = DEBUG_EVENT { - event_code: 0, - process_id: 0, - thread_id: 0, - _junk: [0; 164], - }; + let mut event = DEBUG_EVENT { event_code: 0, process_id: 0, thread_id: 0, _junk: [0; 164] }; loop { if unsafe { WaitForDebugEvent(&mut event as *mut DEBUG_EVENT, INFINITE) } == 0 { panic!("WaitForDebugEvent failed!"); @@ -2034,9 +2080,10 @@ mod tests { break; } - if unsafe { ContinueDebugEvent(event.process_id, - event.thread_id, - DBG_EXCEPTION_NOT_HANDLED) } == 0 { + if unsafe { + ContinueDebugEvent(event.process_id, event.thread_id, DBG_EXCEPTION_NOT_HANDLED) + } == 0 + { panic!("ContinueDebugEvent failed!"); } } |
