Refine currying docs.

This commit is contained in:
Stephen Chung 2020-07-23 15:49:09 +08:00
parent dc7f847a8e
commit 1a48a2d8ba
3 changed files with 36 additions and 34 deletions

View File

@ -1,14 +1,14 @@
//! Module containing interfaces with native-Rust functions. //! Module containing interfaces with native-Rust functions.
use crate::any::Dynamic; use crate::any::Dynamic;
use crate::engine::Engine; use crate::engine::Engine;
use crate::module::{Module, FuncReturn}; use crate::module::{FuncReturn, Module};
use crate::parser::ScriptFnDef; use crate::parser::ScriptFnDef;
use crate::result::EvalAltResult; use crate::result::EvalAltResult;
use crate::token::{is_valid_identifier, Position}; use crate::token::{is_valid_identifier, Position};
use crate::utils::ImmutableString; use crate::utils::{ImmutableString, StaticVec};
use crate::Scope; use crate::Scope;
use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, rc::Rc, string::String, sync::Arc}; use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, mem, rc::Rc, string::String, sync::Arc};
/// 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")]
@ -50,7 +50,8 @@ pub fn shared_take<T: Clone>(value: Shared<T>) -> T {
pub type FnCallArgs<'a> = [&'a mut Dynamic]; pub type FnCallArgs<'a> = [&'a mut Dynamic];
/// A general function pointer. /// A general function pointer, which may carry additional (i.e. curried) argument values
/// to be passed onto a function during a call.
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct FnPtr(ImmutableString, Vec<Dynamic>); pub struct FnPtr(ImmutableString, Vec<Dynamic>);
@ -71,34 +72,34 @@ impl FnPtr {
pub(crate) fn take_data(self) -> (ImmutableString, Vec<Dynamic>) { pub(crate) fn take_data(self) -> (ImmutableString, Vec<Dynamic>) {
(self.0, self.1) (self.0, self.1)
} }
/// Get the curried data. /// Get the curried arguments.
pub(crate) fn curry(&self) -> &[Dynamic] { pub fn curry(&self) -> &[Dynamic] {
&self.1 &self.1
} }
/// A shortcut of `Engine::call_fn_dynamic` function that takes into /// Call the function pointer with curried arguments (if any).
/// consideration curry-ed and passed arguments both. ///
/// ## WARNING
///
/// All the arguments are _consumed_, meaning that they're replaced by `()`.
/// This is to avoid unnecessarily cloning the arguments.
/// Do not use the arguments after this call. If they are needed afterwards,
/// clone them _before_ calling this function.
pub fn call_dynamic( pub fn call_dynamic(
&self, &self,
engine: &Engine, engine: &Engine,
lib: impl AsRef<Module>, lib: impl AsRef<Module>,
mut this_ptr: Option<&mut Dynamic>, this_ptr: Option<&mut Dynamic>,
mut arg_values: impl AsMut<[Dynamic]> mut arg_values: impl AsMut<[Dynamic]>,
) -> FuncReturn<Dynamic> { ) -> FuncReturn<Dynamic> {
let mut args: Vec<Dynamic> = self let args = self
.1 .1
.iter() .iter()
.chain(arg_values.as_mut().iter())
.cloned() .cloned()
.collect(); .chain(arg_values.as_mut().iter_mut().map(|v| mem::take(v)))
.collect::<StaticVec<_>>();
engine.call_fn_dynamic( engine.call_fn_dynamic(&mut Scope::new(), lib, self.0.as_str(), this_ptr, args)
&mut Scope::new(),
lib,
&self.0.as_ref(),
this_ptr,
args
)
} }
} }

View File

