From dbdaa9b433633b7240ec71b52fe8fd66fa7b6528 Mon Sep 17 00:00:00 2001 From: Blaine Date: Mon, 7 Jun 2021 02:31:18 +0530 Subject: [PATCH] Use oncecell --- Cargo.toml | 1 + src/main.rs | 6 +++--- src/scripts.rs | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cf6fc34..c9fd999 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ rust-embed = "5.9.0" serde_json = "1.0.56" serde = { version = "1.0.114", features = ["derive"] } glob = "0.3.0" +once_cell = "1.7.2" [profile.release] panic = "abort" diff --git a/src/main.rs b/src/main.rs index 852c47f..a9ff308 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,9 +39,9 @@ fn exec( fn main() { let context = tauri::generate_context!(); - unsafe { - scripts::PACKAGE_INFO = Some(context.package_info().clone()); - }; + scripts::PACKAGE_INFO + .set(context.package_info().clone()) + .unwrap(); tauri::Builder::default() .manage(Scripts(Default::default())) .invoke_handler(tauri::generate_handler![doc_ready, exec]) diff --git a/src/scripts.rs b/src/scripts.rs index aeddfea..a1b5906 100644 --- a/src/scripts.rs +++ b/src/scripts.rs @@ -8,7 +8,8 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; -pub static mut PACKAGE_INFO: Option = None; +use once_cell::sync::OnceCell; +pub static PACKAGE_INFO: OnceCell = OnceCell::new(); #[derive(Serialize, Deserialize)] struct Metadata { @@ -66,7 +67,7 @@ pub fn build_scripts( window: Window, script_list: &mut HashMap, ) -> tauri::Result<()> { - if let Some(resource_dir) = unsafe { resource_dir(&(PACKAGE_INFO.clone()).unwrap()) } { + if let Some(resource_dir) = resource_dir(&PACKAGE_INFO.get().unwrap()) { dbg!(&resource_dir); let script_path = &resource_dir.join("scripts").join("**").join("*.js"); install_scripts(&window, script_list, script_path.to_str().unwrap())?;