rhai/src/fn_native.rs

190 lines
5.6 KiB
Rust
Raw Normal View History

2020-05-11 07:36:50 +02:00
use crate::any::Dynamic;
use crate::parser::ScriptFnDef;
2020-05-11 07:36:50 +02:00
use crate::result::EvalAltResult;
use crate::stdlib::{boxed::Box, fmt, rc::Rc, sync::Arc};
2020-05-11 07:36:50 +02:00
2020-05-12 10:32:22 +02:00
#[cfg(feature = "sync")]
2020-05-21 11:11:01 +02:00
pub trait SendSync: Send + Sync {}
2020-05-12 10:32:22 +02:00
#[cfg(feature = "sync")]
2020-05-21 11:11:01 +02:00
impl<T: Send + Sync> SendSync for T {}
2020-05-12 10:32:22 +02:00
#[cfg(not(feature = "sync"))]
2020-05-21 11:11:01 +02:00
pub trait SendSync {}
2020-05-12 10:32:22 +02:00
#[cfg(not(feature = "sync"))]
2020-05-21 11:11:01 +02:00
impl<T> SendSync for T {}
2020-05-12 10:32:22 +02:00
#[cfg(not(feature = "sync"))]
2020-05-21 11:11:01 +02:00
pub type Shared<T> = Rc<T>;
2020-05-12 10:32:22 +02:00
#[cfg(feature = "sync")]
2020-05-21 11:11:01 +02:00
pub type Shared<T> = Arc<T>;
2020-05-12 10:32:22 +02:00
2020-05-26 08:14:03 +02:00
/// Consume a `Shared` resource and return a mutable reference to the wrapped value.
/// If the resource is shared (i.e. has other outstanding references), a cloned copy is used.
2020-05-25 11:01:39 +02:00
pub fn shared_make_mut<T: Clone>(value: &mut Shared<T>) -> &mut T {
#[cfg(not(feature = "sync"))]
{
Rc::make_mut(value)
}
#[cfg(feature = "sync")]
{
Arc::make_mut(value)
}
}
2020-05-26 08:14:03 +02:00
/// Consume a `Shared` resource, assuming that it is unique (i.e. not shared).
///
/// # Panics
///
/// Panics if the resource is shared (i.e. has other outstanding references).
pub fn shared_take<T: Clone>(value: Shared<T>) -> T {
2020-05-25 11:01:39 +02:00
#[cfg(not(feature = "sync"))]
{
2020-05-26 08:14:03 +02:00
Rc::try_unwrap(value).map_err(|_| ()).unwrap()
2020-05-25 11:01:39 +02:00
}
#[cfg(feature = "sync")]
{
2020-05-26 08:14:03 +02:00
Arc::try_unwrap(value).map_err(|_| ()).unwrap()
2020-05-25 11:01:39 +02:00
}
}
2020-05-21 11:11:01 +02:00
pub type FnCallArgs<'a> = [&'a mut Dynamic];
2020-05-12 10:32:22 +02:00
2020-05-19 16:25:57 +02:00
#[cfg(not(feature = "sync"))]
2020-05-21 11:11:01 +02:00
pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>>;
2020-05-19 16:25:57 +02:00
#[cfg(feature = "sync")]
2020-05-21 11:11:01 +02:00
pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync;
2020-05-19 16:25:57 +02:00
2020-05-21 11:11:01 +02:00
pub type IteratorFn = fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
2020-05-19 16:25:57 +02:00
2020-06-02 07:33:16 +02:00
#[cfg(not(feature = "sync"))]
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + 'static>;
#[cfg(feature = "sync")]
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + Send + Sync + 'static>;
/// A type encapsulating a function callable by Rhai.
2020-05-19 16:25:57 +02:00
#[derive(Clone)]
pub enum CallableFunction {
/// A pure native Rust function with all arguments passed by value.
2020-05-21 11:11:01 +02:00
Pure(Shared<FnAny>),
/// A native Rust object method with the first argument passed by reference,
/// and the rest passed by value.
2020-05-21 11:11:01 +02:00
Method(Shared<FnAny>),
/// An iterator function.
2020-05-20 13:27:23 +02:00
Iterator(IteratorFn),
/// A script-defined function.
Script(Shared<ScriptFnDef>),
}
impl fmt::Debug for CallableFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Pure(_) => write!(f, "NativePureFunction"),
Self::Method(_) => write!(f, "NativeMethod"),
Self::Iterator(_) => write!(f, "NativeIterator"),
Self::Script(fn_def) => write!(f, "{:?}", fn_def),
}
}
}
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,
}
}
/// 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-11 07:36:50 +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-11 07:36:50 +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,
}
}
/// 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 {
match self {
2020-05-19 16:25:57 +02:00
Self::Pure(f) | Self::Method(f) => f.as_ref(),
Self::Iterator(_) | Self::Script(_) => panic!(),
}
}
/// Get a shared reference to a script-defined function definition.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Script`.
pub fn get_shared_fn_def(&self) -> Shared<ScriptFnDef> {
match self {
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => panic!(),
Self::Script(f) => f.clone(),
}
}
/// Get a reference to a script-defined function definition.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Script`.
pub fn get_fn_def(&self) -> &ScriptFnDef {
match self {
2020-05-19 16:25:57 +02:00
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => panic!(),
Self::Script(f) => f,
}
}
/// 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 {
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-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
}
impl From<IteratorFn> for CallableFunction {
fn from(func: IteratorFn) -> Self {
Self::Iterator(func)
}
}
impl From<ScriptFnDef> for CallableFunction {
fn from(func: ScriptFnDef) -> Self {
Self::Script(func.into())
}
}
impl From<Shared<ScriptFnDef>> for CallableFunction {
fn from(func: Shared<ScriptFnDef>) -> Self {
Self::Script(func)
}
}