From 0193c1ede06e5f3fdc5a6af34265128454ae7db1 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 29 Jan 2025 03:13:58 -0500 Subject: [PATCH] wip --- src/blocking/command.rs | 59 +++++++++++++++++++-------------------- src/command.rs | 62 ++++++++++++++++++++--------------------- tests/behavior.rs | 26 ++++++++--------- tests/pipe.rs | 26 ++++++++--------- tests/split.rs | 8 +++--- 5 files changed, 89 insertions(+), 92 deletions(-) diff --git a/src/blocking/command.rs b/src/blocking/command.rs index 691649c..083f3e3 100644 --- a/src/blocking/command.rs +++ b/src/blocking/command.rs @@ -26,13 +26,15 @@ impl Command { } /// See [`std::process::Command::arg`] - pub fn arg>(&mut self, arg: S) -> &mut Self { + #[must_use] + pub fn arg>(mut self, arg: S) -> Self { self.inner.arg(arg); self } /// See [`std::process::Command::args`] - pub fn args(&mut self, args: I) -> &mut Self + #[must_use] + pub fn args(mut self, args: I) -> Self where I: IntoIterator, S: AsRef, @@ -42,7 +44,8 @@ impl Command { } /// See [`std::process::Command::env`] - pub fn env(&mut self, key: K, val: V) -> &mut Self + #[must_use] + pub fn env(mut self, key: K, val: V) -> Self where K: AsRef, V: AsRef, @@ -52,7 +55,8 @@ impl Command { } /// See [`std::process::Command::envs`] - pub fn envs(&mut self, vars: I) -> &mut Self + #[must_use] + pub fn envs(mut self, vars: I) -> Self where I: IntoIterator, K: AsRef, @@ -63,54 +67,45 @@ impl Command { } /// See [`std::process::Command::env_remove`] - pub fn env_remove>( - &mut self, - key: K, - ) -> &mut Self { + #[must_use] + pub fn env_remove>(mut self, key: K) -> Self { self.inner.env_remove(key); self } /// See [`std::process::Command::env_clear`] - pub fn env_clear(&mut self) -> &mut Self { + #[must_use] + pub fn env_clear(mut self) -> Self { self.inner.env_clear(); self } /// See [`std::process::Command::current_dir`] - pub fn current_dir>( - &mut self, - dir: P, - ) -> &mut Self { + #[must_use] + pub fn current_dir>(mut self, dir: P) -> Self { self.inner.current_dir(dir); self } /// See [`std::process::Command::stdin`] - pub fn stdin>( - &mut self, - cfg: T, - ) -> &mut Self { + #[must_use] + pub fn stdin>(mut self, cfg: T) -> Self { self.stdin = true; self.inner.stdin(cfg); self } /// See [`std::process::Command::stdout`] - pub fn stdout>( - &mut self, - cfg: T, - ) -> &mut Self { + #[must_use] + pub fn stdout>(mut self, cfg: T) -> Self { self.stdout = true; self.inner.stdout(cfg); self } /// See [`std::process::Command::stderr`] - pub fn stderr>( - &mut self, - cfg: T, - ) -> &mut Self { + #[must_use] + pub fn stderr>(mut self, cfg: T) -> Self { self.stderr = true; self.inner.stderr(cfg); self @@ -133,7 +128,7 @@ impl Command { /// session leader or set its controlling terminal. #[allow(clippy::needless_pass_by_value)] pub fn spawn( - &mut self, + mut self, pts: crate::blocking::Pts, ) -> crate::Result { self.spawn_impl(&pts) @@ -202,20 +197,23 @@ impl Command { } /// See [`std::os::unix::process::CommandExt::uid`] - pub fn uid(&mut self, id: u32) -> &mut Self { + #[must_use] + pub fn uid(mut self, id: u32) -> Self { self.inner.uid(id); self } /// See [`std::os::unix::process::CommandExt::gid`] - pub fn gid(&mut self, id: u32) -> &mut Self { + #[must_use] + pub fn gid(mut self, id: u32) -> Self { self.inner.gid(id); self } /// See [`std::os::unix::process::CommandExt::pre_exec`] #[allow(clippy::missing_safety_doc)] - pub unsafe fn pre_exec(&mut self, f: F) -> &mut Self + #[must_use] + pub unsafe fn pre_exec(mut self, f: F) -> Self where F: FnMut() -> std::io::Result<()> + Send + Sync + 'static, { @@ -224,7 +222,8 @@ impl Command { } /// See [`std::os::unix::process::CommandExt::arg0`] - pub fn arg0(&mut self, arg: S) -> &mut Self + #[must_use] + pub fn arg0(mut self, arg: S) -> Self where S: AsRef, { diff --git a/src/command.rs b/src/command.rs index 634dd95..2ff0ffd 100644 --- a/src/command.rs +++ b/src/command.rs @@ -24,13 +24,15 @@ impl Command { } /// See [`tokio::process::Command::arg`] - pub fn arg>(&mut self, arg: S) -> &mut Self { + #[must_use] + pub fn arg>(mut self, arg: S) -> Self { self.inner.arg(arg); self } /// See [`tokio::process::Command::args`] - pub fn args(&mut self, args: I) -> &mut Self + #[must_use] + pub fn args(mut self, args: I) -> Self where I: IntoIterator, S: AsRef, @@ -40,7 +42,8 @@ impl Command { } /// See [`tokio::process::Command::env`] - pub fn env(&mut self, key: K, val: V) -> &mut Self + #[must_use] + pub fn env(mut self, key: K, val: V) -> Self where K: AsRef, V: AsRef, @@ -50,7 +53,8 @@ impl Command { } /// See [`tokio::process::Command::envs`] - pub fn envs(&mut self, vars: I) -> &mut Self + #[must_use] + pub fn envs(mut self, vars: I) -> Self where I: IntoIterator, K: AsRef, @@ -61,60 +65,52 @@ impl Command { } /// See [`tokio::process::Command::env_remove`] - pub fn env_remove>( - &mut self, - key: K, - ) -> &mut Self { + #[must_use] + pub fn env_remove>(mut self, key: K) -> Self { self.inner.env_remove(key); self } /// See [`tokio::process::Command::env_clear`] - pub fn env_clear(&mut self) -> &mut Self { + #[must_use] + pub fn env_clear(mut self) -> Self { self.inner.env_clear(); self } /// See [`tokio::process::Command::current_dir`] - pub fn current_dir>( - &mut self, - dir: P, - ) -> &mut Self { + #[must_use] + pub fn current_dir>(mut self, dir: P) -> Self { self.inner.current_dir(dir); self } /// See [`tokio::process::Command::kill_on_drop`] - pub fn kill_on_drop(&mut self, kill_on_drop: bool) -> &mut Self { + #[must_use] + pub fn kill_on_drop(mut self, kill_on_drop: bool) -> Self { self.inner.kill_on_drop(kill_on_drop); self } /// See [`tokio::process::Command::stdin`] - pub fn stdin>( - &mut self, - cfg: T, - ) -> &mut Self { + #[must_use] + pub fn stdin>(mut self, cfg: T) -> Self { self.stdin = true; self.inner.stdin(cfg); self } /// See [`tokio::process::Command::stdout`] - pub fn stdout>( - &mut self, - cfg: T, - ) -> &mut Self { + #[must_use] + pub fn stdout>(mut self, cfg: T) -> Self { self.stdout = true; self.inner.stdout(cfg); self } /// See [`tokio::process::Command::stderr`] - pub fn stderr>( - &mut self, - cfg: T, - ) -> &mut Self { + #[must_use] + pub fn stderr>(mut self, cfg: T) -> Self { self.stderr = true; self.inner.stderr(cfg); self @@ -137,7 +133,7 @@ impl Command { /// session leader or set its controlling terminal. #[allow(clippy::needless_pass_by_value)] pub fn spawn( - &mut self, + mut self, pts: crate::Pts, ) -> crate::Result { self.spawn_impl(&pts) @@ -206,20 +202,23 @@ impl Command { } /// See [`tokio::process::Command::uid`] - pub fn uid(&mut self, id: u32) -> &mut Self { + #[must_use] + pub fn uid(mut self, id: u32) -> Self { self.inner.uid(id); self } /// See [`tokio::process::Command::gid`] - pub fn gid(&mut self, id: u32) -> &mut Self { + #[must_use] + pub fn gid(mut self, id: u32) -> Self { self.inner.gid(id); self } /// See [`tokio::process::Command::pre_exec`] #[allow(clippy::missing_safety_doc)] - pub unsafe fn pre_exec(&mut self, f: F) -> &mut Self + #[must_use] + pub unsafe fn pre_exec(mut self, f: F) -> Self where F: FnMut() -> std::io::Result<()> + Send + Sync + 'static, { @@ -228,7 +227,8 @@ impl Command { } /// See [`tokio::process::Command::arg0`] - pub fn arg0(&mut self, arg: S) -> &mut Self + #[must_use] + pub fn arg0(mut self, arg: S) -> Self where S: AsRef, { diff --git a/tests/behavior.rs b/tests/behavior.rs index 28b2351..6e849d4 100644 --- a/tests/behavior.rs +++ b/tests/behavior.rs @@ -75,10 +75,10 @@ fn test_multiple_configured() { let (pre_exec_pipe_r, pre_exec_pipe_w) = pipe(); let mut pre_exec_pipe_r = std::io::BufReader::new(std::fs::File::from(pre_exec_pipe_r)); - let mut cmd = pty_process::blocking::Command::new("perl"); - cmd.arg("-Esay 'foo'; say STDERR 'foo-stderr'; open my $fh, '>&=3'; say $fh 'foo-3';") + let cmd = pty_process::blocking::Command::new("perl") + .arg("-Esay 'foo'; say STDERR 'foo-stderr'; open my $fh, '>&=3'; say $fh 'foo-3';") .stderr(std::process::Stdio::from(stderr_pipe_w)); - unsafe { + let mut cmd = unsafe { cmd.pre_exec(move || { nix::unistd::dup2(pre_exec_pipe_w.as_raw_fd(), 3)?; nix::fcntl::fcntl( @@ -86,8 +86,8 @@ fn test_multiple_configured() { nix::fcntl::F_SETFD(nix::fcntl::FdFlag::empty()), )?; Ok(()) - }); - } + }) + }; let mut child = cmd.spawn_borrowed(&pts).unwrap(); let mut output = helpers::output(&pty); @@ -149,15 +149,15 @@ async fn test_multiple_configured_async() { let mut pre_exec_pipe_r = tokio::io::BufReader::new(unsafe { tokio::fs::File::from_raw_fd(pre_exec_pipe_r.into_raw_fd()) }); - let mut cmd = pty_process::Command::new("perl"); - cmd.arg( - "-Esay 'foo'; \ + let cmd = pty_process::Command::new("perl") + .arg( + "-Esay 'foo'; \ say STDERR 'foo-stderr'; \ open my $fh, '>&=3'; \ say $fh 'foo-3';", - ) - .stderr(std::process::Stdio::from(stderr_pipe_w)); - unsafe { + ) + .stderr(std::process::Stdio::from(stderr_pipe_w)); + let mut cmd = unsafe { cmd.pre_exec(move || { nix::unistd::dup2(pre_exec_pipe_w.as_raw_fd(), 3)?; nix::fcntl::fcntl( @@ -165,8 +165,8 @@ async fn test_multiple_configured_async() { nix::fcntl::F_SETFD(nix::fcntl::FdFlag::empty()), )?; Ok(()) - }); - } + }) + }; let mut child = cmd.spawn_borrowed(&pts).unwrap(); let mut output = helpers::output_async(pty_r); diff --git a/tests/pipe.rs b/tests/pipe.rs index c1cb1c3..0d06eb5 100644 --- a/tests/pipe.rs +++ b/tests/pipe.rs @@ -31,19 +31,18 @@ fn test_pipe_blocking() { let (pty_from, pts_from) = pty_process::blocking::open().unwrap(); pty_from.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut cmd_from = pty_process::blocking::Command::new("seq"); - cmd_from.args(["1", "10"]); - cmd_from.stdout(std::process::Stdio::from(write_fd)); + let cmd_from = pty_process::blocking::Command::new("seq") + .args(["1", "10"]) + .stdout(std::process::Stdio::from(write_fd)); let mut child_from = cmd_from.spawn(pts_from).unwrap(); let (mut pty_to, pts_to) = pty_process::blocking::open().unwrap(); - let mut cmd_to = pty_process::blocking::Command::new("sh"); - cmd_to.args(["-c", TAC]); - cmd_to.stdin(std::process::Stdio::from(read_fd)); + let cmd_to = pty_process::blocking::Command::new("sh") + .args(["-c", TAC]) + .stdin(std::process::Stdio::from(read_fd)); let mut child_to = cmd_to.spawn(pts_to).unwrap(); assert!(child_from.wait().unwrap().success()); - drop(cmd_from); // wait for the `tac` process to finish generating output (we don't really // have a good way to detect when that happens) @@ -68,19 +67,18 @@ async fn test_pipe_async() { let (pty_from, pts_from) = pty_process::open().unwrap(); pty_from.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut cmd_from = pty_process::Command::new("seq"); - cmd_from.args(["1", "10"]); - cmd_from.stdout(std::process::Stdio::from(write_fd)); + let cmd_from = pty_process::Command::new("seq") + .args(["1", "10"]) + .stdout(std::process::Stdio::from(write_fd)); let mut child_from = cmd_from.spawn(pts_from).unwrap(); let (mut pty_to, pts_to) = pty_process::open().unwrap(); - let mut cmd_to = pty_process::Command::new("sh"); - cmd_to.args(["-c", TAC]); - cmd_to.stdin(std::process::Stdio::from(read_fd)); + let cmd_to = pty_process::Command::new("sh") + .args(["-c", TAC]) + .stdin(std::process::Stdio::from(read_fd)); let mut child_to = cmd_to.spawn(pts_to).unwrap(); assert!(child_from.wait().await.unwrap().success()); - drop(cmd_from); // wait for the `tac` process to finish generating output (we // don't really have a good way to detect when that happens) diff --git a/tests/split.rs b/tests/split.rs index 429821e..b8fddee 100644 --- a/tests/split.rs +++ b/tests/split.rs @@ -8,8 +8,8 @@ async fn test_split() { let (mut pty, pts) = pty_process::open().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut cmd = pty_process::Command::new("perl"); - cmd.args(["-plE", "BEGIN { $SIG{WINCH} = sub { say 'WINCH' } }"]); + let cmd = pty_process::Command::new("perl") + .args(["-plE", "BEGIN { $SIG{WINCH} = sub { say 'WINCH' } }"]); let mut child = cmd.spawn(pts).unwrap(); { @@ -46,8 +46,8 @@ async fn test_into_split() { let (mut pty, pts) = pty_process::open().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut cmd = pty_process::Command::new("perl"); - cmd.args(["-plE", "BEGIN { $SIG{WINCH} = sub { say 'WINCH' } }"]); + let cmd = pty_process::Command::new("perl") + .args(["-plE", "BEGIN { $SIG{WINCH} = sub { say 'WINCH' } }"]); let mut child = cmd.spawn(pts).unwrap(); {