Move CallableFunction to own file.
This commit is contained in:
parent
57d7985015
commit
b4756b4575
@ -1,6 +1,7 @@
|
|||||||
//! Implement function-calling mechanism for [`Engine`].
|
//! Implement function-calling mechanism for [`Engine`].
|
||||||
|
|
||||||
use super::native::{CallableFunction, FnAny};
|
use super::callable_function::CallableFunction;
|
||||||
|
use super::native::FnAny;
|
||||||
use super::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn};
|
use super::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn};
|
||||||
use crate::api::default_limits::MAX_DYNAMIC_PARAMETERS;
|
use crate::api::default_limits::MAX_DYNAMIC_PARAMETERS;
|
||||||
use crate::ast::FnCallHashes;
|
use crate::ast::FnCallHashes;
|
||||||
|
250
src/func/callable_function.rs
Normal file
250
src/func/callable_function.rs
Normal file
@ -0,0 +1,250 @@
|
|||||||
|
//! Module defining the standard Rhai function type.
|
||||||
|
|
||||||
|
use super::native::{FnAny, FnPlugin, IteratorFn, SendSync};
|
||||||
|
use crate::ast::FnAccess;
|
||||||
|
use crate::plugin::PluginFunction;
|
||||||
|
use crate::Shared;
|
||||||
|
use std::fmt;
|
||||||
|
#[cfg(feature = "no_std")]
|
||||||
|
use std::prelude::v1::*;
|
||||||
|
|
||||||
|
/// A type encapsulating a function callable by Rhai.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum CallableFunction {
|
||||||
|
/// A pure native Rust function with all arguments passed by value.
|
||||||
|
Pure(Shared<FnAny>),
|
||||||
|
/// A native Rust object method with the first argument passed by reference,
|
||||||
|
/// and the rest passed by value.
|
||||||
|
Method(Shared<FnAny>),
|
||||||
|
/// An iterator function.
|
||||||
|
Iterator(IteratorFn),
|
||||||
|
/// A plugin function,
|
||||||
|
Plugin(Shared<FnPlugin>),
|
||||||
|
/// A script-defined function.
|
||||||
|
///
|
||||||
|
/// Not available under `no_function`.
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Script(Shared<crate::ast::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::Plugin(_) => write!(f, "PluginFunction"),
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(fn_def) => fmt::Debug::fmt(fn_def, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display 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::Plugin(_) => write!(f, "PluginFunction"),
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
CallableFunction::Script(s) => fmt::Display::fmt(s, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CallableFunction {
|
||||||
|
/// Is this a pure native Rust function?
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn is_pure(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Pure(_) => true,
|
||||||
|
Self::Method(_) | Self::Iterator(_) => false,
|
||||||
|
|
||||||
|
Self::Plugin(p) => !p.is_method_call(),
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Is this a native Rust method function?
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn is_method(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Method(_) => true,
|
||||||
|
Self::Pure(_) | Self::Iterator(_) => false,
|
||||||
|
|
||||||
|
Self::Plugin(p) => p.is_method_call(),
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Is this an iterator function?
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub const fn is_iter(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Iterator(_) => true,
|
||||||
|
Self::Pure(_) | Self::Method(_) | Self::Plugin(_) => false,
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Is this a script-defined function?
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub const fn is_script(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(_) => true,
|
||||||
|
|
||||||
|
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Is this a plugin function?
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub const fn is_plugin_fn(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Plugin(_) => true,
|
||||||
|
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => false,
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Is this a native Rust function?
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub const fn is_native(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Pure(_) | Self::Method(_) => true,
|
||||||
|
Self::Plugin(_) => true,
|
||||||
|
Self::Iterator(_) => true,
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Get the access mode.
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn access(&self) -> FnAccess {
|
||||||
|
match self {
|
||||||
|
Self::Plugin(_) => FnAccess::Public,
|
||||||
|
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => FnAccess::Public,
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(f) => f.access,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Get a shared reference to a native Rust function.
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn get_native_fn(&self) -> Option<&Shared<FnAny>> {
|
||||||
|
match self {
|
||||||
|
Self::Pure(f) | Self::Method(f) => Some(f),
|
||||||
|
Self::Iterator(_) | Self::Plugin(_) => None,
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Get a shared reference to a script-defined function definition.
|
||||||
|
///
|
||||||
|
/// Not available under `no_function`.
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub const fn get_script_fn_def(&self) -> Option<&Shared<crate::ast::ScriptFnDef>> {
|
||||||
|
match self {
|
||||||
|
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => None,
|
||||||
|
Self::Script(f) => Some(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Get a reference to an iterator function.
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn get_iter_fn(&self) -> Option<IteratorFn> {
|
||||||
|
match self {
|
||||||
|
Self::Iterator(f) => Some(*f),
|
||||||
|
Self::Pure(_) | Self::Method(_) | Self::Plugin(_) => None,
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Get a shared reference to a plugin function.
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn get_plugin_fn(&self) -> Option<&Shared<FnPlugin>> {
|
||||||
|
match self {
|
||||||
|
Self::Plugin(f) => Some(f),
|
||||||
|
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => None,
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
Self::Script(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Create a new [`CallableFunction::Pure`].
|
||||||
|
#[inline(always)]
|
||||||
|
#[must_use]
|
||||||
|
pub fn from_pure(func: Box<FnAny>) -> Self {
|
||||||
|
Self::Pure(func.into())
|
||||||
|
}
|
||||||
|
/// Create a new [`CallableFunction::Method`].
|
||||||
|
#[inline(always)]
|
||||||
|
#[must_use]
|
||||||
|
pub fn from_method(func: Box<FnAny>) -> Self {
|
||||||
|
Self::Method(func.into())
|
||||||
|
}
|
||||||
|
/// Create a new [`CallableFunction::Plugin`].
|
||||||
|
#[inline(always)]
|
||||||
|
#[must_use]
|
||||||
|
pub fn from_plugin(func: impl PluginFunction + 'static + SendSync) -> Self {
|
||||||
|
Self::Plugin((Box::new(func) as Box<FnPlugin>).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<IteratorFn> for CallableFunction {
|
||||||
|
#[inline(always)]
|
||||||
|
fn from(func: IteratorFn) -> Self {
|
||||||
|
Self::Iterator(func)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
impl From<crate::ast::ScriptFnDef> for CallableFunction {
|
||||||
|
#[inline(always)]
|
||||||
|
fn from(_func: crate::ast::ScriptFnDef) -> Self {
|
||||||
|
Self::Script(_func.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
impl From<Shared<crate::ast::ScriptFnDef>> for CallableFunction {
|
||||||
|
#[inline(always)]
|
||||||
|
fn from(_func: Shared<crate::ast::ScriptFnDef>) -> Self {
|
||||||
|
Self::Script(_func)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: PluginFunction + 'static + SendSync> From<T> for CallableFunction {
|
||||||
|
#[inline(always)]
|
||||||
|
fn from(func: T) -> Self {
|
||||||
|
Self::from_plugin(func)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Shared<FnPlugin>> for CallableFunction {
|
||||||
|
#[inline(always)]
|
||||||
|
fn from(func: Shared<FnPlugin>) -> Self {
|
||||||
|
Self::Plugin(func)
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
pub mod args;
|
pub mod args;
|
||||||
pub mod builtin;
|
pub mod builtin;
|
||||||
pub mod call;
|
pub mod call;
|
||||||
|
pub mod callable_function;
|
||||||
pub mod func;
|
pub mod func;
|
||||||
pub mod hashing;
|
pub mod hashing;
|
||||||
pub mod native;
|
pub mod native;
|
||||||
@ -13,6 +14,7 @@ pub mod script;
|
|||||||
pub use args::FuncArgs;
|
pub use args::FuncArgs;
|
||||||
pub use builtin::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn};
|
pub use builtin::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn};
|
||||||
pub use call::FnCallArgs;
|
pub use call::FnCallArgs;
|
||||||
|
pub use callable_function::CallableFunction;
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
pub use func::Func;
|
pub use func::Func;
|
||||||
pub use hashing::{
|
pub use hashing::{
|
||||||
@ -20,8 +22,8 @@ pub use hashing::{
|
|||||||
combine_hashes, get_hasher,
|
combine_hashes, get_hasher,
|
||||||
};
|
};
|
||||||
pub use native::{
|
pub use native::{
|
||||||
shared_make_mut, shared_take, shared_take_or_clone, shared_try_take, shared_write_lock,
|
shared_make_mut, shared_take, shared_take_or_clone, shared_try_take, shared_write_lock, FnAny,
|
||||||
CallableFunction, FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared,
|
FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared,
|
||||||
};
|
};
|
||||||
pub use plugin::PluginFunction;
|
pub use plugin::PluginFunction;
|
||||||
pub use register::RegisterNativeFunction;
|
pub use register::RegisterNativeFunction;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Module defining interfaces to native-Rust functions.
|
//! Module defining interfaces to native-Rust functions.
|
||||||
|
|
||||||
use super::call::FnCallArgs;
|
use super::call::FnCallArgs;
|
||||||
use crate::ast::{FnAccess, FnCallHashes};
|
use crate::ast::FnCallHashes;
|
||||||
use crate::engine::{EvalState, Imports};
|
use crate::engine::{EvalState, Imports};
|
||||||
use crate::plugin::PluginFunction;
|
use crate::plugin::PluginFunction;
|
||||||
use crate::tokenizer::{Token, TokenizeState};
|
use crate::tokenizer::{Token, TokenizeState};
|
||||||
@ -10,9 +10,9 @@ use crate::{
|
|||||||
calc_fn_hash, Dynamic, Engine, EvalAltResult, EvalContext, FuncArgs, Module, Position,
|
calc_fn_hash, Dynamic, Engine, EvalAltResult, EvalContext, FuncArgs, Module, Position,
|
||||||
RhaiResult, StaticVec,
|
RhaiResult, StaticVec,
|
||||||
};
|
};
|
||||||
|
use std::any::type_name;
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
use std::{any::type_name, fmt};
|
|
||||||
|
|
||||||
/// Trait that maps to `Send + Sync` only under the `sync` feature.
|
/// Trait that maps to `Send + Sync` only under the `sync` feature.
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
@ -414,244 +414,3 @@ pub type OnVarCallback = Box<
|
|||||||
+ Sync
|
+ Sync
|
||||||
+ 'static,
|
+ 'static,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
/// A type encapsulating a function callable by Rhai.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum CallableFunction {
|
|
||||||
/// A pure native Rust function with all arguments passed by value.
|
|
||||||
Pure(Shared<FnAny>),
|
|
||||||
/// A native Rust object method with the first argument passed by reference,
|
|
||||||
/// and the rest passed by value.
|
|
||||||
Method(Shared<FnAny>),
|
|
||||||
/// An iterator function.
|
|
||||||
Iterator(IteratorFn),
|
|
||||||
/// A plugin function,
|
|
||||||
Plugin(Shared<FnPlugin>),
|
|
||||||
/// A script-defined function.
|
|
||||||
///
|
|
||||||
/// Not available under `no_function`.
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Script(Shared<crate::ast::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::Plugin(_) => write!(f, "PluginFunction"),
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(fn_def) => fmt::Debug::fmt(fn_def, f),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display 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::Plugin(_) => write!(f, "PluginFunction"),
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
CallableFunction::Script(s) => fmt::Display::fmt(s, f),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CallableFunction {
|
|
||||||
/// Is this a pure native Rust function?
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn is_pure(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::Pure(_) => true,
|
|
||||||
Self::Method(_) | Self::Iterator(_) => false,
|
|
||||||
|
|
||||||
Self::Plugin(p) => !p.is_method_call(),
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(_) => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Is this a native Rust method function?
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn is_method(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::Method(_) => true,
|
|
||||||
Self::Pure(_) | Self::Iterator(_) => false,
|
|
||||||
|
|
||||||
Self::Plugin(p) => p.is_method_call(),
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(_) => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Is this an iterator function?
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub const fn is_iter(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::Iterator(_) => true,
|
|
||||||
Self::Pure(_) | Self::Method(_) | Self::Plugin(_) => false,
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(_) => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Is this a script-defined function?
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub const fn is_script(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(_) => true,
|
|
||||||
|
|
||||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Is this a plugin function?
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub const fn is_plugin_fn(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::Plugin(_) => true,
|
|
||||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => false,
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(_) => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Is this a native Rust function?
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub const fn is_native(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::Pure(_) | Self::Method(_) => true,
|
|
||||||
Self::Plugin(_) => true,
|
|
||||||
Self::Iterator(_) => true,
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(_) => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Get the access mode.
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn access(&self) -> FnAccess {
|
|
||||||
match self {
|
|
||||||
Self::Plugin(_) => FnAccess::Public,
|
|
||||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => FnAccess::Public,
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(f) => f.access,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Get a shared reference to a native Rust function.
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn get_native_fn(&self) -> Option<&Shared<FnAny>> {
|
|
||||||
match self {
|
|
||||||
Self::Pure(f) | Self::Method(f) => Some(f),
|
|
||||||
Self::Iterator(_) | Self::Plugin(_) => None,
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(_) => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Get a shared reference to a script-defined function definition.
|
|
||||||
///
|
|
||||||
/// Not available under `no_function`.
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub const fn get_script_fn_def(&self) -> Option<&Shared<crate::ast::ScriptFnDef>> {
|
|
||||||
match self {
|
|
||||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => None,
|
|
||||||
Self::Script(f) => Some(f),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Get a reference to an iterator function.
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn get_iter_fn(&self) -> Option<IteratorFn> {
|
|
||||||
match self {
|
|
||||||
Self::Iterator(f) => Some(*f),
|
|
||||||
Self::Pure(_) | Self::Method(_) | Self::Plugin(_) => None,
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(_) => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Get a shared reference to a plugin function.
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn get_plugin_fn(&self) -> Option<&Shared<FnPlugin>> {
|
|
||||||
match self {
|
|
||||||
Self::Plugin(f) => Some(f),
|
|
||||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => None,
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
Self::Script(_) => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Create a new [`CallableFunction::Pure`].
|
|
||||||
#[inline(always)]
|
|
||||||
#[must_use]
|
|
||||||
pub fn from_pure(func: Box<FnAny>) -> Self {
|
|
||||||
Self::Pure(func.into())
|
|
||||||
}
|
|
||||||
/// Create a new [`CallableFunction::Method`].
|
|
||||||
#[inline(always)]
|
|
||||||
#[must_use]
|
|
||||||
pub fn from_method(func: Box<FnAny>) -> Self {
|
|
||||||
Self::Method(func.into())
|
|
||||||
}
|
|
||||||
/// Create a new [`CallableFunction::Plugin`].
|
|
||||||
#[inline(always)]
|
|
||||||
#[must_use]
|
|
||||||
pub fn from_plugin(func: impl PluginFunction + 'static + SendSync) -> Self {
|
|
||||||
Self::Plugin((Box::new(func) as Box<FnPlugin>).into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<IteratorFn> for CallableFunction {
|
|
||||||
#[inline(always)]
|
|
||||||
fn from(func: IteratorFn) -> Self {
|
|
||||||
Self::Iterator(func)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
impl From<crate::ast::ScriptFnDef> for CallableFunction {
|
|
||||||
#[inline(always)]
|
|
||||||
fn from(_func: crate::ast::ScriptFnDef) -> Self {
|
|
||||||
Self::Script(_func.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
impl From<Shared<crate::ast::ScriptFnDef>> for CallableFunction {
|
|
||||||
#[inline(always)]
|
|
||||||
fn from(_func: Shared<crate::ast::ScriptFnDef>) -> Self {
|
|
||||||
Self::Script(_func)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: PluginFunction + 'static + SendSync> From<T> for CallableFunction {
|
|
||||||
#[inline(always)]
|
|
||||||
fn from(func: T) -> Self {
|
|
||||||
Self::from_plugin(func)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Shared<FnPlugin>> for CallableFunction {
|
|
||||||
#[inline(always)]
|
|
||||||
fn from(func: Shared<FnPlugin>) -> Self {
|
|
||||||
Self::Plugin(func)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
use crate::func::call::FnCallArgs;
|
use super::call::FnCallArgs;
|
||||||
use crate::func::native::{CallableFunction, FnAny, SendSync};
|
use super::callable_function::CallableFunction;
|
||||||
|
use super::native::{FnAny, SendSync};
|
||||||
use crate::r#unsafe::unsafe_try_cast;
|
use crate::r#unsafe::unsafe_try_cast;
|
||||||
use crate::tokenizer::Position;
|
use crate::tokenizer::Position;
|
||||||
use crate::types::dynamic::{DynamicWriteLock, Variant};
|
use crate::types::dynamic::{DynamicWriteLock, Variant};
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
//! Implement script function-calling mechanism for [`Engine`].
|
//! Implement script function-calling mechanism for [`Engine`].
|
||||||
#![cfg(not(feature = "no_function"))]
|
#![cfg(not(feature = "no_function"))]
|
||||||
|
|
||||||
|
use super::call::FnCallArgs;
|
||||||
use crate::ast::ScriptFnDef;
|
use crate::ast::ScriptFnDef;
|
||||||
use crate::engine::{EvalState, Imports};
|
use crate::engine::{EvalState, Imports};
|
||||||
use crate::func::call::FnCallArgs;
|
|
||||||
use crate::r#unsafe::unsafe_cast_var_name_to_lifetime;
|
use crate::r#unsafe::unsafe_cast_var_name_to_lifetime;
|
||||||
use crate::{Dynamic, Engine, EvalAltResult, Module, Position, RhaiResult, Scope, StaticVec};
|
use crate::{Dynamic, Engine, EvalAltResult, Module, Position, RhaiResult, Scope, StaticVec};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
@ -611,14 +611,11 @@ impl fmt::Display for Dynamic {
|
|||||||
#[cfg(feature = "decimal")]
|
#[cfg(feature = "decimal")]
|
||||||
Union::Decimal(ref value, _, _) => fmt::Display::fmt(value, f),
|
Union::Decimal(ref value, _, _) => fmt::Display::fmt(value, f),
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Union::Array(ref value, _, _) => fmt::Debug::fmt(value, f),
|
Union::Array(_, _, _) => fmt::Debug::fmt(self, f),
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Union::Blob(ref value, _, _) => fmt::Debug::fmt(value, f),
|
Union::Blob(_, _, _) => fmt::Debug::fmt(self, f),
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
Union::Map(ref value, _, _) => {
|
Union::Map(_, _, _) => fmt::Debug::fmt(self, f),
|
||||||
f.write_str("#")?;
|
|
||||||
fmt::Debug::fmt(value, f)
|
|
||||||
}
|
|
||||||
Union::FnPtr(ref value, _, _) => fmt::Display::fmt(value, f),
|
Union::FnPtr(ref value, _, _) => fmt::Display::fmt(value, f),
|
||||||
#[cfg(not(feature = "no_std"))]
|
#[cfg(not(feature = "no_std"))]
|
||||||
Union::TimeStamp(_, _, _) => f.write_str("<timestamp>"),
|
Union::TimeStamp(_, _, _) => f.write_str("<timestamp>"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user