split get_or_create_class_function into two parts in case you want to get multiple functions from the same class

This commit is contained in:
Quaternions 2024-10-06 11:41:52 -07:00
parent 5e45e952d9
commit 692697f82b

View File

@ -257,7 +257,10 @@ impl mlua::UserData for Instance{
descriptor:Some(class), descriptor:Some(class),
}; };
iter.find_map(|class| iter.find_map(|class|
cf.get_or_create_class_function(lua,&class.name,index_str).transpose() cf.get_or_create_class_methods(&class.name)
.and_then(|mut class_methods|
class_methods.get_or_create_function(lua,index_str).transpose()
)
).transpose() ).transpose()
})?{ })?{
return function.into_lua(lua); return function.into_lua(lua);
@ -378,20 +381,33 @@ struct ClassFunctions{
> >
} }
impl ClassFunctions{ impl ClassFunctions{
/// return self.classes[class][index] or create the function in the hashmap and then return it /// return self.classes[class] or create the ClassMethods and then return it
fn get_or_create_class_function(&mut self,lua:&mlua::Lua,class:&str,index:&str)->mlua::Result<Option<mlua::Function>>{ fn get_or_create_class_methods(&mut self,class:&str)->Option<ClassMethods>{
// Use get_entry to get the &'static str keys 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 // and use it as a key for the classes hashmap
if let Some((&static_class_str,class_functions))=CLASS_FUNCTION_DATABASE.get_entry(class){ CLASS_FUNCTION_DATABASE.get_entry(class)
if let Some((&static_index_str,function_pointer))=class_functions.get_entry(index){ .map(|(&static_class_str,method_pointers)|
return Ok(Some( ClassMethods{
self.classes.entry(static_class_str) method_pointers,
.or_insert_with(||HashMap::new()) methods:self.classes.entry(static_class_str)
.entry(static_index_str) .or_insert_with(||HashMap::new()),
.or_insert(lua.create_function(function_pointer)?) }
.clone() )
)) }
} }
struct ClassMethods<'a>{
method_pointers:&'static phf::Map<&'static str,ClassFunctionPointer>,
methods:&'a mut HashMap<&'static str,mlua::Function>,
}
impl ClassMethods<'_>{
/// return self.methods[index] or create the function in the hashmap and then return it
fn get_or_create_function(&mut self,lua:&mlua::Lua,index:&str)->mlua::Result<Option<mlua::Function>>{
if let Some((&static_index_str,function_pointer))=self.method_pointers.get_entry(index){
return Ok(Some(
self.methods.entry(static_index_str)
.or_insert(lua.create_function(function_pointer)?)
.clone()
))
} }
return Ok(None) return Ok(None)
} }