@@ -68,21 +68,21 @@ pub fn current_exe() -> io::Result<PathBuf> {
68
68
unsupported()
69
69
}
70
70
71
- static mut ENV: Option< Mutex<HashMap<OsString, OsString>>> = None;
71
+ static ENV: Mutex<Option< HashMap<OsString, OsString>>> = Mutex::new( None) ;
72
72
73
73
pub fn init_environment(env: *const *const i8) {
74
- unsafe {
75
- ENV = Some(Mutex::new( HashMap::new() ));
74
+ let mut guard = ENV.lock().unwrap();
75
+ let map = guard.insert( HashMap::new());
76
76
77
- if env.is_null() {
78
- return;
79
- }
77
+ if env.is_null() {
78
+ return;
79
+ }
80
80
81
- let mut guard = ENV.as_ref().unwrap().lock().unwrap();
81
+ unsafe {
82
82
let mut environ = env;
83
83
while !(*environ).is_null() {
84
84
if let Some((key, value)) = parse(CStr::from_ptr(*environ).to_bytes()) {
85
- guard .insert(key, value);
85
+ map .insert(key, value);
86
86
}
87
87
environ = environ.add(1);
88
88
}
@@ -154,30 +154,26 @@ impl Iterator for Env {
154
154
/// Returns a vector of (variable, value) byte-vector pairs for all the
155
155
/// environment variables of the current process.
156
156
pub fn env() -> Env {
157
- unsafe {
158
- let guard = ENV.as_ref().unwrap().lock().unwrap();
159
- let mut result = Vec::new();
157
+ let guard = ENV.lock().unwrap();
158
+ let env = guard.as_ref().unwrap();
160
159
161
- for (key, value) in guard.iter() {
162
- result.push((key.clone(), value.clone()));
163
- }
160
+ let result = env.iter().map(|(key, value)| (key.clone(), value.clone())).collect::<Vec<_>>();
164
161
165
- return Env { iter: result.into_iter() };
166
- }
162
+ Env { iter: result.into_iter() }
167
163
}
168
164
169
165
pub fn getenv(k: &OsStr) -> Option<OsString> {
170
- unsafe { ENV.as_ref ().unwrap().lock ().unwrap().get_mut (k).cloned() }
166
+ ENV.lock ().unwrap().as_ref ().unwrap().get (k).cloned()
171
167
}
172
168
173
169
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
174
170
let (k, v) = (k.to_owned(), v.to_owned());
175
- ENV.as_ref ().unwrap().lock ().unwrap().insert(k, v);
171
+ ENV.lock ().unwrap().as_mut ().unwrap().insert(k, v);
176
172
Ok(())
177
173
}
178
174
179
175
pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
180
- ENV.as_ref ().unwrap().lock ().unwrap().remove(k);
176
+ ENV.lock ().unwrap().as_mut ().unwrap().remove(k);
181
177
Ok(())
182
178
}
183
179
0 commit comments