about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-01-25 20:24:02 +0000
committerbors <bors@rust-lang.org>2017-01-25 20:24:02 +0000
commit94d4589388f3c82b4042276d8fd78f8d8009b531 (patch)
tree1dd9abbdb8090ed6cde6d668eb0bd14fd4d702d8 /src/libstd
parentfc57e40ce703cb70a39249f5cfb661020a1549b6 (diff)
parent2580950fcd7c516ebd2fc090443b5406a18f77bb (diff)
downloadrust-94d4589388f3c82b4042276d8fd78f8d8009b531.tar.gz
rust-94d4589388f3c82b4042276d8fd78f8d8009b531.zip
Auto merge of #38856 - zackw:process-envs, r=aturon
Add std::process::Command::envs()

`Command::envs()` adds a vector of key-value pairs to the child
process environment all at once.  Suggested in #38526.

This is not fully baked and frankly I'm not sure it even _works_, but I need some help finishing it up, and this is the simplest way to show you what I've got.  The problems I know exist and don't know how to solve, from most to least important, are:

* [ ] I don't know if the type signature of the new function is correct.
* [x] The new test might not be getting run.  I didn't see it go by in the output of `x.py test src/libstd --stage 1`.
* [x] The tidy check says ``process.rs:402: different `since` than before`` which I don't know what it means.

r? @brson
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/process.rs37
1 files changed, 36 insertions, 1 deletions
diff --git a/src/libstd/process.rs b/src/libstd/process.rs
index aa76b792535..5af4ba53bf9 100644
--- a/src/libstd/process.rs
+++ b/src/libstd/process.rs
@@ -352,7 +352,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());
         }
@@ -384,6 +386,39 @@ impl Command {
         self
     }
 
+    /// Add or update multiple environment variable mappings.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    /// ```no_run
+    /// use std::process::{Command, Stdio};
+    /// use std::env;
+    /// use std::collections::HashMap;
+    ///
+    /// let filtered_env : HashMap<String, String> =
+    ///     env::vars().filter(|&(ref k, _)|
+    ///         k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH"
+    ///     ).collect();
+    ///
+    /// Command::new("printenv")
+    ///         .stdin(Stdio::null())
+    ///         .stdout(Stdio::inherit())
+    ///         .env_clear()
+    ///         .envs(&filtered_env)
+    ///         .spawn()
+    ///         .expect("printenv failed to start");
+    /// ```
+    #[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 {
+            self.inner.env(key.as_ref(), val.as_ref());
+        }
+        self
+    }
+
     /// Removes an environment variable mapping.
     ///
     /// # Examples