Rename AnonymousFn to Func
This commit is contained in:
@@ -9,8 +9,10 @@ use crate::parser::AST;
|
||||
use crate::result::EvalAltResult;
|
||||
use crate::scope::Scope;
|
||||
|
||||
use crate::stdlib::{boxed::Box, string::ToString};
|
||||
|
||||
/// A trait to create a Rust anonymous function from a script.
|
||||
pub trait AnonymousFn<ARGS, RET> {
|
||||
pub trait Func<ARGS, RET> {
|
||||
type Output;
|
||||
|
||||
/// Create a Rust anonymous function from an `AST`.
|
||||
@@ -20,19 +22,19 @@ pub trait AnonymousFn<ARGS, RET> {
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), rhai::EvalAltResult> {
|
||||
/// use rhai::{Engine, AnonymousFn}; // use 'AnonymousFn' for 'create_from_ast'
|
||||
/// use rhai::{Engine, Func}; // use 'Func' for 'create_from_ast'
|
||||
///
|
||||
/// let engine = Engine::new(); // create a new 'Engine' just for this
|
||||
///
|
||||
/// let ast = engine.compile("fn calc(x, y) { x + y.len() < 42 }")?;
|
||||
///
|
||||
/// // AnonymousFn takes two type parameters:
|
||||
/// // Func takes two type parameters:
|
||||
/// // 1) a tuple made up of the types of the script function's parameters
|
||||
/// // 2) the return type of the script function
|
||||
/// //
|
||||
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, EvalAltResult>> and is callable!
|
||||
/// let func = AnonymousFn::<(i64, String), bool>::create_from_ast(
|
||||
/// // ^^^^^^^^^^^^^ function parameter types in tuple
|
||||
/// let func = Func::<(i64, String), bool>::create_from_ast(
|
||||
/// // ^^^^^^^^^^^^^ function parameter types in tuple
|
||||
///
|
||||
/// engine, // the 'Engine' is consumed into the closure
|
||||
/// ast, // the 'AST'
|
||||
@@ -51,19 +53,19 @@ pub trait AnonymousFn<ARGS, RET> {
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), rhai::EvalAltResult> {
|
||||
/// use rhai::{Engine, AnonymousFn}; // use 'AnonymousFn' for 'create_from_script'
|
||||
/// use rhai::{Engine, Func}; // use 'Func' for 'create_from_script'
|
||||
///
|
||||
/// let engine = Engine::new(); // create a new 'Engine' just for this
|
||||
///
|
||||
/// let script = "fn calc(x, y) { x + y.len() < 42 }";
|
||||
///
|
||||
/// // AnonymousFn takes two type parameters:
|
||||
/// // Func takes two type parameters:
|
||||
/// // 1) a tuple made up of the types of the script function's parameters
|
||||
/// // 2) the return type of the script function
|
||||
/// //
|
||||
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, EvalAltResult>> and is callable!
|
||||
/// let func = AnonymousFn::<(i64, String), bool>::create_from_script(
|
||||
/// // ^^^^^^^^^^^^^ function parameter types in tuple
|
||||
/// let func = Func::<(i64, String), bool>::create_from_script(
|
||||
/// // ^^^^^^^^^^^^^ function parameter types in tuple
|
||||
///
|
||||
/// engine, // the 'Engine' is consumed into the closure
|
||||
/// script, // the script, notice number of parameters must match
|
||||
@@ -86,7 +88,7 @@ macro_rules! def_anonymous_fn {
|
||||
def_anonymous_fn!(imp);
|
||||
};
|
||||
(imp $($par:ident),*) => {
|
||||
impl<'e, $($par: Any + Clone,)* RET: Any + Clone> AnonymousFn<($($par,)*), RET> for Engine<'e>
|
||||
impl<'e, $($par: Any + Clone,)* RET: Any + Clone> Func<($($par,)*), RET> for Engine<'e>
|
||||
{
|
||||
#[cfg(feature = "sync")]
|
||||
type Output = Box<dyn Fn($($par),*) -> Result<RET, EvalAltResult> + Send + Sync + 'e>;
|
||||
@@ -98,13 +100,13 @@ macro_rules! def_anonymous_fn {
|
||||
let name = entry_point.to_string();
|
||||
|
||||
Box::new(move |$($par: $par),*| {
|
||||
self.call_fn::<_, RET>(&mut Scope::new(), &ast, &name, ($($par,)*))
|
||||
self.call_fn(&mut Scope::new(), &ast, &name, ($($par,)*))
|
||||
})
|
||||
}
|
||||
|
||||
fn create_from_script(self, script: &str, entry_point: &str) -> Result<Self::Output, ParseError> {
|
||||
let ast = self.compile(script)?;
|
||||
Ok(AnonymousFn::<($($par,)*), RET>::create_from_ast(self, ast, entry_point))
|
||||
Ok(Func::<($($par,)*), RET>::create_from_ast(self, ast, entry_point))
|
||||
}
|
||||
}
|
||||
};
|
22
src/lib.rs
22
src/lib.rs
@@ -3,15 +3,19 @@
|
||||
//! Rhai is a tiny, simple and very fast embedded scripting language for Rust
|
||||
//! that gives you a safe and easy way to add scripting to your applications.
|
||||
//! It provides a familiar syntax based on JS and Rust and a simple Rust interface.
|
||||
//! Here is a quick example. First, the contents of `my_script.rhai`:
|
||||
//! Here is a quick example.
|
||||
//!
|
||||
//! First, the contents of `my_script.rhai`:
|
||||
//!
|
||||
//! ```,ignore
|
||||
//! // Brute force factorial function
|
||||
//! fn factorial(x) {
|
||||
//! if x == 1 { return 1; }
|
||||
//! x * factorial(x - 1)
|
||||
//! }
|
||||
//!
|
||||
//! compute_something(factorial(10))
|
||||
//! // Calling an external function 'compute'
|
||||
//! compute(factorial(10))
|
||||
//! ```
|
||||
//!
|
||||
//! And the Rust part:
|
||||
@@ -21,16 +25,22 @@
|
||||
//!
|
||||
//! fn main() -> Result<(), EvalAltResult>
|
||||
//! {
|
||||
//! // Define external function
|
||||
//! fn compute_something(x: i64) -> bool {
|
||||
//! (x % 40) == 0
|
||||
//! }
|
||||
//!
|
||||
//! // Create scripting engine
|
||||
//! let mut engine = Engine::new();
|
||||
//!
|
||||
//! engine.register_fn("compute_something", compute_something);
|
||||
//! // Register external function as 'compute'
|
||||
//! engine.register_fn("compute", compute_something);
|
||||
//!
|
||||
//! # #[cfg(not(feature = "no_std"))]
|
||||
//! assert_eq!(engine.eval_file::<bool>("my_script.rhai".into())?, true);
|
||||
//! assert_eq!(
|
||||
//! engine.eval_file::<bool>("my_script.rhai".into())?,
|
||||
//! true
|
||||
//! );
|
||||
//!
|
||||
//! Ok(())
|
||||
//! }
|
||||
@@ -63,8 +73,8 @@ mod api;
|
||||
mod builtin;
|
||||
mod engine;
|
||||
mod error;
|
||||
mod fn_anonymous;
|
||||
mod fn_call;
|
||||
mod fn_func;
|
||||
mod fn_register;
|
||||
mod optimize;
|
||||
mod parser;
|
||||
@@ -82,7 +92,7 @@ pub use result::EvalAltResult;
|
||||
pub use scope::Scope;
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
pub use fn_anonymous::AnonymousFn;
|
||||
pub use fn_func::Func;
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
pub use engine::Array;
|
||||
|
Reference in New Issue
Block a user