Skip to content

Commit 2580950

Browse files
committed
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.
1 parent c74efdd commit 2580950

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/libstd/process.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ impl Command {
345345
/// .expect("ls command failed to start");
346346
/// ```
347347
#[stable(feature = "process", since = "1.0.0")]
348-
pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command {
348+
pub fn args<I, S>(&mut self, args: I) -> &mut Command
349+
where I: IntoIterator<Item=S>, S: AsRef<OsStr>
350+
{
349351
for arg in args {
350352
self.arg(arg.as_ref());
351353
}
@@ -385,8 +387,9 @@ impl Command {
385387
/// ```no_run
386388
/// use std::process::{Command, Stdio};
387389
/// use std::env;
390+
/// use std::collections::HashMap;
388391
///
389-
/// let filtered_env : Vec<(String, String)> =
392+
/// let filtered_env : HashMap<String, String> =
390393
/// env::vars().filter(|&(ref k, _)|
391394
/// k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH"
392395
/// ).collect();
@@ -399,11 +402,11 @@ impl Command {
399402
/// .spawn()
400403
/// .expect("printenv failed to start");
401404
/// ```
402-
#[stable(feature = "command_envs", since = "1.16.0")]
403-
pub fn envs<K, V>(&mut self, vars: &[(K, V)]) -> &mut Command
404-
where K: AsRef<OsStr>, V: AsRef<OsStr>
405+
#[unstable(feature = "command_envs", issue = "38526")]
406+
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
407+
where I: IntoIterator<Item=(K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>
405408
{
406-
for &(ref key, ref val) in vars {
409+
for (ref key, ref val) in vars {
407410
self.inner.env(key.as_ref(), val.as_ref());
408411
}
409412
self

src/test/run-pass/process-envs.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
// ignore-emscripten
1212

13+
#![feature(command_envs)]
14+
1315
use std::process::Command;
1416
use std::env;
17+
use std::collections::HashMap;
1518

1619
#[cfg(all(unix, not(target_os="android")))]
1720
pub fn env_cmd() -> Command {
@@ -38,7 +41,7 @@ fn main() {
3841
env::set_var("RUN_TEST_NEW_ENV", "123");
3942

4043
// create filtered environment vector
41-
let filtered_env : Vec<(String, String)> =
44+
let filtered_env : HashMap<String, String> =
4245
env::vars().filter(|&(ref k, _)| k == "PATH").collect();
4346

4447
let mut cmd = env_cmd();

0 commit comments

Comments
 (0)