diff options
Diffstat (limited to 'src/sheet/loader.rs')
| -rw-r--r-- | src/sheet/loader.rs | 19 |
1 files changed, 6 insertions, 13 deletions
diff --git a/src/sheet/loader.rs b/src/sheet/loader.rs index 613d2a5..35b6da2 100644 --- a/src/sheet/loader.rs +++ b/src/sheet/loader.rs @@ -1,20 +1,12 @@ -use std::{ - collections::HashMap, - path::Path, - sync::{Mutex, MutexGuard}, -}; +use std::{collections::HashMap, path::Path, ptr::addr_of_mut}; -use lazy_static::lazy_static; use mlua::{Lua, UserData}; +use once_cell::sync::Lazy; use crate::lua::{ownedfunction::OwnedFunction, runnable::Runnable}; use super::{register::SheetId, tablized::Tablized}; -lazy_static! { - static ref GLOBAL_LOADER: Mutex<Loader> = Mutex::new(Loader::new()); -} - #[derive(Default)] pub struct Loader { loaders: HashMap<String, Box<dyn Runnable<String, Tablized>>>, @@ -40,15 +32,16 @@ impl Loader { self.loaders.insert(extension, func); } - pub fn get() -> MutexGuard<'static, Loader> { - GLOBAL_LOADER.lock().unwrap() + pub fn get() -> &'static mut Self { + static mut GLOBAL_LOADER: Lazy<Loader> = Lazy::new(|| Loader::new()); + unsafe { &mut *addr_of_mut!(GLOBAL_LOADER) } } } impl UserData for Loader { fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(methods: &mut M) { methods.add_function("add", |_, (extension, func): (String, OwnedFunction)| { - GLOBAL_LOADER.lock().unwrap().add(extension, Box::new(func)); + Loader::get().add(extension, Box::new(func)); Ok(()) }); } |