Generalize envs() and args() to iterators.

* Command::envs() now takes anything that is IntoIterator<Item=(K, V)>
   where both K and V are AsRef<OsStr>.
 * Since we're not 100% sure that's the right signature, envs() is
   now marked unstable.  (You can use envs() with HashMap<str, str> but
   not Vec<(str, str)>, for instance.)
 * Update the test to match.

 * By analogy, args() now takes any IntoIterator<Item=S>, S: AsRef<OsStr>.
   This should be uncontroversial.
This commit is contained in:
Zack Weinberg 2017-01-21 11:01:11 -05:00
parent c74efddc89
commit 2580950fcd
2 changed files with 13 additions and 7 deletions

View File

@ -345,7 +345,9 @@ impl Command {
/// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command {
pub fn args<I, S>(&mut self, args: I) -> &mut Command
where I: IntoIterator<Item=S>, S: AsRef<OsStr>
{
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<String, String> =
/// 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<K, V>(&mut self, vars: &[(K, V)]) -> &mut Command
where K: AsRef<OsStr>, V: AsRef<OsStr>
#[unstable(feature = "command_envs", issue = "38526")]
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
where I: IntoIterator<Item=(K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>
{
for &(ref key, ref val) in vars {
for (ref key, ref val) in vars {
self.inner.env(key.as_ref(), val.as_ref());
}
self

View File

@ -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<String, String> =
env::vars().filter(|&(ref k, _)| k == "PATH").collect();
let mut cmd = env_cmd();