2016-02-29 22:43:45 +01:00
|
|
|
use std::any::Any;
|
|
|
|
use std::boxed::Box;
|
|
|
|
|
2016-03-03 15:31:42 +01:00
|
|
|
use engine::{EvalAltResult, Engine, FnType};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
pub trait FnRegister<A, RetVal, Args> {
|
|
|
|
fn register_fn(&mut self, name: &str, f: A);
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V, W, X, Y, Z> FnRegister<A, Z, (&'a mut T, U, V, W, X, Y)> for Engine
|
2016-04-14 03:01:08 +02:00
|
|
|
where A: 'static+Fn(&mut T, U, V, W, X, Y) -> Z, T: Any, U: Clone+Any, V: Clone+Any, W: Clone+Any,
|
2016-03-26 18:46:28 +01:00
|
|
|
X: Clone+Any, Y: Clone+Any, Z: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>,
|
|
|
|
&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
|
|
|
|
2016-03-02 16:49:24 +01:00
|
|
|
Box::new(
|
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>,
|
|
|
|
arg5: &mut Box<Any>, arg6: &mut Box<Any>| {
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-02 16:49:24 +01:00
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
|
|
|
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
|
|
|
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
|
|
|
let inside5 = (*arg5).downcast_mut() as Option<&mut X>;
|
|
|
|
let inside6 = (*arg6).downcast_mut() as Option<&mut Y>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-02 16:49:24 +01:00
|
|
|
match (inside1, inside2, inside3, inside4, inside5, inside6) {
|
2016-04-14 03:01:08 +02:00
|
|
|
(Some(b), Some(c), Some(d), Some(e), Some(f), Some(g)) => Ok(Box::new(fun(b, c.clone(), d.clone(),
|
2016-03-02 16:49:24 +01:00
|
|
|
e.clone(), f.clone(), g.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn6(wrapped));
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V, W, X, Y, Z> FnRegister<A, Z, (&'a T, U, V, W, X, Y)> for Engine
|
2016-04-14 03:01:08 +02:00
|
|
|
where A: 'static+Fn(T, U, V, W, X, Y) -> Z, T: Clone+Any, U: Clone+Any, V: Clone+Any, W: Clone+Any,
|
2016-03-26 18:46:28 +01:00
|
|
|
X: Clone+Any, Y: Clone+Any, Z: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>,
|
|
|
|
&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
|
|
|
|
2016-03-02 16:49:24 +01:00
|
|
|
Box::new(
|
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>,
|
|
|
|
arg5: &mut Box<Any>, arg6: &mut Box<Any>| {
|
|
|
|
|
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
|
|
|
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
|
|
|
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
|
|
|
let inside5 = (*arg5).downcast_mut() as Option<&mut X>;
|
|
|
|
let inside6 = (*arg6).downcast_mut() as Option<&mut Y>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-02 16:49:24 +01:00
|
|
|
match (inside1, inside2, inside3, inside4, inside5, inside6) {
|
2016-04-14 03:01:08 +02:00
|
|
|
(Some(b), Some(c), Some(d), Some(e), Some(f), Some(g)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone(),
|
2016-03-02 16:49:24 +01:00
|
|
|
e.clone(), f.clone(), g.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn6(wrapped));
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V, W, X, Y> FnRegister<A, Y, (&'a mut T, U, V, W, X)> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn(&mut T, U, V, W, X) -> Y, T: Any, U: Clone+Any, V: Clone+Any, W: Clone+Any, X: Clone+Any, Y: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-03-02 16:49:24 +01:00
|
|
|
Box::new(
|
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>,
|
|
|
|
arg5: &mut Box<Any>| {
|
|
|
|
|
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
|
|
|
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
|
|
|
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
|
|
|
let inside5 = (*arg5).downcast_mut() as Option<&mut X>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-02 16:49:24 +01:00
|
|
|
match (inside1, inside2, inside3, inside4, inside5) {
|
2016-04-14 03:01:08 +02:00
|
|
|
(Some(b), Some(c), Some(d), Some(e), Some(f)) => Ok(Box::new(fun(b, c.clone(), d.clone(),
|
2016-03-02 16:49:24 +01:00
|
|
|
e.clone(), f.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn5(wrapped));
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V, W, X, Y> FnRegister<A, Y, (&'a T, U, V, W, X)> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn(T, U, V, W, X) -> Y, T: Clone+Any, U: Clone+Any, V: Clone+Any, W: Clone+Any, X: Clone+Any, Y: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-03-02 16:49:24 +01:00
|
|
|
Box::new(
|
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>,
|
|
|
|
arg5: &mut Box<Any>| {
|
|
|
|
|
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
|
|
|
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
|
|
|
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
|
|
|
let inside5 = (*arg5).downcast_mut() as Option<&mut X>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-02 16:49:24 +01:00
|
|
|
match (inside1, inside2, inside3, inside4, inside5) {
|
2016-04-14 03:01:08 +02:00
|
|
|
(Some(b), Some(c), Some(d), Some(e), Some(f)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone(),
|
2016-03-02 16:49:24 +01:00
|
|
|
e.clone(), f.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn5(wrapped));
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V, W, X> FnRegister<A, X, (&'a mut T, U, V, W)> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn(&mut T, U, V, W) -> X, T: Any, U: Clone+Any, V: Clone+Any, W: Clone+Any, X: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-03-02 16:49:24 +01:00
|
|
|
Box::new(
|
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>| {
|
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
|
|
|
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
|
|
|
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-02 16:49:24 +01:00
|
|
|
match (inside1, inside2, inside3, inside4) {
|
2016-03-16 23:07:08 +01:00
|
|
|
(Some(b), Some(c), Some(d), Some(e)) => Ok(Box::new(fun(b, c.clone(), d.clone(), e.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn4(wrapped));
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V, W, X> FnRegister<A, X, (&'a T, U, V, W)> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn(T, U, V, W) -> X, T: Clone+Any, U: Clone+Any, V: Clone+Any, W: Clone+Any, X: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-03-02 16:49:24 +01:00
|
|
|
Box::new(
|
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>, arg4: &mut Box<Any>| {
|
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
|
|
|
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
|
|
|
let inside4 = (*arg4).downcast_mut() as Option<&mut W>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-02 16:49:24 +01:00
|
|
|
match (inside1, inside2, inside3, inside4) {
|
2016-03-16 23:07:08 +01:00
|
|
|
(Some(b), Some(c), Some(d), Some(e)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone(), e.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn4(wrapped));
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V, W> FnRegister<A, W, (&'a mut T, U, V)> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn(&mut T, U, V) -> W, T: Any, U: Clone+Any, V: Clone+Any, W: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-03-01 21:16:10 +01:00
|
|
|
Box::new(
|
2016-03-02 16:49:24 +01:00
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>| {
|
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
|
|
|
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-01 21:16:10 +01:00
|
|
|
match (inside1, inside2, inside3) {
|
2016-03-16 23:07:08 +01:00
|
|
|
(Some(b), Some(c), Some(d)) => Ok(Box::new(fun(b, c.clone(), d.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn3(wrapped));
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V, W> FnRegister<A, W, (&'a T, U, V)> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn(T, U, V) -> W, T: Clone+Any, U: Clone+Any, V: Clone+Any, W: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-03-01 21:16:10 +01:00
|
|
|
Box::new(
|
2016-03-02 16:49:24 +01:00
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>, arg3: &mut Box<Any>| {
|
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
|
|
|
let inside3 = (*arg3).downcast_mut() as Option<&mut V>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-01 21:16:10 +01:00
|
|
|
match (inside1, inside2, inside3) {
|
2016-03-16 23:07:08 +01:00
|
|
|
(Some(b), Some(c), Some(d)) => Ok(Box::new(fun(b.clone(), c.clone(), d.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn3(wrapped));
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V> FnRegister<A, V, (&'a mut T, U)> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn(&mut T, U) -> V, T: Any, U: Clone+Any, V: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-03-01 15:40:48 +01:00
|
|
|
Box::new(
|
2016-03-02 16:49:24 +01:00
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>| {
|
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-01 15:40:48 +01:00
|
|
|
match (inside1, inside2) {
|
2016-03-16 23:07:08 +01:00
|
|
|
(Some(b), Some(c)) => Ok(Box::new(fun(b, c.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-01 15:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn2(wrapped));
|
2016-03-01 15:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U, V> FnRegister<A, V, (&'a T, U)> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn(T, U) -> V, T: Clone+Any, U: Clone+Any, V: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>, &mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-03-01 15:40:48 +01:00
|
|
|
Box::new(
|
2016-03-02 16:49:24 +01:00
|
|
|
move |arg1: &mut Box<Any>, arg2: &mut Box<Any>| {
|
|
|
|
let inside1 = (*arg1).downcast_mut() as Option<&mut T>;
|
|
|
|
let inside2 = (*arg2).downcast_mut() as Option<&mut U>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-03-01 15:40:48 +01:00
|
|
|
match (inside1, inside2) {
|
2016-03-16 23:07:08 +01:00
|
|
|
(Some(b), Some(c)) => Ok(Box::new(fun(b.clone(), c.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
_ => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-03-01 15:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn2(wrapped));
|
2016-03-01 15:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<'a, A, T, U> FnRegister<A, U, (&'a mut T)> for Engine
|
2016-04-14 03:01:08 +02:00
|
|
|
where A: 'static+Fn(&mut T) -> U, T: Any, U: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-02-29 22:43:45 +01:00
|
|
|
Box::new(
|
2016-03-02 16:49:24 +01:00
|
|
|
move |arg: &mut Box<Any>| {
|
|
|
|
let inside = (*arg).downcast_mut() as Option<&mut T>;
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2016-02-29 22:43:45 +01:00
|
|
|
match inside {
|
2016-03-16 23:07:08 +01:00
|
|
|
Some(b) => Ok(Box::new(fun(b)) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
None => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn1(wrapped));
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
|
|
|
|
impl<'a, A, T, U> FnRegister<A, U, (&'a T)> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn(T) -> U, T: Clone+Any, U: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn(&mut Box<Any>)->Result<Box<Any>, EvalAltResult>> =
|
2016-02-29 22:43:45 +01:00
|
|
|
Box::new(
|
2016-03-02 16:49:24 +01:00
|
|
|
move |arg: &mut Box<Any>| {
|
|
|
|
let inside = (*arg).downcast_mut() as Option<&mut T>;
|
2016-02-29 22:43:45 +01:00
|
|
|
match inside {
|
2016-03-16 23:07:08 +01:00
|
|
|
Some(b) => Ok(Box::new(fun(b.clone())) as Box<Any>),
|
2016-03-03 15:31:42 +01:00
|
|
|
None => Err(EvalAltResult::ErrorFunctionArgMismatch)
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn1(wrapped));
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
impl<A, T> FnRegister<A, T, ()> for Engine
|
2016-03-26 18:46:28 +01:00
|
|
|
where A: 'static+Fn() -> T, T: Any
|
2016-03-16 23:07:08 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, fun: A) {
|
2016-04-14 03:01:08 +02:00
|
|
|
let wrapped : Box<Fn()->Result<Box<Any>, EvalAltResult>> =
|
2016-02-29 22:43:45 +01:00
|
|
|
Box::new(
|
2016-03-16 23:07:08 +01:00
|
|
|
move || { Ok(Box::new(fun()) as Box<Any>) }
|
2016-02-29 22:43:45 +01:00
|
|
|
);
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
let ent = self.fns.entry(name.to_string()).or_insert(Vec::new());
|
2016-03-02 20:14:22 +01:00
|
|
|
(*ent).push(FnType::ExternalFn0(wrapped));
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|