Skip to content

Commit

Permalink
feat: enable iterating through module functions (#220)
Browse files Browse the repository at this point in the history
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 <vadossi@cosmonic.com>
  • Loading branch information
vados-cosmonic authored Nov 4, 2024
1 parent 782ad53 commit 5e1d02f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ir/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/ir/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ impl<'a> Module<'a> {
&mut self.functions,
)
} else {
Self::get_mapping_generic(self.functions.iter())
Self::get_mapping_generic(Iter::<Function<'a>>::iter(&self.functions))
};
let global_mapping = if self.globals.recalculate_ids {
Self::recalculate_ids(
Expand Down Expand Up @@ -1572,8 +1572,12 @@ pub trait GetID {
fn get_id(&self) -> u32;
}

/// Facilitates iteration on types that hold `T`
pub(crate) trait Iter<T> {
/// Iterate over references of `T`
fn iter(&self) -> std::slice::Iter<'_, T>;

/// Clone and build an iterator
fn get_into_iter(&self) -> IntoIter<T>;
}

Expand Down
10 changes: 10 additions & 0 deletions src/ir/module/module_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = &Function<'a>> {
Iter::<Function<'a>>::iter(self)
}
}

impl<'a> Iter<Function<'a>> for Functions<'a> {
/// Get an iterator for the functions.
fn iter(&self) -> std::slice::Iter<'_, Function<'a>> {
Expand Down

0 comments on commit 5e1d02f

Please sign in to comment.