use std::sync::{Mutex, MutexGuard};
static ENV_LOCK: Mutex<()> = Mutex::new(());
pub(crate) fn env_lock() -> MutexGuard<'static, ()> {
ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner())
}
pub(crate) struct EnvGuard {
saved: Vec<(&'static str, Option<std::ffi::OsString>)>,
}
impl EnvGuard {
pub(crate) fn capture(keys: &[&'static str]) -> Self {
let saved = keys.iter().map(|k| (*k, std::env::var_os(k))).collect();
Self { saved }
}
}
impl Drop for EnvGuard {
fn drop(&mut self) {
for (k, v) in self.saved.drain(..) {
match v {
Some(val) => std::env::set_var(k, val),
None => std::env::remove_var(k),
}
}
}
}