Skip to content

Commit

Permalink
Use ExactSizeIterator to avoid needing separate num_* methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Apr 16, 2020
1 parent 840fbbb commit 28454a4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
6 changes: 3 additions & 3 deletions crates/api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ impl Instance {
}
}

if imports.len() != module.num_imports() {
if imports.len() != module.imports().len() {
bail!(
"wrong number of imports provided, {} != {}",
imports.len(),
module.num_imports()
module.imports().len()
);
}

Expand Down Expand Up @@ -164,7 +164,7 @@ impl Instance {
/// information about the export itself. The list returned here maps 1:1 with
/// the list that [`Module::exports`] returns, and [`ExportType`](crate::ExportType)
/// contains the name of each export.
pub fn exports<'me>(&'me self) -> impl Iterator<Item = Export> + 'me {
pub fn exports<'me>(&'me self) -> impl ExactSizeIterator<Item = Export> + 'me {
let instance_handle = &self.instance_handle;
let store = self.module.store();
self.instance_handle
Expand Down
20 changes: 5 additions & 15 deletions crates/api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl Module {
/// # fn main() -> anyhow::Result<()> {
/// # let store = Store::default();
/// let module = Module::new(&store, "(module)")?;
/// assert_eq!(module.num_imports(), 0);
/// assert_eq!(module.imports().len(), 0);
/// # Ok(())
/// # }
/// ```
Expand All @@ -393,7 +393,7 @@ impl Module {
/// )
/// "#;
/// let module = Module::new(&store, wat)?;
/// assert_eq!(module.num_imports(), 1);
/// assert_eq!(module.imports().len(), 1);
/// let import = module.imports().next().unwrap();
/// assert_eq!(import.module, "host");
/// assert_eq!(import.name, "foo");
Expand All @@ -404,7 +404,7 @@ impl Module {
/// # Ok(())
/// # }
/// ```
pub fn imports<'me>(&'me self) -> impl Iterator<Item = ImportType> + 'me {
pub fn imports<'me>(&'me self) -> impl ExactSizeIterator<Item = ImportType> + 'me {
let module = self.inner.compiled.module_ref();
module
.imports
Expand All @@ -419,11 +419,6 @@ impl Module {
})
}

/// Return the number of imports in this module.
pub fn num_imports(&self) -> usize {
self.inner.compiled.module_ref().imports.len()
}

/// Returns the list of exports that this [`Module`] has and will be
/// available after instantiation.
///
Expand Down Expand Up @@ -459,7 +454,7 @@ impl Module {
/// )
/// "#;
/// let module = Module::new(&store, wat)?;
/// assert_eq!(module.num_exports(), 2);
/// assert_eq!(module.exports().len(), 2);
///
/// let foo = module.exports().next().unwrap();
/// assert_eq!(foo.name, "foo");
Expand All @@ -477,7 +472,7 @@ impl Module {
/// # Ok(())
/// # }
/// ```
pub fn exports<'me>(&'me self) -> impl Iterator<Item = ExportType> + 'me {
pub fn exports<'me>(&'me self) -> impl ExactSizeIterator<Item = ExportType> + 'me {
let module = self.inner.compiled.module_ref();
module.exports.iter().map(move |(name, entity_index)| {
let r#type = entity_type(entity_index, module);
Expand All @@ -488,11 +483,6 @@ impl Module {
})
}

/// Return the number of exports in this module.
pub fn num_exports(&self) -> usize {
self.inner.compiled.module_ref().exports.len()
}

/// Returns the [`Store`] that this [`Module`] was compiled into.
pub fn store(&self) -> &Store {
&self.inner.store
Expand Down

0 comments on commit 28454a4

Please sign in to comment.