Skip to content

Commit 1da0579

Browse files
committed
refactor(pal/hermit): make ENV a non-mutable static
Signed-off-by: Martin Kröning <[email protected]>
1 parent 127b469 commit 1da0579

File tree

1 file changed

+18
-19
lines changed
  • library/std/src/sys/pal/hermit

1 file changed

+18
-19
lines changed

library/std/src/sys/pal/hermit/os.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,21 @@ pub fn current_exe() -> io::Result<PathBuf> {
6868
unsupported()
6969
}
7070

71-
static mut ENV: Option<Mutex<HashMap<OsString, OsString>>> = None;
71+
static ENV: Mutex<Option<HashMap<OsString, OsString>>> = Mutex::new(None);
7272

7373
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());
7676

77-
if env.is_null() {
78-
return;
79-
}
77+
if env.is_null() {
78+
return;
79+
}
8080

81-
let mut guard = ENV.as_ref().unwrap().lock().unwrap();
81+
unsafe {
8282
let mut environ = env;
8383
while !(*environ).is_null() {
8484
if let Some((key, value)) = parse(CStr::from_ptr(*environ).to_bytes()) {
85-
guard.insert(key, value);
85+
map.insert(key, value);
8686
}
8787
environ = environ.add(1);
8888
}
@@ -154,30 +154,29 @@ impl Iterator for Env {
154154
/// Returns a vector of (variable, value) byte-vector pairs for all the
155155
/// environment variables of the current process.
156156
pub fn env() -> Env {
157-
unsafe {
158-
let guard = ENV.as_ref().unwrap().lock().unwrap();
159-
let mut result = Vec::new();
160-
161-
for (key, value) in guard.iter() {
162-
result.push((key.clone(), value.clone()));
163-
}
157+
let guard = ENV.lock().unwrap();
158+
let env = guard.as_ref().unwrap();
159+
let mut result = Vec::new();
164160

165-
return Env { iter: result.into_iter() };
161+
for (key, value) in env {
162+
result.push((key.clone(), value.clone()));
166163
}
164+
165+
return Env { iter: result.into_iter() };
167166
}
168167

169168
pub fn getenv(k: &OsStr) -> Option<OsString> {
170-
unsafe { ENV.as_ref().unwrap().lock().unwrap().get_mut(k).cloned() }
169+
ENV.lock().unwrap().as_mut().unwrap().get_mut(k).cloned()
171170
}
172171

173172
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
174173
let (k, v) = (k.to_owned(), v.to_owned());
175-
ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
174+
ENV.lock().unwrap().as_mut().unwrap().insert(k, v);
176175
Ok(())
177176
}
178177

179178
pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
180-
ENV.as_ref().unwrap().lock().unwrap().remove(k);
179+
ENV.lock().unwrap().as_mut().unwrap().remove(k);
181180
Ok(())
182181
}
183182

0 commit comments

Comments
 (0)