Remove unnecessary "sync" feature gates.

This commit is contained in:
Stephen Chung 2020-06-08 14:10:16 +08:00
parent a4cabc1ac7
commit b4b835f80a
3 changed files with 22 additions and 70 deletions

View File

@ -1,5 +1,6 @@
//! Helper module which defines the `Any` trait to to allow dynamic value handling. //! Helper module which defines the `Any` trait to to allow dynamic value handling.
use crate::fn_native::SendSync;
use crate::module::Module; use crate::module::Module;
use crate::parser::{ImmutableString, INT}; use crate::parser::{ImmutableString, INT};
use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast};
@ -54,31 +55,6 @@ pub trait Variant: Any {
fn _closed(&self) -> _Private; fn _closed(&self) -> _Private;
} }
#[cfg(not(feature = "sync"))]
impl<T: Any + Clone> 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<Self>) -> Box<dyn Any> {
self as Box<dyn Any>
}
fn type_name(&self) -> &'static str {
type_name::<T>()
}
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. /// Trait to represent any type.
/// ///
/// `From<_>` is implemented for `i64` (`i32` if `only_i32`), `f64` (if not `no_float`), /// `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; fn _closed(&self) -> _Private;
} }
#[cfg(feature = "sync")] impl<T: Any + Clone + SendSync> Variant for T {
impl<T: Any + Clone + Send + Sync> Variant for T {
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self as &dyn Any self as &dyn Any
} }

View File

