]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys_common/process.rs
Rollup merge of #96173 - jmaargh:jmaargh/with-capacity-doc-fix, r=Dylan-DPC
[rust.git] / library / std / src / sys_common / process.rs
1 #![allow(dead_code)]
2 #![unstable(feature = "process_internals", issue = "none")]
3
4 use crate::collections::BTreeMap;
5 use crate::env;
6 use crate::ffi::{OsStr, OsString};
7 use crate::sys::process::EnvKey;
8
9 // Stores a set of changes to an environment
10 #[derive(Clone, Debug)]
11 pub struct CommandEnv {
12     clear: bool,
13     saw_path: bool,
14     vars: BTreeMap<EnvKey, Option<OsString>>,
15 }
16
17 impl Default for CommandEnv {
18     fn default() -> Self {
19         CommandEnv { clear: false, saw_path: false, vars: Default::default() }
20     }
21 }
22
23 impl CommandEnv {
24     // Capture the current environment with these changes applied
25     pub fn capture(&self) -> BTreeMap<EnvKey, OsString> {
26         let mut result = BTreeMap::<EnvKey, OsString>::new();
27         if !self.clear {
28             for (k, v) in env::vars_os() {
29                 result.insert(k.into(), v);
30             }
31         }
32         for (k, maybe_v) in &self.vars {
33             if let &Some(ref v) = maybe_v {
34                 result.insert(k.clone(), v.clone());
35             } else {
36                 result.remove(k);
37             }
38         }
39         result
40     }
41
42     pub fn is_unchanged(&self) -> bool {
43         !self.clear && self.vars.is_empty()
44     }
45
46     pub fn capture_if_changed(&self) -> Option<BTreeMap<EnvKey, OsString>> {
47         if self.is_unchanged() { None } else { Some(self.capture()) }
48     }
49
50     // The following functions build up changes
51     pub fn set(&mut self, key: &OsStr, value: &OsStr) {
52         let key = EnvKey::from(key);
53         self.maybe_saw_path(&key);
54         self.vars.insert(key, Some(value.to_owned()));
55     }
56
57     pub fn remove(&mut self, key: &OsStr) {
58         let key = EnvKey::from(key);
59         self.maybe_saw_path(&key);
60         if self.clear {
61             self.vars.remove(&key);
62         } else {
63             self.vars.insert(key, None);
64         }
65     }
66
67     pub fn clear(&mut self) {
68         self.clear = true;
69         self.vars.clear();
70     }
71
72     pub fn have_changed_path(&self) -> bool {
73         self.saw_path || self.clear
74     }
75
76     fn maybe_saw_path(&mut self, key: &EnvKey) {
77         if !self.saw_path && key == "PATH" {
78             self.saw_path = true;
79         }
80     }
81
82     pub fn iter(&self) -> CommandEnvs<'_> {
83         let iter = self.vars.iter();
84         CommandEnvs { iter }
85     }
86 }
87
88 /// An iterator over the command environment variables.
89 ///
90 /// This struct is created by
91 /// [`Command::get_envs`][crate::process::Command::get_envs]. See its
92 /// documentation for more.
93 #[must_use = "iterators are lazy and do nothing unless consumed"]
94 #[stable(feature = "command_access", since = "1.57.0")]
95 #[derive(Debug)]
96 pub struct CommandEnvs<'a> {
97     iter: crate::collections::btree_map::Iter<'a, EnvKey, Option<OsString>>,
98 }
99
100 #[stable(feature = "command_access", since = "1.57.0")]
101 impl<'a> Iterator for CommandEnvs<'a> {
102     type Item = (&'a OsStr, Option<&'a OsStr>);
103     fn next(&mut self) -> Option<Self::Item> {
104         self.iter.next().map(|(key, value)| (key.as_ref(), value.as_deref()))
105     }
106     fn size_hint(&self) -> (usize, Option<usize>) {
107         self.iter.size_hint()
108     }
109 }
110
111 #[stable(feature = "command_access", since = "1.57.0")]
112 impl<'a> ExactSizeIterator for CommandEnvs<'a> {
113     fn len(&self) -> usize {
114         self.iter.len()
115     }
116     fn is_empty(&self) -> bool {
117         self.iter.is_empty()
118     }
119 }