From 5e1d02fa10eeab08c4a1af69ed8369a123919b8a Mon Sep 17 00:00:00 2001 From: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> Date: Tue, 5 Nov 2024 04:01:53 +0900 Subject: [PATCH] feat: enable iterating through module functions (#220) This commit makes a `Fucntions::iter` available, to enable iterating through functions present in a module. To keep changes to the public interface small, `Functions<'a>` now has a `iter()` function that returns an opaque iterator. Signed-off-by: Victor Adossi --- src/ir/component.rs | 2 +- src/ir/module/mod.rs | 6 +++++- src/ir/module/module_functions.rs | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ir/component.rs b/src/ir/component.rs index 94f7bb2..5b5fa69 100644 --- a/src/ir/component.rs +++ b/src/ir/component.rs @@ -7,7 +7,7 @@ use crate::ir::helpers::{ print_core_type, }; use crate::ir::id::{CustomSectionID, FunctionID, GlobalID, ModuleID}; -use crate::ir::module::{Iter, Module}; +use crate::ir::module::Module; use crate::ir::section::ComponentSection; use crate::ir::wrappers::{ add_to_namemap, convert_component_type, convert_instance_type, convert_module_type_declaration, diff --git a/src/ir/module/mod.rs b/src/ir/module/mod.rs index eb41fe6..fe6847e 100644 --- a/src/ir/module/mod.rs +++ b/src/ir/module/mod.rs @@ -890,7 +890,7 @@ impl<'a> Module<'a> { &mut self.functions, ) } else { - Self::get_mapping_generic(self.functions.iter()) + Self::get_mapping_generic(Iter::>::iter(&self.functions)) }; let global_mapping = if self.globals.recalculate_ids { Self::recalculate_ids( @@ -1572,8 +1572,12 @@ pub trait GetID { fn get_id(&self) -> u32; } +/// Facilitates iteration on types that hold `T` pub(crate) trait Iter { + /// Iterate over references of `T` fn iter(&self) -> std::slice::Iter<'_, T>; + + /// Clone and build an iterator fn get_into_iter(&self) -> IntoIter; } diff --git a/src/ir/module/module_functions.rs b/src/ir/module/module_functions.rs index 0f89532..bc6fe9b 100644 --- a/src/ir/module/module_functions.rs +++ b/src/ir/module/module_functions.rs @@ -235,6 +235,16 @@ pub struct Functions<'a> { pub(crate) recalculate_ids: bool, } +impl<'a> Functions<'a> { + /// Iterate over functions present in the module + /// + /// Note: Functions returned by this iterator *may* be deleted. + #[must_use] + pub fn iter(&self) -> impl Iterator> { + Iter::>::iter(self) + } +} + impl<'a> Iter> for Functions<'a> { /// Get an iterator for the functions. fn iter(&self) -> std::slice::Iter<'_, Function<'a>> {