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

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