microsd 0.2.0

Light‐weight systemd auxiliars
Documentation
use std::env;
use std::ffi::OsStr;
use std::sync::RwLock;

static ENV: RwLock<()> = RwLock::new(());

pub fn no_env<F>(func: F)
where F: FnOnce() -> () {
	let _guard = ENV.read().unwrap();
	func();
}

pub fn with_env<F, I, K, V>(iter: I, func: F)
where
	F: FnOnce() -> (),
	I: IntoIterator<Item = (K, V)> + Clone,
	K: AsRef<OsStr>,
	V: AsRef<OsStr> {
	let _guard = ENV.write().unwrap();
	for (k, v) in iter.clone() {
		unsafe { env::set_var(k, v); }
	}

	unsafe { crate::process_env(); }
	func();

	for (k, _) in iter {
		unsafe { env::remove_var(k); }
	}
}