From ea78fa2b27bd4bb03399d41501e5e5247579ec6a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 29 Sep 2020 13:06:48 +0800 Subject: [PATCH] Change SharedPluginFunction to Shared --- codegen/src/function.rs | 2 +- codegen/src/rhai_module.rs | 2 +- codegen/src/test/function.rs | 10 ++-- codegen/src/test/module.rs | 92 ++++++++++++++++++------------------ src/fn_native.rs | 36 +++++++------- src/fn_register.rs | 2 +- src/syntax.rs | 10 +--- src/utils.rs | 3 +- 8 files changed, 77 insertions(+), 80 deletions(-) diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 72ad2b64..47427ee8 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -580,7 +580,7 @@ impl ExportedFn { ); quote! { pub fn #callable_fn_name() -> CallableFunction { - CallableFunction::from_plugin(#token_name()) + #token_name().into() } } } diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index f6939ae7..94c0f720 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -126,7 +126,7 @@ pub(crate) fn generate_body( set_fn_stmts.push( syn::parse2::(quote! { m.set_fn(#fn_literal, FnAccess::Public, &[#(#fn_input_types),*], - CallableFunction::from_plugin(#fn_token_name())); + #fn_token_name().into()); }) .unwrap(), ); diff --git a/codegen/src/test/function.rs b/codegen/src/test/function.rs index a5b86ba1..2641417a 100644 --- a/codegen/src/test/function.rs +++ b/codegen/src/test/function.rs @@ -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() diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 472c949d..dbb2d023 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -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::()], - 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::()], - CallableFunction::from_plugin(add_one_to_token())); + add_one_to_token().into()); m.set_fn("add_n", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], - 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::(), core::any::TypeId::of::()], - 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::(), core::any::TypeId::of::()], - CallableFunction::from_plugin(add_together_token())); + add_together_token().into()); m.set_fn("+", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], - CallableFunction::from_plugin(add_together_token())); + add_together_token().into()); m.set_fn("add_together", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], - 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::()], - 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::()], - 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::()], - 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::()], - 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::()], - 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::()], - 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::()], - CallableFunction::from_plugin(int_foo_token())); + int_foo_token().into()); m.set_fn("get$square", FnAccess::Public, &[core::any::TypeId::of::()], - 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::(), core::any::TypeId::of::()], - 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::(), core::any::TypeId::of::()], - CallableFunction::from_plugin(int_foo_token())); + int_foo_token().into()); m.set_fn("set$squared", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], - 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::(), core::any::TypeId::of::()], - 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::(), core::any::TypeId::of::()], - CallableFunction::from_plugin(get_by_index_token())); + get_by_index_token().into()); m.set_fn("index$get$", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::()], - 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::(), core::any::TypeId::of::(), core::any::TypeId::of::()], - 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::(), core::any::TypeId::of::(), core::any::TypeId::of::()], - CallableFunction::from_plugin(set_by_index_token())); + set_by_index_token().into()); m.set_fn("index$set$", FnAccess::Public, &[core::any::TypeId::of::(), core::any::TypeId::of::(), core::any::TypeId::of::()], - 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() diff --git a/src/fn_native.rs b/src/fn_native.rs index 041ff224..390fa340 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -199,10 +199,10 @@ pub type FnAny = /// A standard function that gets an iterator from a type. pub type IteratorFn = fn(Dynamic) -> Box>; -#[cfg(feature = "sync")] -pub type SharedPluginFunction = Arc; #[cfg(not(feature = "sync"))] -pub type SharedPluginFunction = Rc; +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), /// A script-defined function. #[cfg(not(feature = "no_function"))] Script(Shared), @@ -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) -> 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).into()) } } @@ -445,3 +437,15 @@ impl From> for CallableFunction { Self::Script(_func) } } + +impl From for CallableFunction { + fn from(func: T) -> Self { + Self::from_plugin(func) + } +} + +impl From> for CallableFunction { + fn from(func: Shared) -> Self { + Self::Plugin(func.into()) + } +} diff --git a/src/fn_register.rs b/src/fn_register.rs index 12aedad2..b6b7b896 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -90,7 +90,7 @@ pub trait RegisterPlugin { /// std::any::TypeId::of::(), /// std::any::TypeId::of::(), /// std::any::TypeId::of::()], - /// CallableFunction::from_plugin(DistanceFunction())); + /// DistanceFunction().into()); /// let mut r = StaticModuleResolver::new(); /// r.insert("Math::Advanced".to_string(), m); /// engine.set_module_resolver(Some(r)); diff --git a/src/syntax.rs b/src/syntax.rs index 4cc937e6..cf9c9519 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -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).into(), scope_delta, }; diff --git a/src/utils.rs b/src/utils.rs index 2587262b..a257d910 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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.