Merge pull request #91 from jakubdabek/90_function_fn_clone
Make `Function` able to hold capturing closures. Closes #90.
This commit is contained in:
commit
3aef19e71b
@ -4,6 +4,26 @@ use crate::{error::EvalexprResult, value::Value};
|
|||||||
|
|
||||||
pub(crate) mod builtin;
|
pub(crate) mod builtin;
|
||||||
|
|
||||||
|
/// A helper trait to enable cloning through `Fn` trait objects.
|
||||||
|
trait ClonableFn
|
||||||
|
where
|
||||||
|
Self: Fn(&Value) -> EvalexprResult<Value>,
|
||||||
|
Self: Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
fn dyn_clone(&self) -> Box<dyn ClonableFn>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> ClonableFn for F
|
||||||
|
where
|
||||||
|
F: Fn(&Value) -> EvalexprResult<Value>,
|
||||||
|
F: Send + Sync + 'static,
|
||||||
|
F: Clone,
|
||||||
|
{
|
||||||
|
fn dyn_clone(&self) -> Box<dyn ClonableFn> {
|
||||||
|
Box::new(self.clone()) as _
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A user-defined function.
|
/// A user-defined function.
|
||||||
/// Functions can be used in expressions by storing them in a `Context`.
|
/// Functions can be used in expressions by storing them in a `Context`.
|
||||||
///
|
///
|
||||||
@ -18,17 +38,31 @@ pub(crate) mod builtin;
|
|||||||
/// })).unwrap(); // Do proper error handling here
|
/// })).unwrap(); // Do proper error handling here
|
||||||
/// assert_eq!(eval_with_context("id(4)", &context), Ok(Value::from(4)));
|
/// assert_eq!(eval_with_context("id(4)", &context), Ok(Value::from(4)));
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
function: fn(&Value) -> EvalexprResult<Value>,
|
function: Box<dyn ClonableFn>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for Function {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
function: self.function.dyn_clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Function {
|
impl Function {
|
||||||
/// Creates a user-defined function.
|
/// Creates a user-defined function.
|
||||||
///
|
///
|
||||||
/// The `function` is a boxed function that takes a `Value` and returns a `EvalexprResult<Value, Error>`.
|
/// The `function` is boxed for storage.
|
||||||
pub fn new(function: fn(&Value) -> EvalexprResult<Value>) -> Self {
|
pub fn new<F>(function: F) -> Self
|
||||||
Self { function }
|
where
|
||||||
|
F: Fn(&Value) -> EvalexprResult<Value>,
|
||||||
|
F: Send + Sync + 'static,
|
||||||
|
F: Clone,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
function: Box::new(function) as _,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn call(&self, argument: &Value) -> EvalexprResult<Value> {
|
pub(crate) fn call(&self, argument: &Value) -> EvalexprResult<Value> {
|
||||||
|
@ -276,6 +276,36 @@ fn test_n_ary_functions() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_capturing_functions() {
|
||||||
|
let mut context = HashMapContext::new();
|
||||||
|
// this variable is captured by the function
|
||||||
|
let three = 3;
|
||||||
|
context
|
||||||
|
.set_function(
|
||||||
|
"mult_3".into(),
|
||||||
|
Function::new(move |argument| {
|
||||||
|
if let Value::Int(int) = argument {
|
||||||
|
Ok(Value::Int(int * three))
|
||||||
|
} else if let Value::Float(float) = argument {
|
||||||
|
Ok(Value::Float(float * three as f64))
|
||||||
|
} else {
|
||||||
|
Err(EvalexprError::expected_number(argument.clone()))
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let four = 4;
|
||||||
|
context
|
||||||
|
.set_function("function_four".into(), Function::new(move |_| Ok(Value::Int(four))))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(eval_with_context("mult_3 2", &context), Ok(Value::Int(6)));
|
||||||
|
assert_eq!(eval_with_context("mult_3(3)", &context), Ok(Value::Int(9)));
|
||||||
|
assert_eq!(eval_with_context("mult_3(function_four())", &context), Ok(Value::Int(12)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_builtin_functions() {
|
fn test_builtin_functions() {
|
||||||
// Log
|
// Log
|
||||||
|
Loading…
Reference in New Issue
Block a user