Skip to content

Commit

Permalink
Generalize envs() and args() to iterators.
Browse files Browse the repository at this point in the history
 * 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.
  • Loading branch information
zackw committed Jan 21, 2017
1 parent c74efdd commit 2580950
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/test/run-pass/process-envs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down

0 comments on commit 2580950

Please sign in to comment.