diff --git a/src/module/exports.rs b/src/module/exports.rs index 45e4e8e6..dfbf8997 100644 --- a/src/module/exports.rs +++ b/src/module/exports.rs @@ -103,11 +103,11 @@ impl ModuleExports { } /// Retrieve an exported function by name - pub fn get_func_by_name(&self, name: impl AsRef) -> Result<&Export> { + pub fn get_func_by_name(&self, name: impl AsRef) -> Result { self.iter() - .find(|expt| match expt.item { - ExportItem::Function(_) => expt.name == name.as_ref(), - _ => false, + .find_map(|expt| match expt.item { + ExportItem::Function(fid) if expt.name == name.as_ref() => Some(fid), + _ => None, }) .with_context(|| format!("unable to find function export '{}'", name.as_ref())) } @@ -135,11 +135,6 @@ impl ModuleExports { _ => false, }) } - - /// Check whether exports include the given function by ID - pub fn contains_func_by_id(&self, fid: FunctionId) -> bool { - self.get_exported_func(fid).is_some() - } } impl Module { diff --git a/src/module/functions/mod.rs b/src/module/functions/mod.rs index 596bc4e5..d6cd2068 100644 --- a/src/module/functions/mod.rs +++ b/src/module/functions/mod.rs @@ -455,7 +455,7 @@ impl Module { F: FnOnce((&FuncParams, &FuncResults)) -> Result>, { // If the function is in the imports, replace it - if let Some(original_imported_fn) = self.imports.get_func_by_id(fid) { + if let Some(original_imported_fn) = self.imports.get_imported_func(fid) { // Change types of existing function if let Function { kind: FunctionKind::Import(ImportedFunction { ty: tid, .. }), diff --git a/src/module/imports.rs b/src/module/imports.rs index 09f1a663..2126216b 100644 --- a/src/module/imports.rs +++ b/src/module/imports.rs @@ -110,29 +110,26 @@ impl ModuleImports { &self, module: impl AsRef, name: impl AsRef, - ) -> Result<&Import> { + ) -> Result { self.iter() - .find(|impt| match impt.kind { - ImportKind::Function(_) => { - impt.module == module.as_ref() && impt.name == name.as_ref() + .find_map(|impt| match impt.kind { + ImportKind::Function(fid) + if impt.module == module.as_ref() && impt.name == name.as_ref() => + { + Some(fid) } - _ => false, + _ => None, }) .with_context(|| format!("unable to find function export '{}'", name.as_ref())) } /// Retrieve an imported function by ID - pub fn get_func_by_id(&self, id: FunctionId) -> Option<&Import> { + pub fn get_imported_func(&self, id: FunctionId) -> Option<&Import> { self.arena.iter().find_map(|(_, import)| match import.kind { ImportKind::Function(fid) if fid == id => Some(import), _ => None, }) } - - /// Check whether imports include a function with the given ID - pub fn contains_function_by_id(&self, fid: FunctionId) -> bool { - self.get_func_by_id(fid).is_some() - } } impl Module {