@ -17,7 +17,7 @@ use crate::stdlib::{
sync::Arc, sync::Arc,
}; };
/// A general function trail object. /// A general expression evaluation trait object.
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
pub type FnCustomSyntaxEval = dyn Fn( pub type FnCustomSyntaxEval = dyn Fn(
&Engine, &Engine,
@ -25,12 +25,13 @@ pub type FnCustomSyntaxEval = dyn Fn(
&mut Scope, &mut Scope,
&[Expression], &[Expression],
) -> Result<Dynamic, Box<EvalAltResult>>; ) -> Result<Dynamic, Box<EvalAltResult>>;
/// A general function trail object. /// A general expression evaluation trait object.
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
pub type FnCustomSyntaxEval = dyn Fn(&Engine, &mut EvalContext, &mut Scope, &[Expression]) -> Result<Dynamic, Box<EvalAltResult>> pub type FnCustomSyntaxEval = dyn Fn(&Engine, &mut EvalContext, &mut Scope, &[Expression]) -> Result<Dynamic, Box<EvalAltResult>>
+ Send + Send
+ Sync; + Sync;
/// An expression sub-tree in an AST.
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, Hash)]
pub struct Expression<'a>(&'a Expr); pub struct Expression<'a>(&'a Expr);
@ -71,6 +72,7 @@ impl fmt::Debug for CustomSyntax {
} }
} }
/// Context of a script evaluation process.
#[derive(Debug)] #[derive(Debug)]
pub struct EvalContext<'a, 'b: 'a, 's, 'm, 't, 'd: 't> { pub struct EvalContext<'a, 'b: 'a, 's, 'm, 't, 'd: 't> {
pub(crate) mods: &'a mut Imports<'b>, pub(crate) mods: &'a mut Imports<'b>,

View File

@ -3,7 +3,6 @@ use rhai::{
Dynamic, Engine, EvalAltResult, FnPtr, Func, Module, ParseError, ParseErrorType, Scope, INT, Dynamic, Engine, EvalAltResult, FnPtr, Func, Module, ParseError, ParseErrorType, Scope, INT,
}; };
use std::any::TypeId; use std::any::TypeId;
use std::rc::Rc;
#[test] #[test]
fn test_fn() -> Result<(), Box<EvalAltResult>> { fn test_fn() -> Result<(), Box<EvalAltResult>> {
@ -123,9 +122,9 @@ fn test_fn_ptr_raw() -> Result<(), Box<EvalAltResult>> {
engine.register_raw_fn( engine.register_raw_fn(
"bar", "bar",
&[ &[
std::any::TypeId::of::<INT>(), TypeId::of::<INT>(),
std::any::TypeId::of::<FnPtr>(), TypeId::of::<FnPtr>(),
std::any::TypeId::of::<INT>(), TypeId::of::<INT>(),
], ],
move |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| { move |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| {
let fp = std::mem::take(args[1]).cast::<FnPtr>(); let fp = std::mem::take(args[1]).cast::<FnPtr>();
@ -161,32 +160,32 @@ fn test_fn_ptr_raw() -> Result<(), Box<EvalAltResult>> {
} }
#[test] #[test]
fn test_currying_with_registered_fn() -> Result<(), Box<EvalAltResult>> { fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
let mut module = Module::new(); let mut module = Module::new();
module.set_raw_fn( module.set_raw_fn(
"call_with_arg", "call_with_arg",
&[TypeId::of::<FnPtr>(), TypeId::of::<INT>()], &[TypeId::of::<FnPtr>(), TypeId::of::<INT>()],
|engine: &Engine, module: &Module, args: &mut [&mut Dynamic]| { |engine: &Engine, module: &Module, args: &mut [&mut Dynamic]| {
std::mem::take(args[0]) let fn_ptr = std::mem::take(args[0]).cast::<FnPtr>();
.cast::<FnPtr>() fn_ptr.call_dynamic(engine, module, None, [std::mem::take(args[1])])
.call_dynamic(engine, module, None, [std::mem::take(args[1])])
}, },
); );
let mut engine = Engine::new(); let mut engine = Engine::new();
engine.load_package(Rc::new(module)); engine.load_package(module.into());
#[cfg(not(feature = "no_object"))]
assert_eq!( assert_eq!(
engine.eval::<INT>( engine.eval::<INT>(
r#" r#"
let addition = |x, y| { x + y }; let addition = |x, y| { x + y };
let curryed = addition.curry(100); let curried = addition.curry(2);
call_with_arg(curryed, 5) call_with_arg(curried, 40)
"# "#
)?, )?,
105 42
); );
Ok(()) Ok(())