Acquire the global env-var lock and atomically set one or more environment variables. All variables are restored to their prior state (or removed) when the guard is dropped.
(pairs: &[(&'static str, &str)])
| 36 | /// environment variables. All variables are restored to their prior |
| 37 | /// state (or removed) when the guard is dropped. |
| 38 | pub fn set(pairs: &[(&'static str, &str)]) -> Self { |
| 39 | let lock = ENV_LOCK |
| 40 | .lock() |
| 41 | .unwrap_or_else(std::sync::PoisonError::into_inner); |
| 42 | let mut vars = Vec::with_capacity(pairs.len()); |
| 43 | for &(key, value) in pairs { |
| 44 | let original = std::env::var(key).ok(); |
| 45 | unsafe { |
| 46 | std::env::set_var(key, value); |
| 47 | } |
| 48 | vars.push(SavedVar { key, original }); |
| 49 | } |
| 50 | Self { vars, _lock: lock } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[allow(unsafe_code)] |