diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-10 19:06:46 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-10 19:06:46 +0200 |
| commit | 63cfcbe7a7745b276de58ec92e0141b958c44feb (patch) | |
| tree | 990e33a83756e27187033579ee2f85d5c79169d5 /src/sheet/loader.rs | |
| parent | b747ca8af52129876b577a4f20f7105a05c6b002 (diff) | |
use unsafe blocks instead of mutexes
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(()) }); } |