Unify all functions under CallableFunction type.

This commit is contained in:
Stephen Chung
2020-05-19 19:03:06 +08:00
parent a22f338b03
commit 3295060dba
8 changed files with 199 additions and 220 deletions

View File

@@ -1,4 +1,5 @@
use crate::any::Dynamic;
use crate::parser::{FnDef, SharedFnDef};
use crate::result::EvalAltResult;
use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc};
@@ -72,70 +73,98 @@ pub trait IteratorCallback: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + '
#[cfg(not(feature = "sync"))]
impl<F: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static> IteratorCallback for F {}
/// A type representing the type of ABI of a native Rust function.
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
pub enum NativeFunctionABI {
/// A pure function where all arguments are passed by value.
Pure,
/// An object method where the first argument is the object passed by mutable reference.
/// All other arguments are passed by value.
Method,
/// A type encapsulating a function callable by Rhai.
pub enum CallableFunction {
/// A pure native Rust function with all arguments passed by value.
Pure(Box<FnAny>),
/// A native Rust object method with the first argument passed by reference,
/// and the rest passed by value.
Method(Box<FnAny>),
/// An iterator function.
Iterator(Box<IteratorFn>),
/// A script-defined function.
Script(SharedFnDef),
}
/*
/// Trait implemented by all native Rust functions that are callable by Rhai.
impl CallableFunction {
/// Is this a pure native Rust function?
pub fn is_pure(&self) -> bool {
match self {
CallableFunction::Pure(_) => true,
CallableFunction::Method(_)
| CallableFunction::Iterator(_)
| CallableFunction::Script(_) => false,
}
}
/// Is this a pure native Rust method-call?
pub fn is_method(&self) -> bool {
match self {
CallableFunction::Method(_) => true,
CallableFunction::Pure(_)
| CallableFunction::Iterator(_)
| CallableFunction::Script(_) => false,
}
}
/// Is this an iterator function?
pub fn is_iter(&self) -> bool {
match self {
CallableFunction::Iterator(_) => true,
CallableFunction::Pure(_)
| CallableFunction::Method(_)
| CallableFunction::Script(_) => false,
}
}
/// Is this a Rhai-scripted function?
pub fn is_script(&self) -> bool {
match self {
CallableFunction::Script(_) => true,
CallableFunction::Pure(_)
| CallableFunction::Method(_)
| CallableFunction::Iterator(_) => false,
}
}
/// Get a reference to a native Rust function.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Pure` or `Method`.
pub fn get_native_fn(&self) -> &Box<FnAny> {
match self {
CallableFunction::Pure(f) | CallableFunction::Method(f) => f,
CallableFunction::Iterator(_) | CallableFunction::Script(_) => panic!(),
}
}
/// Get a reference to a script-defined function definition.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Script`.
pub fn get_fn_def(&self) -> &FnDef {
match self {
CallableFunction::Pure(_)
| CallableFunction::Method(_)
| CallableFunction::Iterator(_) => panic!(),
CallableFunction::Script(f) => f,
}
}
/// Get a reference to an iterator function.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Iterator`.
pub fn get_iter_fn(&self) -> &Box<IteratorFn> {
match self {
CallableFunction::Pure(_)
| CallableFunction::Method(_)
| CallableFunction::Script(_) => panic!(),
CallableFunction::Iterator(f) => f,
}
}
}
/// A callable function.
#[cfg(not(feature = "sync"))]
pub trait NativeCallable {
/// Get the ABI type of a native Rust function.
fn abi(&self) -> NativeFunctionABI;
/// Call a native Rust function.
fn call(&self, args: &mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>>;
}
/// Trait implemented by all native Rust functions that are callable by Rhai.
pub type SharedFunction = Rc<CallableFunction>;
/// A callable function.
#[cfg(feature = "sync")]
pub trait NativeCallable: Send + Sync {
/// Get the ABI type of a native Rust function.
fn abi(&self) -> NativeFunctionABI;
/// 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 NativeFunction {
pub fn abi(&self) -> NativeFunctionABI {
self.1
}
pub fn call(&self, args: &mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>> {
(self.0)(args)
}
}
impl From<(Box<FnAny>, NativeFunctionABI)> for NativeFunction {
fn from(func: (Box<FnAny>, NativeFunctionABI)) -> Self {
Self::new(func.0, func.1)
}
}
impl NativeFunction {
/// Create a new `NativeFunction`.
pub fn new(func: Box<FnAny>, abi: NativeFunctionABI) -> Self {
Self(func, abi)
}
}
/// An external native Rust function.
#[cfg(not(feature = "sync"))]
pub type SharedNativeFunction = Rc<NativeFunction>;
/// An external native Rust function.
#[cfg(feature = "sync")]
pub type SharedNativeFunction = Arc<NativeFunction>;
/// A type iterator function.
#[cfg(not(feature = "sync"))]
pub type SharedIteratorFunction = Rc<Box<IteratorFn>>;
/// An external native Rust function.
#[cfg(feature = "sync")]
pub type SharedIteratorFunction = Arc<Box<IteratorFn>>;
pub type SharedFunction = Arc<CallableFunction>;