From 80afea2094bf3af8fb50ba4c03aa2d6a3546915f Mon Sep 17 00:00:00 2001 From: vms Date: Tue, 30 Jun 2020 19:23:43 +0300 Subject: [PATCH] use HashMap for functions in get_interface (#7) --- engine/src/engine.rs | 18 +++++++++--------- engine/src/lib.rs | 2 +- fluence-faas/src/faas.rs | 23 +++++++++++++++++++++-- fluence-faas/src/faas_interface.rs | 16 ++++++++++++---- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/engine/src/engine.rs b/engine/src/engine.rs index b0f11d05..b1986e87 100644 --- a/engine/src/engine.rs +++ b/engine/src/engine.rs @@ -22,10 +22,10 @@ use std::collections::HashMap; /// Represent a function type inside FCE. #[derive(Debug, serde::Serialize)] -pub struct FCEFunction<'a> { +pub struct FCEFunctionSignature<'a> { pub name: &'a str, - pub inputs: &'a Vec, - pub outputs: &'a Vec, + pub input_types: &'a Vec, + pub output_types: &'a Vec, } /// The base struct of the Fluence Compute Engine. @@ -87,7 +87,7 @@ impl FCE { } /// Return function signatures of all loaded info FCE modules with their names. - pub fn interface(&self) -> impl Iterator>)> { + pub fn interface(&self) -> impl Iterator>)> { self.modules.iter().map(|(module_name, module)| { ( module_name.as_str(), @@ -100,20 +100,20 @@ impl FCE { pub fn module_interface>( &self, module_name: S, - ) -> Result>, FCEError> { + ) -> Result>, FCEError> { match self.modules.get(module_name.as_ref()) { Some(module) => Ok(Self::get_module_function_signatures(module)), None => Err(FCEError::NoSuchModule), } } - fn get_module_function_signatures(module: &FCEModule) -> Vec> { + fn get_module_function_signatures(module: &FCEModule) -> Vec> { module .get_exports_signatures() - .map(|(name, inputs, outputs)| FCEFunction { + .map(|(name, input_types, output_types)| FCEFunctionSignature { name, - inputs, - outputs, + input_types, + output_types, }) .collect::>() } diff --git a/engine/src/lib.rs b/engine/src/lib.rs index 3f843395..b65afd77 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -35,7 +35,7 @@ mod misc; pub use config::FCEModuleConfig; pub use engine::FCE; -pub use engine::FCEFunction; +pub use engine::FCEFunctionSignature; pub use errors::FCEError; pub use module::IValue; pub use module::IType; diff --git a/fluence-faas/src/faas.rs b/fluence-faas/src/faas.rs index f45d415f..a3d6de08 100644 --- a/fluence-faas/src/faas.rs +++ b/fluence-faas/src/faas.rs @@ -28,6 +28,7 @@ use fce::FCEModuleConfig; use std::convert::TryInto; use std::fs; use std::path::PathBuf; +use crate::faas_interface::FaaSFunctionSignature; // TODO: remove and use mutex instead unsafe impl Send for FluenceFaaS {} @@ -92,7 +93,7 @@ impl FluenceFaaS { if !path.is_dir() { let module_name = path .file_name() - .ok_or(IOError(format!("No file name in path {:?}", path)))? + .ok_or_else(|| IOError(format!("No file name in path {:?}", path)))? .to_os_string() .into_string() .map_err(|name| IOError(format!("invalid file name: {:?}", name)))?; @@ -137,7 +138,25 @@ impl FluenceFaaS { /// Return all export functions (name and signatures) of loaded on a startup modules. pub fn get_interface(&self) -> FaaSInterface { - let modules = self.fce.interface().collect(); + let modules = self + .fce + .interface() + .map(|(name, signatures)| { + let signatures = signatures + .iter() + .map(|f| { + ( + f.name, + FaaSFunctionSignature { + input_types: f.input_types, + output_types: f.output_types, + }, + ) + }) + .collect(); + (name, signatures) + }) + .collect(); FaaSInterface { modules } } diff --git a/fluence-faas/src/faas_interface.rs b/fluence-faas/src/faas_interface.rs index 7bddf8d4..e128ad81 100644 --- a/fluence-faas/src/faas_interface.rs +++ b/fluence-faas/src/faas_interface.rs @@ -14,13 +14,21 @@ * limitations under the License. */ +use super::IType; +use serde::Serialize; + use std::fmt; use std::collections::HashMap; -use serde::Serialize; #[derive(Debug, Serialize)] pub struct FaaSInterface<'a> { - pub modules: HashMap<&'a str, Vec>>, + pub modules: HashMap<&'a str, HashMap<&'a str, FaaSFunctionSignature<'a>>>, +} + +#[derive(Debug, Serialize)] +pub struct FaaSFunctionSignature<'a> { + pub input_types: &'a Vec, + pub output_types: &'a Vec, } impl<'a> fmt::Display for FaaSInterface<'a> { @@ -28,11 +36,11 @@ impl<'a> fmt::Display for FaaSInterface<'a> { for (name, functions) in self.modules.iter() { writeln!(f, "{}", *name)?; - for function in functions.iter() { + for (name, signature) in functions.iter() { writeln!( f, " pub fn {}({:?}) -> {:?}", - function.name, function.inputs, function.outputs + name, signature.input_types, signature.output_types )?; } }