From 63cfcbe7a7745b276de58ec92e0141b958c44feb Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 10 Aug 2024 19:06:46 +0200 Subject: use unsafe blocks instead of mutexes --- src/sheet/loader.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'src/sheet/loader.rs') 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 = Mutex::new(Loader::new()); -} - #[derive(Default)] pub struct Loader { loaders: HashMap>>, @@ -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 = 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(()) }); } -- cgit v1.2.3-70-g09d2