Back out NativeCallable trait.

This commit is contained in:
Stephen Chung 2020-05-19 10:13:37 +08:00
parent 6b8c6bda42
commit a22f338b03
5 changed files with 15 additions and 14 deletions

View File

@ -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<Dynamic, Box<EvalAltResult>>;
}
*/
/// A type encapsulating a native Rust function callable by Rhai.
pub struct NativeFunction(Box<FnAny>, 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<Dynamic, Box<EvalAltResult>> {
pub fn call(&self, args: &mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>> {
(self.0)(args)
}
}
@ -126,10 +128,10 @@ impl NativeFunction {
/// An external native Rust function.
#[cfg(not(feature = "sync"))]
pub type SharedNativeFunction = Rc<Box<dyn NativeCallable>>;
pub type SharedNativeFunction = Rc<NativeFunction>;
/// An external native Rust function.
#[cfg(feature = "sync")]
pub type SharedNativeFunction = Arc<Box<dyn NativeCallable>>;
pub type SharedNativeFunction = Arc<NativeFunction>;
/// A type iterator function.
#[cfg(not(feature = "sync"))]

View File

@ -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};

View File

@ -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<dyn NativeCallable>;
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<dyn NativeCallable>> {
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<dyn NativeCallable>, Box<EvalAltResult>> {
) -> Result<&NativeFunction, Box<EvalAltResult>> {
self.all_functions
.get(&hash_fn_native)
.map(|f| f.as_ref())

View File

@ -15,7 +15,6 @@ use crate::utils::StaticVec;
use crate::stdlib::{
boxed::Box,
iter::empty,
mem,
string::{String, ToString},
vec,
vec::Vec,

View File

@ -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<dyn NativeCallable>> {
pub fn get_fn(&self, hash: u64) -> Option<&NativeFunction> {
self.packages
.iter()
.map(|p| p.get_fn(hash))