From 73b7b50c430b5887d353ff3907cf8ddc37bf4aa6 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sat, 5 Oct 2024 15:22:52 -0700 Subject: [PATCH] multiple unsafe but it compiles --- src/runner/instance.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/runner/instance.rs b/src/runner/instance.rs index c336cba..9b44206 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -264,18 +264,18 @@ impl mlua::UserData for Instance{ //find a function with a matching name if let Some(ret)=class_functions_mut(lua,|cf|{ let class_str=instance.class.as_str(); - Ok(match CLASS_FUNCTION_DATABASE.get_entry(class_str){ + let f=match CLASS_FUNCTION_DATABASE.get_entry(class_str){ Some((&static_class_str,class_functions))=>{ match cf.classes.entry(static_class_str){ - Entry::Occupied(occupied_entry)=>{ + Entry::Occupied(mut occupied_entry)=>{ match class_functions.get_entry(index_str){ Some((&static_index_str,function_pointer))=>{ - match occupied_entry.get().entry(static_index_str){ + match occupied_entry.get_mut().entry(static_index_str){ Entry::Occupied(occupied_entry)=>{ - Some(*occupied_entry.get()) + Some(occupied_entry.get().clone()) }, Entry::Vacant(vacant_entry)=>{ - Some(*vacant_entry.insert(lua.create_function(function_pointer)?)) + Some(vacant_entry.insert(unsafe{core::mem::transmute(lua.create_function(function_pointer)?)}).clone()) }, } }, @@ -286,8 +286,8 @@ impl mlua::UserData for Instance{ match class_functions.get_entry(index_str){ 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) + h.entry(static_index_str).or_insert(unsafe{core::mem::transmute(lua.create_function(function_pointer)?)}); + vacant_entry.insert(h).get(static_index_str).map(|f|f.clone()) }, None=>None, } @@ -295,7 +295,11 @@ impl mlua::UserData for Instance{ } }, None=>None, - }) + }; + Ok(f.map(|f|{ + let f_static:mlua::Function::<'static>=unsafe{core::mem::transmute(f)}; + f_static + })) })?{ return Ok(ret.into_lua(lua)); }