diff --git a/src/any.rs b/src/any.rs index 0b6d81d2..0647d219 100644 --- a/src/any.rs +++ b/src/any.rs @@ -1,5 +1,6 @@ //! Helper module which defines the `Any` trait to to allow dynamic value handling. +use crate::fn_native::SendSync; use crate::module::Module; use crate::parser::{ImmutableString, INT}; use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; @@ -54,31 +55,6 @@ pub trait Variant: Any { fn _closed(&self) -> _Private; } -#[cfg(not(feature = "sync"))] -impl Variant for T { - fn as_any(&self) -> &dyn Any { - self as &dyn Any - } - fn as_mut_any(&mut self) -> &mut dyn Any { - self as &mut dyn Any - } - fn as_box_any(self: Box) -> Box { - self as Box - } - fn type_name(&self) -> &'static str { - type_name::() - } - fn into_dynamic(self) -> Dynamic { - Dynamic::from(self) - } - fn clone_into_dynamic(&self) -> Dynamic { - Dynamic::from(self.clone()) - } - fn _closed(&self) -> _Private { - _Private - } -} - /// Trait to represent any type. /// /// `From<_>` is implemented for `i64` (`i32` if `only_i32`), `f64` (if not `no_float`), @@ -108,8 +84,7 @@ pub trait Variant: Any + Send + Sync { fn _closed(&self) -> _Private; } -#[cfg(feature = "sync")] -impl Variant for T { +impl Variant for T { fn as_any(&self) -> &dyn Any { self as &dyn Any } diff --git a/src/fn_register.rs b/src/fn_register.rs index 1384f666..86e7ada9 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -4,7 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::Engine; -use crate::fn_native::{CallableFunction, FnAny, FnCallArgs}; +use crate::fn_native::{CallableFunction, FnAny, FnCallArgs, SendSync}; use crate::parser::FnAccess; use crate::result::EvalAltResult; use crate::utils::ImmutableString; @@ -101,10 +101,10 @@ pub fn by_ref(data: &mut Dynamic) -> &mut T { #[inline(always)] pub fn by_value(data: &mut Dynamic) -> T { if TypeId::of::() == TypeId::of::<&str>() { - // &str parameters are mapped to the underlying ImmutableString - let r = data.as_str().unwrap(); - let x = unsafe { mem::transmute::<_, &T>(&r) }; - x.clone() + // If T is &str, data must be ImmutableString, so map directly to it + let ref_str = data.as_str().unwrap(); + let ref_T = unsafe { mem::transmute::<_, &T>(&ref_str) }; + ref_T.clone() } else { // We consume the argument and then replace it with () - the argument is not supposed to be used again. // This way, we avoid having to clone the argument again, because it is already a clone when passed here. @@ -178,13 +178,7 @@ macro_rules! def_register { // ^ dereferencing function impl< $($par: Variant + Clone,)* - - #[cfg(feature = "sync")] - FN: Fn($($param),*) -> RET + Send + Sync + 'static, - - #[cfg(not(feature = "sync"))] - FN: Fn($($param),*) -> RET + 'static, - + FN: Fn($($param),*) -> RET + SendSync + 'static, RET: Variant + Clone > RegisterFn for Engine { @@ -198,11 +192,7 @@ macro_rules! def_register { impl< $($par: Variant + Clone,)* - - #[cfg(feature = "sync")] - FN: Fn($($param),*) -> Result> + Send + Sync + 'static, - #[cfg(not(feature = "sync"))] - FN: Fn($($param),*) -> Result> + 'static, + FN: Fn($($param),*) -> Result> + SendSync + 'static, > RegisterResultFn for Engine { fn register_result_fn(&mut self, name: &str, f: FN) { diff --git a/src/module.rs b/src/module.rs index 2a722e3b..1deddee4 100644 --- a/src/module.rs +++ b/src/module.rs @@ -321,8 +321,7 @@ impl Module { pub fn set_fn_0( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn() -> FuncReturn + Send + Sync + 'static, + func: impl Fn() -> FuncReturn + SendSync + 'static, ) -> u64 { let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); let args = []; @@ -350,8 +349,7 @@ impl Module { pub fn set_fn_1( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(A) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(A) -> FuncReturn + SendSync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::()).map(Dynamic::from); @@ -380,8 +378,7 @@ impl Module { pub fn set_fn_1_mut( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(&mut A) -> FuncReturn + SendSync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| { func(args[0].downcast_mut::().unwrap()).map(Dynamic::from) @@ -412,8 +409,7 @@ impl Module { pub fn set_getter_fn( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(&mut A) -> FuncReturn + SendSync + 'static, ) -> u64 { self.set_fn_1_mut(make_getter(&name.into()), func) } @@ -436,8 +432,7 @@ impl Module { pub fn set_fn_2( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(A, B) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(A, B) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(A, B) -> FuncReturn + SendSync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| { let a = mem::take(args[0]).cast::(); @@ -473,8 +468,7 @@ impl Module { pub fn set_fn_2_mut( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(&mut A, B) -> FuncReturn + SendSync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| { let b = mem::take(args[1]).cast::(); @@ -512,8 +506,7 @@ impl Module { pub fn set_setter_fn( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn<()> + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn<()> + Send + Sync + 'static, + func: impl Fn(&mut A, B) -> FuncReturn<()> + SendSync + 'static, ) -> u64 { self.set_fn_2_mut(make_setter(&name.into()), func) } @@ -538,8 +531,7 @@ impl Module { #[cfg(not(feature = "no_index"))] pub fn set_indexer_get_fn( &mut self, - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(&mut A, B) -> FuncReturn + SendSync + 'static, ) -> u64 { self.set_fn_2_mut(FUNC_INDEXER_GET, func) } @@ -567,8 +559,7 @@ impl Module { >( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(A, B, C) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(A, B, C) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(A, B, C) -> FuncReturn + SendSync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| { let a = mem::take(args[0]).cast::(); @@ -610,8 +601,7 @@ impl Module { >( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A, B, C) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(&mut A, B, C) -> FuncReturn + SendSync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| { let b = mem::take(args[1]).cast::(); @@ -648,8 +638,7 @@ impl Module { /// ``` pub fn set_indexer_set_fn( &mut self, - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, A) -> FuncReturn<()> + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A, B, A) -> FuncReturn<()> + Send + Sync + 'static, + func: impl Fn(&mut A, B, A) -> FuncReturn<()> + SendSync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| { let b = mem::take(args[1]).cast::(); @@ -691,8 +680,7 @@ impl Module { >( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(A, B, C, D) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(A, B, C, D) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(A, B, C, D) -> FuncReturn + SendSync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| { let a = mem::take(args[0]).cast::(); @@ -741,8 +729,7 @@ impl Module { >( &mut self, name: impl Into, - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C, D) -> FuncReturn + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A, B, C, D) -> FuncReturn + Send + Sync + 'static, + func: impl Fn(&mut A, B, C, D) -> FuncReturn + SendSync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| { let b = mem::take(args[1]).cast::();