From a22f338b03857742f6a650b77885a4a5fb6ac94a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 10:13:37 +0800 Subject: [PATCH] Back out NativeCallable trait. --- src/fn_native.rs | 12 +++++++----- src/lib.rs | 2 +- src/module.rs | 10 +++++----- src/optimize.rs | 1 - src/packages/mod.rs | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/fn_native.rs b/src/fn_native.rs index 7a1dd0e6..ccc458d3 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -82,6 +82,7 @@ pub enum NativeFunctionABI { Method, } +/* /// Trait implemented by all native Rust functions that are callable by Rhai. #[cfg(not(feature = "sync"))] pub trait NativeCallable { @@ -99,15 +100,16 @@ pub trait NativeCallable: Send + Sync { /// Call a native Rust function. fn call(&self, args: &mut FnCallArgs) -> Result>; } +*/ /// A type encapsulating a native Rust function callable by Rhai. pub struct NativeFunction(Box, NativeFunctionABI); -impl NativeCallable for NativeFunction { - fn abi(&self) -> NativeFunctionABI { +impl NativeFunction { + pub fn abi(&self) -> NativeFunctionABI { self.1 } - fn call(&self, args: &mut FnCallArgs) -> Result> { + pub fn call(&self, args: &mut FnCallArgs) -> Result> { (self.0)(args) } } @@ -126,10 +128,10 @@ impl NativeFunction { /// An external native Rust function. #[cfg(not(feature = "sync"))] -pub type SharedNativeFunction = Rc>; +pub type SharedNativeFunction = Rc; /// An external native Rust function. #[cfg(feature = "sync")] -pub type SharedNativeFunction = Arc>; +pub type SharedNativeFunction = Arc; /// A type iterator function. #[cfg(not(feature = "sync"))] diff --git a/src/lib.rs b/src/lib.rs index 5c7a10ae..2494d71f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,7 @@ mod utils; pub use any::Dynamic; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; -pub use fn_native::NativeCallable; +//pub use fn_native::NativeCallable; pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; pub use module::Module; pub use parser::{AST, INT}; diff --git a/src/module.rs b/src/module.rs index 377a4357..f28c87fe 100644 --- a/src/module.rs +++ b/src/module.rs @@ -4,8 +4,8 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; use crate::fn_native::{ - FnAny, FnCallArgs, IteratorFn, NativeCallable, NativeFunction, NativeFunctionABI, - NativeFunctionABI::*, SharedIteratorFunction, SharedNativeFunction, + FnAny, FnCallArgs, IteratorFn, NativeFunction, NativeFunctionABI, NativeFunctionABI::*, + SharedIteratorFunction, SharedNativeFunction, }; use crate::parser::{FnAccess, FnDef, SharedFnDef, AST}; use crate::result::EvalAltResult; @@ -285,7 +285,7 @@ impl Module { ) -> u64 { let hash_fn = calc_fn_hash(empty(), &name, params.iter().cloned()); - let f = Box::new(NativeFunction::from((func, abi))) as Box; + let f = NativeFunction::from((func, abi)); #[cfg(not(feature = "sync"))] let func = Rc::new(f); @@ -531,7 +531,7 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash_fn: u64) -> Option<&Box> { + pub fn get_fn(&self, hash_fn: u64) -> Option<&NativeFunction> { self.functions.get(&hash_fn).map(|(_, _, _, v)| v.as_ref()) } @@ -543,7 +543,7 @@ impl Module { &mut self, name: &str, hash_fn_native: u64, - ) -> Result<&Box, Box> { + ) -> Result<&NativeFunction, Box> { self.all_functions .get(&hash_fn_native) .map(|f| f.as_ref()) diff --git a/src/optimize.rs b/src/optimize.rs index 671415e3..5fc6d608 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -15,7 +15,6 @@ use crate::utils::StaticVec; use crate::stdlib::{ boxed::Box, iter::empty, - mem, string::{String, ToString}, vec, vec::Vec, diff --git a/src/packages/mod.rs b/src/packages/mod.rs index e56843f0..ad61ae19 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,6 @@ //! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::fn_native::{NativeCallable, SharedIteratorFunction}; +use crate::fn_native::{NativeFunction, SharedIteratorFunction}; use crate::module::Module; use crate::utils::StaticVec; @@ -69,7 +69,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_fn(hash)) } /// Get specified function via its hash key. - pub fn get_fn(&self, hash: u64) -> Option<&Box> { + pub fn get_fn(&self, hash: u64) -> Option<&NativeFunction> { self.packages .iter() .map(|p| p.get_fn(hash))