From 28454a4efcd69797b08fddc5f728ed77631e0c91 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 16 Apr 2020 16:44:08 -0700 Subject: [PATCH] Use `ExactSizeIterator` to avoid needing separate `num_*` methods. --- crates/api/src/instance.rs | 6 +++--- crates/api/src/module.rs | 20 +++++--------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/crates/api/src/instance.rs b/crates/api/src/instance.rs index 7be541658cea..d45177745f96 100644 --- a/crates/api/src/instance.rs +++ b/crates/api/src/instance.rs @@ -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() ); } @@ -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 + 'me { + pub fn exports<'me>(&'me self) -> impl ExactSizeIterator + 'me { let instance_handle = &self.instance_handle; let store = self.module.store(); self.instance_handle diff --git a/crates/api/src/module.rs b/crates/api/src/module.rs index 0f55f0b4ee7f..b417f3cfcd16 100644 --- a/crates/api/src/module.rs +++ b/crates/api/src/module.rs @@ -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(()) /// # } /// ``` @@ -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"); @@ -404,7 +404,7 @@ impl Module { /// # Ok(()) /// # } /// ``` - pub fn imports<'me>(&'me self) -> impl Iterator + 'me { + pub fn imports<'me>(&'me self) -> impl ExactSizeIterator + 'me { let module = self.inner.compiled.module_ref(); module .imports @@ -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. /// @@ -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"); @@ -477,7 +472,7 @@ impl Module { /// # Ok(()) /// # } /// ``` - pub fn exports<'me>(&'me self) -> impl Iterator + 'me { + pub fn exports<'me>(&'me self) -> impl ExactSizeIterator + 'me { let module = self.inner.compiled.module_ref(); module.exports.iter().map(move |(name, entity_index)| { let r#type = entity_type(entity_index, module); @@ -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