2020-05-11 07:36:50 +02:00
|
|
|
use crate::any::Dynamic;
|
2020-05-19 16:25:57 +02:00
|
|
|
use crate::parser::FnDef;
|
2020-05-11 07:36:50 +02:00
|
|
|
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 15:58:38 +02:00
|
|
|
pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync;
|
2020-05-11 07:36:50 +02:00
|
|
|
#[cfg(not(feature = "sync"))]
|
2020-05-13 15:58:38 +02:00
|
|
|
pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>>;
|
2020-05-11 07:36:50 +02:00
|
|
|
|
2020-05-20 13:27:23 +02:00
|
|
|
pub type IteratorFn = fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
|
2020-05-11 07:36:50 +02:00
|
|
|
|
2020-05-12 10:32:22 +02: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 05:43:32 +02: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 10:32:22 +02: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 {}
|
|
|
|
|
2020-05-19 16:25:57 +02:00
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub type SharedNativeFunction = Rc<FnAny>;
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub type SharedNativeFunction = Arc<FnAny>;
|
|
|
|
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub type SharedFnDef = Arc<FnDef>;
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub type SharedFnDef = Rc<FnDef>;
|
|
|
|
|
2020-05-19 13:03:06 +02:00
|
|
|
/// A type encapsulating a function callable by Rhai.
|
2020-05-19 16:25:57 +02:00
|
|
|
#[derive(Clone)]
|
2020-05-19 13:03:06 +02:00
|
|
|
pub enum CallableFunction {
|
|
|
|
/// A pure native Rust function with all arguments passed by value.
|
2020-05-19 16:25:57 +02:00
|
|
|
Pure(SharedNativeFunction),
|
2020-05-19 13:03:06 +02:00
|
|
|
/// A native Rust object method with the first argument passed by reference,
|
|
|
|
/// and the rest passed by value.
|
2020-05-19 16:25:57 +02:00
|
|
|
Method(SharedNativeFunction),
|
2020-05-19 13:03:06 +02:00
|
|
|
/// An iterator function.
|
2020-05-20 13:27:23 +02:00
|
|
|
Iterator(IteratorFn),
|
2020-05-19 13:03:06 +02:00
|
|
|
/// A script-defined function.
|
|
|
|
Script(SharedFnDef),
|
2020-05-11 12:55:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:03:06 +02:00
|
|
|
impl CallableFunction {
|
|
|
|
/// Is this a pure native Rust function?
|
|
|
|
pub fn is_pure(&self) -> bool {
|
|
|
|
match self {
|
2020-05-19 16:25:57 +02:00
|
|
|
Self::Pure(_) => true,
|
|
|
|
Self::Method(_) | Self::Iterator(_) | Self::Script(_) => false,
|
2020-05-19 13:03:06 +02:00
|
|
|
}
|
2020-05-11 12:55:58 +02:00
|
|
|
}
|
2020-05-19 13:03:06 +02:00
|
|
|
/// Is this a pure native Rust method-call?
|
|
|
|
pub fn is_method(&self) -> bool {
|
|
|
|
match self {
|
2020-05-19 16:25:57 +02:00
|
|
|
Self::Method(_) => true,
|
|
|
|
Self::Pure(_) | Self::Iterator(_) | Self::Script(_) => false,
|
2020-05-19 13:03:06 +02:00
|
|
|
}
|
2020-05-11 07:36:50 +02:00
|
|
|
}
|
2020-05-19 13:03:06 +02:00
|
|
|
/// Is this an iterator function?
|
|
|
|
pub fn is_iter(&self) -> bool {
|
|
|
|
match self {
|
2020-05-19 16:25:57 +02:00
|
|
|
Self::Iterator(_) => true,
|
|
|
|
Self::Pure(_) | Self::Method(_) | Self::Script(_) => false,
|
2020-05-19 13:03:06 +02:00
|
|
|
}
|
2020-05-11 07:36:50 +02:00
|
|
|
}
|
2020-05-19 13:03:06 +02:00
|
|
|
/// Is this a Rhai-scripted function?
|
|
|
|
pub fn is_script(&self) -> bool {
|
|
|
|
match self {
|
2020-05-19 16:25:57 +02:00
|
|
|
Self::Script(_) => true,
|
|
|
|
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => false,
|
2020-05-19 13:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Get a reference to a native Rust function.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the `CallableFunction` is not `Pure` or `Method`.
|
2020-05-19 16:25:57 +02:00
|
|
|
pub fn get_native_fn(&self) -> &FnAny {
|
2020-05-19 13:03:06 +02:00
|
|
|
match self {
|
2020-05-19 16:25:57 +02:00
|
|
|
Self::Pure(f) | Self::Method(f) => f.as_ref(),
|
|
|
|
Self::Iterator(_) | Self::Script(_) => panic!(),
|
2020-05-19 13:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// 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 {
|
2020-05-19 16:25:57 +02:00
|
|
|
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => panic!(),
|
|
|
|
Self::Script(f) => f,
|
2020-05-19 13:03:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Get a reference to an iterator function.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the `CallableFunction` is not `Iterator`.
|
2020-05-20 13:27:23 +02:00
|
|
|
pub fn get_iter_fn(&self) -> IteratorFn {
|
2020-05-19 13:03:06 +02:00
|
|
|
match self {
|
2020-05-20 13:27:23 +02:00
|
|
|
Self::Iterator(f) => *f,
|
2020-05-19 16:25:57 +02:00
|
|
|
Self::Pure(_) | Self::Method(_) | Self::Script(_) => panic!(),
|
2020-05-19 13:03:06 +02:00
|
|
|
}
|
2020-05-11 07:36:50 +02:00
|
|
|
}
|
2020-05-19 16:25:57 +02:00
|
|
|
/// Create a new `CallableFunction::Pure`.
|
|
|
|
pub fn from_pure(func: Box<FnAny>) -> Self {
|
|
|
|
Self::Pure(func.into())
|
|
|
|
}
|
|
|
|
/// Create a new `CallableFunction::Method`.
|
|
|
|
pub fn from_method(func: Box<FnAny>) -> Self {
|
|
|
|
Self::Method(func.into())
|
|
|
|
}
|
2020-05-11 07:36:50 +02:00
|
|
|
}
|