use wasmer_types::{ ExportType, ExternType, FunctionType, GlobalType, ImportType, MemoryType, TableType, }; // Code inspired from // https://www.reddit.com/r/rust/comments/9vspv4/extending_iterators_ergonomically/ /// This iterator allows us to iterate over the exports /// and offer nice API ergonomics over it. pub struct ExportsIterator + Sized> { pub(crate) iter: I, pub(crate) size: usize, } impl + Sized> ExactSizeIterator for ExportsIterator { // We can easily calculate the remaining number of iterations. fn len(&self) -> usize { self.size } } impl + Sized> ExportsIterator { /// Get only the functions pub fn functions(self) -> impl Iterator> + Sized { self.iter.filter_map(|extern_| match extern_.ty() { ExternType::Function(ty) => Some(ExportType::new(extern_.name(), ty.clone())), _ => None, }) } /// Get only the memories pub fn memories(self) -> impl Iterator> + Sized { self.iter.filter_map(|extern_| match extern_.ty() { ExternType::Memory(ty) => Some(ExportType::new(extern_.name(), *ty)), _ => None, }) } /// Get only the tables pub fn tables(self) -> impl Iterator> + Sized { self.iter.filter_map(|extern_| match extern_.ty() { ExternType::Table(ty) => Some(ExportType::new(extern_.name(), *ty)), _ => None, }) } /// Get only the globals pub fn globals(self) -> impl Iterator> + Sized { self.iter.filter_map(|extern_| match extern_.ty() { ExternType::Global(ty) => Some(ExportType::new(extern_.name(), *ty)), _ => None, }) } } impl + Sized> Iterator for ExportsIterator { type Item = ExportType; fn next(&mut self) -> Option { self.iter.next() } } /// This iterator allows us to iterate over the imports /// and offer nice API ergonomics over it. pub struct ImportsIterator + Sized> { pub(crate) iter: I, pub(crate) size: usize, } impl + Sized> ExactSizeIterator for ImportsIterator { // We can easily calculate the remaining number of iterations. fn len(&self) -> usize { self.size } } impl + Sized> ImportsIterator { /// Get only the functions pub fn functions(self) -> impl Iterator> + Sized { self.iter.filter_map(|extern_| match extern_.ty() { ExternType::Function(ty) => Some(ImportType::new( extern_.module(), extern_.name(), ty.clone(), )), _ => None, }) } /// Get only the memories pub fn memories(self) -> impl Iterator> + Sized { self.iter.filter_map(|extern_| match extern_.ty() { ExternType::Memory(ty) => Some(ImportType::new(extern_.module(), extern_.name(), *ty)), _ => None, }) } /// Get only the tables pub fn tables(self) -> impl Iterator> + Sized { self.iter.filter_map(|extern_| match extern_.ty() { ExternType::Table(ty) => Some(ImportType::new(extern_.module(), extern_.name(), *ty)), _ => None, }) } /// Get only the globals pub fn globals(self) -> impl Iterator> + Sized { self.iter.filter_map(|extern_| match extern_.ty() { ExternType::Global(ty) => Some(ImportType::new(extern_.module(), extern_.name(), *ty)), _ => None, }) } } impl + Sized> Iterator for ImportsIterator { type Item = ImportType; fn next(&mut self) -> Option { self.iter.next() } }