From 0de31465a4ad88f8ff93d3caf10434fc56dd60fb Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sun, 6 Oct 2024 10:41:42 -0700 Subject: [PATCH] ClassFunction exceptional simplification --- src/runner/instance.rs | 50 ++++++++++++------------------------------ 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/src/runner/instance.rs b/src/runner/instance.rs index 2286f4c..36a5fe2 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -1,4 +1,4 @@ -use std::collections::{hash_map::Entry,HashMap}; +use std::collections::HashMap; use mlua::{FromLua,FromLuaMulti,IntoLua,IntoLuaMulti}; use rbx_types::Ref; @@ -384,43 +384,21 @@ struct ClassFunctions{ > } impl ClassFunctions{ - /// Someone please rewrite this, all it's supposed to do is /// return self.classes[class][index] or create the function in the hashmap and then return it fn get_or_create_class_function(&mut self,lua:&mlua::Lua,class:&str,index:&str)->mlua::Result>{ - // Use get_entry to get the &'static str key of the database + // Use get_entry to get the &'static str keys of the database // and use it as a key for the classes hashmap - let f=match CLASS_FUNCTION_DATABASE.get_entry(class){ - Some((&static_class_str,class_functions))=>{ - match self.classes.entry(static_class_str){ - Entry::Occupied(mut occupied_entry)=>{ - match class_functions.get_entry(index){ - Some((&static_index_str,function_pointer))=>{ - match occupied_entry.get_mut().entry(static_index_str){ - Entry::Occupied(occupied_entry)=>{ - Some(occupied_entry.get().clone()) - }, - Entry::Vacant(vacant_entry)=>{ - Some(vacant_entry.insert(lua.create_function(function_pointer)?).clone()) - }, - } - }, - None=>None, - } - }, - Entry::Vacant(vacant_entry)=>{ - match class_functions.get_entry(index){ - Some((&static_index_str,function_pointer))=>{ - let mut h=HashMap::new(); - h.entry(static_index_str).or_insert(lua.create_function(function_pointer)?); - vacant_entry.insert(h).get(static_index_str).map(|f|f.clone()) - }, - None=>None, - } - }, - } - }, - None=>None, - }; - Ok(f) + if let Some((&static_class_str,class_functions))=CLASS_FUNCTION_DATABASE.get_entry(class){ + if let Some((&static_index_str,function_pointer))=class_functions.get_entry(index){ + return Ok(Some( + self.classes.entry(static_class_str) + .or_insert(HashMap::new()) + .entry(static_index_str) + .or_insert(lua.create_function(function_pointer)?) + .clone() + )) + } + } + return Ok(None) } }