diff --git a/proxmox-product-config/src/init.rs b/proxmox-product-config/src/init.rs index a244559a..1bc47110 100644 --- a/proxmox-product-config/src/init.rs +++ b/proxmox-product-config/src/init.rs @@ -1,18 +1,21 @@ +use std::sync::OnceLock; + +#[derive(Debug)] struct ProxmoxProductConfig { api_user: nix::unistd::User, priv_user: nix::unistd::User, } -static mut PRODUCT_CONFIG: Option = None; +static PRODUCT_CONFIG: OnceLock = OnceLock::new(); /// Initialize the global product configuration. pub fn init(api_user: nix::unistd::User, priv_user: nix::unistd::User) { - unsafe { - PRODUCT_CONFIG = Some(ProxmoxProductConfig { + PRODUCT_CONFIG + .set(ProxmoxProductConfig { api_user, priv_user, - }); - } + }) + .expect("cannot init proxmox product config twice"); } /// Returns the global api user set with [init]. @@ -21,12 +24,10 @@ pub fn init(api_user: nix::unistd::User, priv_user: nix::unistd::User) { /// /// Panics if [init] wasn't called before. pub fn get_api_user() -> &'static nix::unistd::User { - unsafe { - &PRODUCT_CONFIG - .as_ref() - .expect("ProxmoxProductConfig is not initialized!") - .api_user - } + &PRODUCT_CONFIG + .get() + .expect("ProxmoxProductConfig is not initialized!") + .api_user } // Returns the global privileged user set with [init]. @@ -35,10 +36,8 @@ pub fn get_api_user() -> &'static nix::unistd::User { /// /// Panics if [init] wasn't called before. pub fn get_priv_user() -> &'static nix::unistd::User { - unsafe { - &PRODUCT_CONFIG - .as_ref() - .expect("ProxmoxProductConfig is not initialized!") - .priv_user - } + &PRODUCT_CONFIG + .get() + .expect("ProxmoxProductConfig is not initialized!") + .priv_user }