2020-05-11 13:36:50 +08:00
|
|
|
use crate::any::Dynamic;
|
|
|
|
use crate::result::EvalAltResult;
|
|
|
|
|
|
|
|
use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc};
|
|
|
|
|
|
|
|
pub type FnCallArgs<'a> = [&'a mut Dynamic];
|
|
|
|
|
|
|
|
#[cfg(feature = "sync")]
|
2020-05-13 21:58:38 +08:00
|
|
|
pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync;
|
2020-05-11 13:36:50 +08:00
|
|
|
#[cfg(not(feature = "sync"))]
|
2020-05-13 21:58:38 +08:00
|
|
|
pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>>;
|
2020-05-11 13:36:50 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub type IteratorFn = dyn Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync;
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub type IteratorFn = dyn Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
|
|
|
|
|
2020-05-12 16:32:22 +08:00
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub type PrintCallback = dyn Fn(&str) + Send + Sync + 'static;
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub type PrintCallback = dyn Fn(&str) + 'static;
|
2020-05-15 11:43:32 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub type ProgressCallback = dyn Fn(u64) -> bool + Send + Sync + 'static;
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub type ProgressCallback = dyn Fn(u64) -> bool + 'static;
|
2020-05-12 16:32:22 +08:00
|
|
|
|
|
|
|
// Define callback function types
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub trait ObjectGetCallback<T, U>: Fn(&mut T) -> U + Send + Sync + 'static {}
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
impl<F: Fn(&mut T) -> U + Send + Sync + 'static, T, U> ObjectGetCallback<T, U> for F {}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub trait ObjectGetCallback<T, U>: Fn(&mut T) -> U + 'static {}
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
impl<F: Fn(&mut T) -> U + 'static, T, U> ObjectGetCallback<T, U> for F {}
|
|
|
|
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub trait ObjectSetCallback<T, U>: Fn(&mut T, U) + Send + Sync + 'static {}
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
impl<F: Fn(&mut T, U) + Send + Sync + 'static, T, U> ObjectSetCallback<T, U> for F {}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub trait ObjectSetCallback<T, U>: Fn(&mut T, U) + 'static {}
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
impl<F: Fn(&mut T, U) + 'static, T, U> ObjectSetCallback<T, U> for F {}
|
|
|
|
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub trait ObjectIndexerCallback<T, X, U>: Fn(&mut T, X) -> U + Send + Sync + 'static {}
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
impl<F: Fn(&mut T, X) -> U + Send + Sync + 'static, T, X, U> ObjectIndexerCallback<T, X, U> for F {}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub trait ObjectIndexerCallback<T, X, U>: Fn(&mut T, X) -> U + 'static {}
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
impl<F: Fn(&mut T, X) -> U + 'static, T, X, U> ObjectIndexerCallback<T, X, U> for F {}
|
|
|
|
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub trait IteratorCallback:
|
|
|
|
Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
impl<F: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static> IteratorCallback
|
|
|
|
for F
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub trait IteratorCallback: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static {}
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
impl<F: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static> IteratorCallback for F {}
|
|
|
|
|
2020-05-11 18:55:58 +08:00
|
|
|
/// A type representing the type of ABI of a native Rust function.
|
2020-05-12 18:48:25 +08:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
|
2020-05-11 18:55:58 +08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2020-05-15 21:40:54 +08:00
|
|
|
/// Trait implemented by all native Rust functions that are callable by Rhai.
|
2020-05-12 16:32:22 +08:00
|
|
|
#[cfg(not(feature = "sync"))]
|
2020-05-11 13:36:50 +08:00
|
|
|
pub trait NativeCallable {
|
2020-05-11 18:55:58 +08:00
|
|
|
/// Get the ABI type of a native Rust function.
|
|
|
|
fn abi(&self) -> NativeFunctionABI;
|
2020-05-11 13:36:50 +08:00
|
|
|
/// Call a native Rust function.
|
2020-05-13 21:58:38 +08:00
|
|
|
fn call(&self, args: &mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>>;
|
2020-05-12 16:32:22 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 21:40:54 +08:00
|
|
|
/// Trait implemented by all native Rust functions that are callable by Rhai.
|
2020-05-12 16:32:22 +08:00
|
|
|
#[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.
|
2020-05-13 21:58:38 +08:00
|
|
|
fn call(&self, args: &mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>>;
|
2020-05-11 13:36:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A type encapsulating a native Rust function callable by Rhai.
|
2020-05-11 18:55:58 +08:00
|
|
|
pub struct NativeFunction(Box<FnAny>, NativeFunctionABI);
|
2020-05-11 13:36:50 +08:00
|
|
|
|
|
|
|
impl NativeCallable for NativeFunction {
|
2020-05-11 18:55:58 +08:00
|
|
|
fn abi(&self) -> NativeFunctionABI {
|
|
|
|
self.1
|
|
|
|
}
|
2020-05-13 21:58:38 +08:00
|
|
|
fn call(&self, args: &mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>> {
|
|
|
|
(self.0)(args)
|
2020-05-11 13:36:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:55:58 +08:00
|
|
|
impl From<(Box<FnAny>, NativeFunctionABI)> for NativeFunction {
|
|
|
|
fn from(func: (Box<FnAny>, NativeFunctionABI)) -> Self {
|
|
|
|
Self::new(func.0, func.1)
|
2020-05-11 13:36:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl NativeFunction {
|
|
|
|
/// Create a new `NativeFunction`.
|
2020-05-11 18:55:58 +08:00
|
|
|
pub fn new(func: Box<FnAny>, abi: NativeFunctionABI) -> Self {
|
|
|
|
Self(func, abi)
|
2020-05-11 13:36:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An external native Rust function.
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub type SharedNativeFunction = Rc<Box<dyn NativeCallable>>;
|
|
|
|
/// An external native Rust function.
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub type SharedNativeFunction = Arc<Box<dyn NativeCallable>>;
|
2020-05-13 19:21:42 +08:00
|
|
|
|
|
|
|
/// 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>>;
|