use HashMap for functions in get_interface (#7)

This commit is contained in:
vms 2020-06-30 19:23:43 +03:00 committed by GitHub
parent 46ef013f7e
commit 80afea2094
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 16 deletions

View File

@ -22,10 +22,10 @@ use std::collections::HashMap;
/// Represent a function type inside FCE. /// Represent a function type inside FCE.
#[derive(Debug, serde::Serialize)] #[derive(Debug, serde::Serialize)]
pub struct FCEFunction<'a> { pub struct FCEFunctionSignature<'a> {
pub name: &'a str, pub name: &'a str,
pub inputs: &'a Vec<IType>, pub input_types: &'a Vec<IType>,
pub outputs: &'a Vec<IType>, pub output_types: &'a Vec<IType>,
} }
/// The base struct of the Fluence Compute Engine. /// 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. /// Return function signatures of all loaded info FCE modules with their names.
pub fn interface(&self) -> impl Iterator<Item = (&str, Vec<FCEFunction<'_>>)> { pub fn interface(&self) -> impl Iterator<Item = (&str, Vec<FCEFunctionSignature<'_>>)> {
self.modules.iter().map(|(module_name, module)| { self.modules.iter().map(|(module_name, module)| {
( (
module_name.as_str(), module_name.as_str(),
@ -100,20 +100,20 @@ impl FCE {
pub fn module_interface<S: AsRef<str>>( pub fn module_interface<S: AsRef<str>>(
&self, &self,
module_name: S, module_name: S,
) -> Result<Vec<FCEFunction<'_>>, FCEError> { ) -> Result<Vec<FCEFunctionSignature<'_>>, FCEError> {
match self.modules.get(module_name.as_ref()) { match self.modules.get(module_name.as_ref()) {
Some(module) => Ok(Self::get_module_function_signatures(module)), Some(module) => Ok(Self::get_module_function_signatures(module)),
None => Err(FCEError::NoSuchModule), None => Err(FCEError::NoSuchModule),
} }
} }
fn get_module_function_signatures(module: &FCEModule) -> Vec<FCEFunction<'_>> { fn get_module_function_signatures(module: &FCEModule) -> Vec<FCEFunctionSignature<'_>> {
module module
.get_exports_signatures() .get_exports_signatures()
.map(|(name, inputs, outputs)| FCEFunction { .map(|(name, input_types, output_types)| FCEFunctionSignature {
name, name,
inputs, input_types,
outputs, output_types,
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }

View File

@ -35,7 +35,7 @@ mod misc;
pub use config::FCEModuleConfig; pub use config::FCEModuleConfig;
pub use engine::FCE; pub use engine::FCE;
pub use engine::FCEFunction; pub use engine::FCEFunctionSignature;
pub use errors::FCEError; pub use errors::FCEError;
pub use module::IValue; pub use module::IValue;
pub use module::IType; pub use module::IType;

View File

@ -28,6 +28,7 @@ use fce::FCEModuleConfig;
use std::convert::TryInto; use std::convert::TryInto;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use crate::faas_interface::FaaSFunctionSignature;
// TODO: remove and use mutex instead // TODO: remove and use mutex instead
unsafe impl Send for FluenceFaaS {} unsafe impl Send for FluenceFaaS {}
@ -92,7 +93,7 @@ impl FluenceFaaS {
if !path.is_dir() { if !path.is_dir() {
let module_name = path let module_name = path
.file_name() .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() .to_os_string()
.into_string() .into_string()
.map_err(|name| IOError(format!("invalid file name: {:?}", name)))?; .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. /// Return all export functions (name and signatures) of loaded on a startup modules.
pub fn get_interface(&self) -> FaaSInterface { 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 } FaaSInterface { modules }
} }

View File

@ -14,13 +14,21 @@
* limitations under the License. * limitations under the License.
*/ */
use super::IType;
use serde::Serialize;
use std::fmt; use std::fmt;
use std::collections::HashMap; use std::collections::HashMap;
use serde::Serialize;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct FaaSInterface<'a> { pub struct FaaSInterface<'a> {
pub modules: HashMap<&'a str, Vec<fce::FCEFunction<'a>>>, pub modules: HashMap<&'a str, HashMap<&'a str, FaaSFunctionSignature<'a>>>,
}
#[derive(Debug, Serialize)]
pub struct FaaSFunctionSignature<'a> {
pub input_types: &'a Vec<IType>,
pub output_types: &'a Vec<IType>,
} }
impl<'a> fmt::Display for FaaSInterface<'a> { 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() { for (name, functions) in self.modules.iter() {
writeln!(f, "{}", *name)?; writeln!(f, "{}", *name)?;
for function in functions.iter() { for (name, signature) in functions.iter() {
writeln!( writeln!(
f, f,
" pub fn {}({:?}) -> {:?}", " pub fn {}({:?}) -> {:?}",
function.name, function.inputs, function.outputs name, signature.input_types, signature.output_types
)?; )?;
} }
} }