rfcs
rfcs copied to clipboard
Add a method to reset the arguments to `std::process::Command`
It would be cool if this struct has an option to reset all the current arguments, this would be useful fo example here:
use std::{
io::Result,
process::{Child, Command},
};
pub fn get_killer() -> Box<dyn FnMut(&str) -> Result<Child>> {
let is_windows = cfg!(target_os = "windows");
let mut cmd = Command::new(if is_windows { "cmd" } else { "pkill" });
Box::new(move |program| {
cmd.reset_args(); // removing previous args from previous closure calls
if is_windows {
cmd.args(["taskkill", "/IM", program, "/F"]);
} else {
cmd.arg(program);
}
cmd.spawn()
})
}
Using tools possible today:
let make_cmd = move || Command::new(if is_windows { "cmd" } else { "pkill" });
Box::new(move |program| {
let mut cmd = make_cmd();
Using tools possible today:
let make_cmd = move || Command::new(if is_windows { "cmd" } else { "pkill" }); Box::new(move |program| { let mut cmd = make_cmd();
This will require instantiating Command multiple times when we could use the same instance all the time.
when we could use the same instance all the time
What is the expected benefit of using one instance? I believe you will need to prove that benefit.
I feel like it is a fairly safe assumption to make that, on all operating systems, on all platforms and in all environments, the total overhead associated with each call to Command::spawn (when spawning multiple processes) should far exceed the cost of allocating the Command's argument vector.
It's weird that Command have env_clear() while args_clear() is missing 🙁
I propose to add arg_clear() and arg_remove(arg) so it have the same parity with env_clear() and env_remove(env)
It's weird that Command have
env_clear()whileargs_clear()is missing
env_clear says (emphasis mine):
Clears all explicitly set environment variables and prevents inheriting any parent process environment variables.
There are no "inherited parent process arguments", so all arguments were explicitly set. The two are not directly comparable.