Change SharedPluginFunction to Shared<FnPlugin>

This commit is contained in:
Stephen Chung
2020-09-29 13:06:48 +08:00
parent 64c421b3d7
commit ea78fa2b27
8 changed files with 77 additions and 80 deletions

View File

@@ -199,10 +199,10 @@ pub type FnAny =
/// A standard function that gets an iterator from a type.
pub type IteratorFn = fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
#[cfg(feature = "sync")]
pub type SharedPluginFunction = Arc<dyn PluginFunction + Send + Sync>;
#[cfg(not(feature = "sync"))]
pub type SharedPluginFunction = Rc<dyn PluginFunction>;
pub type FnPlugin = dyn PluginFunction;
#[cfg(feature = "sync")]
pub type FnPlugin = dyn PluginFunction + Send + Sync;
/// A standard callback function.
#[cfg(not(feature = "sync"))]
@@ -222,7 +222,7 @@ pub enum CallableFunction {
/// An iterator function.
Iterator(IteratorFn),
/// A plugin-defined function,
Plugin(SharedPluginFunction),
Plugin(Shared<FnPlugin>),
/// A script-defined function.
#[cfg(not(feature = "no_function"))]
Script(Shared<ScriptFnDef>),
@@ -389,9 +389,9 @@ impl CallableFunction {
/// # Panics
///
/// Panics if the `CallableFunction` is not `Plugin`.
pub fn get_plugin_fn<'s>(&'s self) -> SharedPluginFunction {
pub fn get_plugin_fn<'s>(&'s self) -> &FnPlugin {
match self {
Self::Plugin(f) => f.clone(),
Self::Plugin(f) => f.as_ref(),
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => unreachable!(),
#[cfg(not(feature = "no_function"))]
@@ -406,17 +406,9 @@ impl CallableFunction {
pub fn from_method(func: Box<FnAny>) -> Self {
Self::Method(func.into())
}
#[cfg(feature = "sync")]
/// Create a new `CallableFunction::Plugin`.
pub fn from_plugin(plugin: impl PluginFunction + 'static + Send + Sync) -> Self {
Self::Plugin(Arc::new(plugin))
}
#[cfg(not(feature = "sync"))]
/// Create a new `CallableFunction::Plugin`.
pub fn from_plugin(plugin: impl PluginFunction + 'static) -> Self {
Self::Plugin(Rc::new(plugin))
pub fn from_plugin(func: impl PluginFunction + 'static + SendSync) -> Self {
Self::Plugin((Box::new(func) as Box<FnPlugin>).into())
}
}
@@ -445,3 +437,15 @@ impl From<Shared<ScriptFnDef>> for CallableFunction {
Self::Script(_func)
}
}
impl<T: PluginFunction + 'static + SendSync> From<T> for CallableFunction {
fn from(func: T) -> Self {
Self::from_plugin(func)
}
}
impl From<Shared<FnPlugin>> for CallableFunction {
fn from(func: Shared<FnPlugin>) -> Self {
Self::Plugin(func.into())
}
}

View File

@@ -90,7 +90,7 @@ pub trait RegisterPlugin<PL: crate::plugin::Plugin> {
/// std::any::TypeId::of::<NUMBER>(),
/// std::any::TypeId::of::<NUMBER>(),
/// std::any::TypeId::of::<NUMBER>()],
/// CallableFunction::from_plugin(DistanceFunction()));
/// DistanceFunction().into());
/// let mut r = StaticModuleResolver::new();
/// r.insert("Math::Advanced".to_string(), m);
/// engine.set_module_resolver(Some(r));

View File

@@ -17,11 +17,6 @@ use crate::stdlib::{
string::{String, ToString},
};
#[cfg(not(feature = "sync"))]
use crate::stdlib::rc::Rc;
#[cfg(feature = "sync")]
use crate::stdlib::sync::Arc;
/// A general expression evaluation trait object.
#[cfg(not(feature = "sync"))]
pub type FnCustomSyntaxEval = dyn Fn(
@@ -175,10 +170,7 @@ impl Engine {
let syntax = CustomSyntax {
segments,
#[cfg(not(feature = "sync"))]
func: Rc::new(func),
#[cfg(feature = "sync")]
func: Arc::new(func),
func: (Box::new(func) as Box<FnCustomSyntaxEval>).into(),
scope_delta,
};

View File

@@ -61,7 +61,8 @@ impl BuildHasher for StraightHasherBuilder {
}
}
/// Calculate a `u64` hash key from a module-qualified function name and parameter types.
/// [INTERNALS] Calculate a `u64` hash key from a module-qualified function name and parameter types.
/// Exported under the `internals` feature only.
///
/// Module names are passed in via `&str` references from an iterator.
/// Parameter types are passed in via `TypeId` values from an iterator.