@ -4,7 +4,7 @@
use crate::any::{Dynamic, Variant}; use crate::any::{Dynamic, Variant};
use crate::engine::Engine; 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::parser::FnAccess;
use crate::result::EvalAltResult; use crate::result::EvalAltResult;
use crate::utils::ImmutableString; use crate::utils::ImmutableString;
@ -101,10 +101,10 @@ pub fn by_ref<T: Variant + Clone>(data: &mut Dynamic) -> &mut T {
#[inline(always)] #[inline(always)]
pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T { pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
if TypeId::of::<T>() == TypeId::of::<&str>() { if TypeId::of::<T>() == TypeId::of::<&str>() {
// &str parameters are mapped to the underlying ImmutableString // If T is &str, data must be ImmutableString, so map directly to it
let r = data.as_str().unwrap(); let ref_str = data.as_str().unwrap();
let x = unsafe { mem::transmute::<_, &T>(&r) }; let ref_T = unsafe { mem::transmute::<_, &T>(&ref_str) };
x.clone() ref_T.clone()
} else { } else {
// We consume the argument and then replace it with () - the argument is not supposed to be used again. // 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. // 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 // ^ dereferencing function
impl< impl<
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
FN: Fn($($param),*) -> RET + SendSync + 'static,
#[cfg(feature = "sync")]
FN: Fn($($param),*) -> RET + Send + Sync + 'static,
#[cfg(not(feature = "sync"))]
FN: Fn($($param),*) -> RET + 'static,
RET: Variant + Clone RET: Variant + Clone
> RegisterFn<FN, ($($mark,)*), RET> for Engine > RegisterFn<FN, ($($mark,)*), RET> for Engine
{ {
@ -198,11 +192,7 @@ macro_rules! def_register {
impl< impl<
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
FN: Fn($($param),*) -> Result<Dynamic, Box<EvalAltResult>> + SendSync + 'static,
#[cfg(feature = "sync")]
FN: Fn($($param),*) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync + 'static,
#[cfg(not(feature = "sync"))]
FN: Fn($($param),*) -> Result<Dynamic, Box<EvalAltResult>> + 'static,
> RegisterResultFn<FN, ($($mark,)*)> for Engine > RegisterResultFn<FN, ($($mark,)*)> for Engine
{ {
fn register_result_fn(&mut self, name: &str, f: FN) { fn register_result_fn(&mut self, name: &str, f: FN) {

View File

@ -321,8 +321,7 @@ impl Module {
pub fn set_fn_0<T: Variant + Clone>( pub fn set_fn_0<T: Variant + Clone>(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn<T> + 'static, func: impl Fn() -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn() -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); let f = move |_: &mut FnCallArgs| func().map(Dynamic::from);
let args = []; let args = [];
@ -350,8 +349,7 @@ impl Module {
pub fn set_fn_1<A: Variant + Clone, T: Variant + Clone>( pub fn set_fn_1<A: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn<T> + 'static, func: impl Fn(A) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(A) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = let f =
move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::<A>()).map(Dynamic::from); move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::<A>()).map(Dynamic::from);
@ -380,8 +378,7 @@ impl Module {
pub fn set_fn_1_mut<A: Variant + Clone, T: Variant + Clone>( pub fn set_fn_1_mut<A: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn<T> + 'static, func: impl Fn(&mut A) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = move |args: &mut FnCallArgs| { let f = move |args: &mut FnCallArgs| {
func(args[0].downcast_mut::<A>().unwrap()).map(Dynamic::from) func(args[0].downcast_mut::<A>().unwrap()).map(Dynamic::from)
@ -412,8 +409,7 @@ impl Module {
pub fn set_getter_fn<A: Variant + Clone, T: Variant + Clone>( pub fn set_getter_fn<A: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn<T> + 'static, func: impl Fn(&mut A) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
self.set_fn_1_mut(make_getter(&name.into()), func) self.set_fn_1_mut(make_getter(&name.into()), func)
} }
@ -436,8 +432,7 @@ impl Module {
pub fn set_fn_2<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>( pub fn set_fn_2<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(A, B) -> FuncReturn<T> + 'static, func: impl Fn(A, B) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(A, B) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = move |args: &mut FnCallArgs| { let f = move |args: &mut FnCallArgs| {
let a = mem::take(args[0]).cast::<A>(); let a = mem::take(args[0]).cast::<A>();
@ -473,8 +468,7 @@ impl Module {
pub fn set_fn_2_mut<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>( pub fn set_fn_2_mut<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn<T> + 'static, func: impl Fn(&mut A, B) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = move |args: &mut FnCallArgs| { let f = move |args: &mut FnCallArgs| {
let b = mem::take(args[1]).cast::<B>(); let b = mem::take(args[1]).cast::<B>();
@ -512,8 +506,7 @@ impl Module {
pub fn set_setter_fn<A: Variant + Clone, B: Variant + Clone>( pub fn set_setter_fn<A: Variant + Clone, B: Variant + Clone>(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn<()> + 'static, func: impl Fn(&mut A, B) -> FuncReturn<()> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn<()> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
self.set_fn_2_mut(make_setter(&name.into()), func) self.set_fn_2_mut(make_setter(&name.into()), func)
} }
@ -538,8 +531,7 @@ impl Module {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
pub fn set_indexer_get_fn<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>( pub fn set_indexer_get_fn<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn<T> + 'static, func: impl Fn(&mut A, B) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
self.set_fn_2_mut(FUNC_INDEXER_GET, func) self.set_fn_2_mut(FUNC_INDEXER_GET, func)
} }
@ -567,8 +559,7 @@ impl Module {
>( >(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(A, B, C) -> FuncReturn<T> + 'static, func: impl Fn(A, B, C) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(A, B, C) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = move |args: &mut FnCallArgs| { let f = move |args: &mut FnCallArgs| {
let a = mem::take(args[0]).cast::<A>(); let a = mem::take(args[0]).cast::<A>();
@ -610,8 +601,7 @@ impl Module {
>( >(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C) -> FuncReturn<T> + 'static, func: impl Fn(&mut A, B, C) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A, B, C) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = move |args: &mut FnCallArgs| { let f = move |args: &mut FnCallArgs| {
let b = mem::take(args[1]).cast::<B>(); let b = mem::take(args[1]).cast::<B>();
@ -648,8 +638,7 @@ impl Module {
/// ``` /// ```
pub fn set_indexer_set_fn<A: Variant + Clone, B: Variant + Clone>( pub fn set_indexer_set_fn<A: Variant + Clone, B: Variant + Clone>(
&mut self, &mut self,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, A) -> FuncReturn<()> + 'static, func: impl Fn(&mut A, B, A) -> FuncReturn<()> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A, B, A) -> FuncReturn<()> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = move |args: &mut FnCallArgs| { let f = move |args: &mut FnCallArgs| {
let b = mem::take(args[1]).cast::<B>(); let b = mem::take(args[1]).cast::<B>();
@ -691,8 +680,7 @@ impl Module {
>( >(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(A, B, C, D) -> FuncReturn<T> + 'static, func: impl Fn(A, B, C, D) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(A, B, C, D) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = move |args: &mut FnCallArgs| { let f = move |args: &mut FnCallArgs| {
let a = mem::take(args[0]).cast::<A>(); let a = mem::take(args[0]).cast::<A>();
@ -741,8 +729,7 @@ impl Module {
>( >(
&mut self, &mut self,
name: impl Into<String>, name: impl Into<String>,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C, D) -> FuncReturn<T> + 'static, func: impl Fn(&mut A, B, C, D) -> FuncReturn<T> + SendSync + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A, B, C, D) -> FuncReturn<T> + Send + Sync + 'static,
) -> u64 { ) -> u64 {
let f = move |args: &mut FnCallArgs| { let f = move |args: &mut FnCallArgs| {
let b = mem::take(args[1]).cast::<B>(); let b = mem::take(args[1]).cast::<B>();