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

@ -580,7 +580,7 @@ impl ExportedFn {
);
quote! {
pub fn #callable_fn_name() -> CallableFunction {
CallableFunction::from_plugin(#token_name())
#token_name().into()
}
}
}

View File

@ -126,7 +126,7 @@ pub(crate) fn generate_body(
set_fn_stmts.push(
syn::parse2::<syn::Stmt>(quote! {
m.set_fn(#fn_literal, FnAccess::Public, &[#(#fn_input_types),*],
CallableFunction::from_plugin(#fn_token_name()));
#fn_token_name().into());
})
.unwrap(),
);

View File

@ -290,7 +290,7 @@ mod generate_tests {
}
}
pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token())
Token().into()
}
pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
@ -335,7 +335,7 @@ mod generate_tests {
}
}
pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token())
Token().into()
}
pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
@ -412,7 +412,7 @@ mod generate_tests {
}
}
pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token())
Token().into()
}
pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
@ -459,7 +459,7 @@ mod generate_tests {
}
}
pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token())
Token().into()
}
pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()
@ -505,7 +505,7 @@ mod generate_tests {
}
}
pub fn token_callable() -> CallableFunction {
CallableFunction::from_plugin(Token())
Token().into()
}
pub fn token_input_types() -> Box<[TypeId]> {
Token().input_types()

View File

@ -296,7 +296,7 @@ mod generate_tests {
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("get_mystic_number", FnAccess::Public, &[],
CallableFunction::from_plugin(get_mystic_number_token()));
get_mystic_number_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -320,7 +320,7 @@ mod generate_tests {
}
}
pub fn get_mystic_number_token_callable() -> CallableFunction {
CallableFunction::from_plugin(get_mystic_number_token())
get_mystic_number_token().into()
}
pub fn get_mystic_number_token_input_types() -> Box<[TypeId]> {
get_mystic_number_token().input_types()
@ -358,7 +358,7 @@ mod generate_tests {
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("add_one_to", FnAccess::Public, &[core::any::TypeId::of::<INT>()],
CallableFunction::from_plugin(add_one_to_token()));
add_one_to_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -383,7 +383,7 @@ mod generate_tests {
}
}
pub fn add_one_to_token_callable() -> CallableFunction {
CallableFunction::from_plugin(add_one_to_token())
add_one_to_token().into()
}
pub fn add_one_to_token_input_types() -> Box<[TypeId]> {
add_one_to_token().input_types()
@ -432,10 +432,10 @@ mod generate_tests {
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("add_n", FnAccess::Public, &[core::any::TypeId::of::<INT>()],
CallableFunction::from_plugin(add_one_to_token()));
add_one_to_token().into());
m.set_fn("add_n", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
core::any::TypeId::of::<INT>()],
CallableFunction::from_plugin(add_n_to_token()));
add_n_to_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -460,7 +460,7 @@ mod generate_tests {
}
}
pub fn add_one_to_token_callable() -> CallableFunction {
CallableFunction::from_plugin(add_one_to_token())
add_one_to_token().into()
}
pub fn add_one_to_token_input_types() -> Box<[TypeId]> {
add_one_to_token().input_types()
@ -490,7 +490,7 @@ mod generate_tests {
}
}
pub fn add_n_to_token_callable() -> CallableFunction {
CallableFunction::from_plugin(add_n_to_token())
add_n_to_token().into()
}
pub fn add_n_to_token_input_types() -> Box<[TypeId]> {
add_n_to_token().input_types()
@ -529,7 +529,7 @@ mod generate_tests {
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("add_together", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
core::any::TypeId::of::<INT>()],
CallableFunction::from_plugin(add_together_token()));
add_together_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -556,7 +556,7 @@ mod generate_tests {
}
}
pub fn add_together_token_callable() -> CallableFunction {
CallableFunction::from_plugin(add_together_token())
add_together_token().into()
}
pub fn add_together_token_input_types() -> Box<[TypeId]> {
add_together_token().input_types()
@ -596,13 +596,13 @@ mod generate_tests {
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("add", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
core::any::TypeId::of::<INT>()],
CallableFunction::from_plugin(add_together_token()));
add_together_token().into());
m.set_fn("+", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
core::any::TypeId::of::<INT>()],
CallableFunction::from_plugin(add_together_token()));
add_together_token().into());
m.set_fn("add_together", FnAccess::Public, &[core::any::TypeId::of::<INT>(),
core::any::TypeId::of::<INT>()],
CallableFunction::from_plugin(add_together_token()));
add_together_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -629,7 +629,7 @@ mod generate_tests {
}
}
pub fn add_together_token_callable() -> CallableFunction {
CallableFunction::from_plugin(add_together_token())
add_together_token().into()
}
pub fn add_together_token_input_types() -> Box<[TypeId]> {
add_together_token().input_types()
@ -844,7 +844,7 @@ mod generate_tests {
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("get_mystic_number", FnAccess::Public, &[],
CallableFunction::from_plugin(get_mystic_number_token()));
get_mystic_number_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -868,7 +868,7 @@ mod generate_tests {
}
}
pub fn get_mystic_number_token_callable() -> CallableFunction {
CallableFunction::from_plugin(get_mystic_number_token())
get_mystic_number_token().into()
}
pub fn get_mystic_number_token_input_types() -> Box<[TypeId]> {
get_mystic_number_token().input_types()
@ -937,7 +937,7 @@ mod generate_tests {
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("print_out_to", FnAccess::Public,
&[core::any::TypeId::of::<ImmutableString>()],
CallableFunction::from_plugin(print_out_to_token()));
print_out_to_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -962,7 +962,7 @@ mod generate_tests {
}
}
pub fn print_out_to_token_callable() -> CallableFunction {
CallableFunction::from_plugin(print_out_to_token())
print_out_to_token().into()
}
pub fn print_out_to_token_input_types() -> Box<[TypeId]> {
print_out_to_token().input_types()
@ -1001,7 +1001,7 @@ mod generate_tests {
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("print_out_to", FnAccess::Public,
&[core::any::TypeId::of::<ImmutableString>()],
CallableFunction::from_plugin(print_out_to_token()));
print_out_to_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1026,7 +1026,7 @@ mod generate_tests {
}
}
pub fn print_out_to_token_callable() -> CallableFunction {
CallableFunction::from_plugin(print_out_to_token())
print_out_to_token().into()
}
pub fn print_out_to_token_input_types() -> Box<[TypeId]> {
print_out_to_token().input_types()
@ -1065,7 +1065,7 @@ mod generate_tests {
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("increment", FnAccess::Public,
&[core::any::TypeId::of::<FLOAT>()],
CallableFunction::from_plugin(increment_token()));
increment_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1090,7 +1090,7 @@ mod generate_tests {
}
}
pub fn increment_token_callable() -> CallableFunction {
CallableFunction::from_plugin(increment_token())
increment_token().into()
}
pub fn increment_token_input_types() -> Box<[TypeId]> {
increment_token().input_types()
@ -1132,7 +1132,7 @@ mod generate_tests {
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("increment", FnAccess::Public,
&[core::any::TypeId::of::<FLOAT>()],
CallableFunction::from_plugin(increment_token()));
increment_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1157,7 +1157,7 @@ mod generate_tests {
}
}
pub fn increment_token_callable() -> CallableFunction {
CallableFunction::from_plugin(increment_token())
increment_token().into()
}
pub fn increment_token_input_types() -> Box<[TypeId]> {
increment_token().input_types()
@ -1219,7 +1219,7 @@ mod generate_tests {
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("increment", FnAccess::Public,
&[core::any::TypeId::of::<FLOAT>()],
CallableFunction::from_plugin(increment_token()));
increment_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1244,7 +1244,7 @@ mod generate_tests {
}
}
pub fn increment_token_callable() -> CallableFunction {
CallableFunction::from_plugin(increment_token())
increment_token().into()
}
pub fn increment_token_input_types() -> Box<[TypeId]> {
increment_token().input_types()
@ -1304,7 +1304,7 @@ mod generate_tests {
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("get$square", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
int_foo_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1329,7 +1329,7 @@ mod generate_tests {
}
}
pub fn int_foo_token_callable() -> CallableFunction {
CallableFunction::from_plugin(int_foo_token())
int_foo_token().into()
}
pub fn int_foo_token_input_types() -> Box<[TypeId]> {
int_foo_token().input_types()
@ -1368,9 +1368,9 @@ mod generate_tests {
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn("square", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
int_foo_token().into());
m.set_fn("get$square", FnAccess::Public, &[core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
int_foo_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1395,7 +1395,7 @@ mod generate_tests {
}
}
pub fn int_foo_token_callable() -> CallableFunction {
CallableFunction::from_plugin(int_foo_token())
int_foo_token().into()
}
pub fn int_foo_token_input_types() -> Box<[TypeId]> {
int_foo_token().input_types()
@ -1436,7 +1436,7 @@ mod generate_tests {
m.set_fn("set$squared", FnAccess::Public,
&[core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
int_foo_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1462,7 +1462,7 @@ mod generate_tests {
}
}
pub fn int_foo_token_callable() -> CallableFunction {
CallableFunction::from_plugin(int_foo_token())
int_foo_token().into()
}
pub fn int_foo_token_input_types() -> Box<[TypeId]> {
int_foo_token().input_types()
@ -1503,11 +1503,11 @@ mod generate_tests {
m.set_fn("set_sq", FnAccess::Public,
&[core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
int_foo_token().into());
m.set_fn("set$squared", FnAccess::Public,
&[core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(int_foo_token()));
int_foo_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1533,7 +1533,7 @@ mod generate_tests {
}
}
pub fn int_foo_token_callable() -> CallableFunction {
CallableFunction::from_plugin(int_foo_token())
int_foo_token().into()
}
pub fn int_foo_token_input_types() -> Box<[TypeId]> {
int_foo_token().input_types()
@ -1574,7 +1574,7 @@ mod generate_tests {
m.set_fn("index$get$", FnAccess::Public,
&[core::any::TypeId::of::<MyCollection>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(get_by_index_token()));
get_by_index_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1601,7 +1601,7 @@ mod generate_tests {
}
}
pub fn get_by_index_token_callable() -> CallableFunction {
CallableFunction::from_plugin(get_by_index_token())
get_by_index_token().into()
}
pub fn get_by_index_token_input_types() -> Box<[TypeId]> {
get_by_index_token().input_types()
@ -1642,11 +1642,11 @@ mod generate_tests {
m.set_fn("get", FnAccess::Public,
&[core::any::TypeId::of::<MyCollection>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(get_by_index_token()));
get_by_index_token().into());
m.set_fn("index$get$", FnAccess::Public,
&[core::any::TypeId::of::<MyCollection>(),
core::any::TypeId::of::<u64>()],
CallableFunction::from_plugin(get_by_index_token()));
get_by_index_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1673,7 +1673,7 @@ mod generate_tests {
}
}
pub fn get_by_index_token_callable() -> CallableFunction {
CallableFunction::from_plugin(get_by_index_token())
get_by_index_token().into()
}
pub fn get_by_index_token_input_types() -> Box<[TypeId]> {
get_by_index_token().input_types()
@ -1715,7 +1715,7 @@ mod generate_tests {
&[core::any::TypeId::of::<MyCollection>(),
core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<FLOAT>()],
CallableFunction::from_plugin(set_by_index_token()));
set_by_index_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1744,7 +1744,7 @@ mod generate_tests {
}
}
pub fn set_by_index_token_callable() -> CallableFunction {
CallableFunction::from_plugin(set_by_index_token())
set_by_index_token().into()
}
pub fn set_by_index_token_input_types() -> Box<[TypeId]> {
set_by_index_token().input_types()
@ -1786,12 +1786,12 @@ mod generate_tests {
&[core::any::TypeId::of::<MyCollection>(),
core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<FLOAT>()],
CallableFunction::from_plugin(set_by_index_token()));
set_by_index_token().into());
m.set_fn("index$set$", FnAccess::Public,
&[core::any::TypeId::of::<MyCollection>(),
core::any::TypeId::of::<u64>(),
core::any::TypeId::of::<FLOAT>()],
CallableFunction::from_plugin(set_by_index_token()));
set_by_index_token().into());
if flatten {} else {}
}
#[allow(non_camel_case_types)]
@ -1820,7 +1820,7 @@ mod generate_tests {
}
}
pub fn set_by_index_token_callable() -> CallableFunction {
CallableFunction::from_plugin(set_by_index_token())
set_by_index_token().into()
}
pub fn set_by_index_token_input_types() -> Box<[TypeId]> {
set_by_index_token().input_types()

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.