From 2580950fcd7c516ebd2fc090443b5406a18f77bb Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Sat, 21 Jan 2017 11:01:11 -0500 Subject: [PATCH] Generalize envs() and args() to iterators. * Command::envs() now takes anything that is IntoIterator where both K and V are AsRef. * Since we're not 100% sure that's the right signature, envs() is now marked unstable. (You can use envs() with HashMap but not Vec<(str, str)>, for instance.) * Update the test to match. * By analogy, args() now takes any IntoIterator, S: AsRef. This should be uncontroversial. --- src/libstd/process.rs | 15 +++++++++------ src/test/run-pass/process-envs.rs | 5 ++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 1b1f2291826..d4dbbec1fee 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -345,7 +345,9 @@ impl Command { /// .expect("ls command failed to start"); /// ``` #[stable(feature = "process", since = "1.0.0")] - pub fn args>(&mut self, args: &[S]) -> &mut Command { + pub fn args(&mut self, args: I) -> &mut Command + where I: IntoIterator, S: AsRef + { for arg in args { self.arg(arg.as_ref()); } @@ -385,8 +387,9 @@ impl Command { /// ```no_run /// use std::process::{Command, Stdio}; /// use std::env; + /// use std::collections::HashMap; /// - /// let filtered_env : Vec<(String, String)> = + /// let filtered_env : HashMap = /// env::vars().filter(|&(ref k, _)| /// k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH" /// ).collect(); @@ -399,11 +402,11 @@ impl Command { /// .spawn() /// .expect("printenv failed to start"); /// ``` - #[stable(feature = "command_envs", since = "1.16.0")] - pub fn envs(&mut self, vars: &[(K, V)]) -> &mut Command - where K: AsRef, V: AsRef + #[unstable(feature = "command_envs", issue = "38526")] + pub fn envs(&mut self, vars: I) -> &mut Command + where I: IntoIterator, K: AsRef, V: AsRef { - for &(ref key, ref val) in vars { + for (ref key, ref val) in vars { self.inner.env(key.as_ref(), val.as_ref()); } self diff --git a/src/test/run-pass/process-envs.rs b/src/test/run-pass/process-envs.rs index 80ff16aadb2..a131dcbe4dd 100644 --- a/src/test/run-pass/process-envs.rs +++ b/src/test/run-pass/process-envs.rs @@ -10,8 +10,11 @@ // ignore-emscripten +#![feature(command_envs)] + use std::process::Command; use std::env; +use std::collections::HashMap; #[cfg(all(unix, not(target_os="android")))] pub fn env_cmd() -> Command { @@ -38,7 +41,7 @@ fn main() { env::set_var("RUN_TEST_NEW_ENV", "123"); // create filtered environment vector - let filtered_env : Vec<(String, String)> = + let filtered_env : HashMap = env::vars().filter(|&(ref k, _)| k == "PATH").collect(); let mut cmd = env_cmd();