Fixed comments

- Would be better to remove the iterator and just collect with a loop to avoid extra allocations
 - tructure
 - A HashSet is probably better
 - Usefull -> Useful
 - I'd have thought plugin-api-derive is a better name
This commit is contained in:
ccgauche 2020-12-12 01:19:20 +01:00 committed by Joshua Barretto
parent 3a5386e047
commit c2bc112e44
8 changed files with 23 additions and 26 deletions

4
Cargo.lock generated
View File

@ -3754,7 +3754,7 @@ dependencies = [
] ]
[[package]] [[package]]
name = "plugin_proc" name = "plugin-api-derive"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2 1.0.24",
@ -5602,7 +5602,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bincode", "bincode",
"common-api", "common-api",
"plugin_proc", "plugin-api-derive",
"serde", "serde",
] ]

Binary file not shown.

View File

@ -4,12 +4,7 @@ pub mod module;
use crate::assets::ASSETS_PATH; use crate::assets::ASSETS_PATH;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{collections::{HashMap, HashSet}, fs, io::Read, path::{Path, PathBuf}};
collections::HashMap,
fs,
io::Read,
path::{Path, PathBuf},
};
use tracing::{error, info}; use tracing::{error, info};
use common_api::Event; use common_api::Event;
@ -19,8 +14,8 @@ use self::{ errors::PluginError, module::{PluginModule, PreparedEventQuery}};
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PluginData { pub struct PluginData {
name: String, name: String,
modules: Vec<PathBuf>, modules: HashSet<PathBuf>,
dependencies: Vec<String>, dependencies: HashSet<String>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -93,9 +88,12 @@ impl PluginMgr {
} }
pub fn execute_prepared<T>(&self, event_name: &str,event: &PreparedEventQuery<T>) -> Result<Vec<T::Response>, PluginError> where T: Event { pub fn execute_prepared<T>(&self, event_name: &str,event: &PreparedEventQuery<T>) -> Result<Vec<T::Response>, PluginError> where T: Event {
Ok(self.plugins.iter().map(|plugin| { let mut out = Vec::new();
plugin.execute_prepared(event_name, event) for plugin in &self.plugins {
}).collect::<Result<Vec<Vec<T::Response>>, _>>()?.into_iter().flatten().collect::<Vec<T::Response>>()) let exe = plugin.execute_prepared(event_name, event)?;
out.extend(exe);
}
Ok(out)
} }
pub fn execute_event<T>(&self, event_name: &str,event: &T) -> Result<Vec<T::Response>, PluginError> where T: Event { pub fn execute_event<T>(&self, event_name: &str,event: &T) -> Result<Vec<T::Response>, PluginError> where T: Event {

View File

@ -1,4 +1,4 @@
use std::{marker::PhantomData, sync::Arc}; use std::{collections::HashSet, marker::PhantomData, sync::Arc};
use error::RuntimeError; use error::RuntimeError;
use parking_lot::Mutex; use parking_lot::Mutex;
@ -11,15 +11,15 @@ use common_api::{Action, Event};
pub type Function<'a> = Func<'a, (i32, u32), i32>; pub type Function<'a> = Func<'a, (i32, u32), i32>;
#[derive(Clone)] #[derive(Clone)]
// This tructure represent the WASM State of the plugin. // This structure represent the WASM State of the plugin.
pub struct PluginModule { pub struct PluginModule {
wasm_instance: Arc<Mutex<Instance>>, wasm_instance: Arc<Mutex<Instance>>,
events: Vec<String>, events: HashSet<String>,
} }
impl PluginModule { impl PluginModule {
// This function take bytes from a WASM File and compile them // This function takes bytes from a WASM File and compile them
pub fn new(wasm_data: &Vec<u8>) -> Result<Self,PluginModuleError> { pub fn new(wasm_data: &Vec<u8>) -> Result<Self,PluginModuleError> {
let module = compile(&wasm_data).map_err(|e| PluginModuleError::Compile(e))?; let module = compile(&wasm_data).map_err(|e| PluginModuleError::Compile(e))?;
let instance = module let instance = module
@ -33,7 +33,7 @@ impl PluginModule {
}) })
} }
// This function try to execute an event for the current module will return None if the event doesn't exists // This function tries to execute an event for the current module. Will return None if the event doesn't exists
pub fn try_execute<T>( pub fn try_execute<T>(
&self, &self,
event_name: &str, event_name: &str,
@ -42,7 +42,7 @@ impl PluginModule {
where where
T: Event, T: Event,
{ {
if !self.events.iter().any(|x| x == event_name) { if !self.events.contains(event_name) {
return None; return None;
} }
let bytes = { let bytes = {
@ -61,14 +61,14 @@ impl PluginModule {
} }
} }
// This structure represent a Pre-encoded event object (Usefull to avoid reencoding for each module in every plugin) // This structure represent a Pre-encoded event object (Useful to avoid reencoding for each module in every plugin)
pub struct PreparedEventQuery<T> { pub struct PreparedEventQuery<T> {
bytes: Vec<u8>, bytes: Vec<u8>,
_phantom: PhantomData<T>, _phantom: PhantomData<T>,
} }
impl<T: Event> PreparedEventQuery<T> { impl<T: Event> PreparedEventQuery<T> {
// Create a prepared query from a event reference (Encode to bytes the struct) // Create a prepared query from an event reference (Encode to bytes the struct)
// This Prepared Query is used by the `try_execute` method in `PluginModule` // This Prepared Query is used by the `try_execute` method in `PluginModule`
pub fn new(event: &T) -> Result<Self, PluginError> pub fn new(event: &T) -> Result<Self, PluginError>
where where

View File

@ -6,7 +6,7 @@ edition = "2018"
[dependencies] [dependencies]
common-api = { path = "../common/common-api" } common-api = { path = "../common/common-api" }
plugin_proc = { path = "./plugin_proc"} plugin-api-derive = { path = "./plugin-api-derive"}
serde = {version = "1.0.118", features = ["derive"]} serde = {version = "1.0.118", features = ["derive"]}
bincode = "1.3.1" bincode = "1.3.1"

View File

@ -1,5 +1,5 @@
[package] [package]
name = "plugin_proc" name = "plugin-api-derive"
version = "0.1.0" version = "0.1.0"
authors = ["ccgauche <gaucheron.laurent@gmail.com>"] authors = ["ccgauche <gaucheron.laurent@gmail.com>"]
edition = "2018" edition = "2018"

View File

@ -1,13 +1,12 @@
#![feature(const_fn)] #![feature(const_fn)]
pub extern crate plugin_proc; pub extern crate plugin_api_derive;
pub extern crate common_api; pub extern crate common_api;
pub use common_api::*; pub use common_api::*;
pub use plugin_api_derive::*;
pub use plugin_proc::*;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;