diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-01-17 23:54:52 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-01-17 23:54:52 +0100 |
| commit | 95ba627a9c5e4e6e1d79f7aaff1854eb831511b4 (patch) | |
| tree | 177d58d387143d644ce7ad2c97eb30032e226533 /src/function.rs | |
| parent | 9cc61497ed8a2336f33407d3262181e4ac3b46cb (diff) | |
hand context on to Function trait
Diffstat (limited to 'src/function.rs')
| -rw-r--r-- | src/function.rs | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/function.rs b/src/function.rs index 0da4574..126477c 100644 --- a/src/function.rs +++ b/src/function.rs @@ -1,4 +1,4 @@ -use crate::complex::Complex; +use crate::{complex::Complex, context::Context}; pub struct FunctionArgument { args : Vec<Complex>, @@ -23,16 +23,16 @@ impl FunctionArgument { } pub trait Function { - fn eval(&self, args: FunctionArgument) -> Result<Complex, String>; + fn eval(&self, args: FunctionArgument, ctx: &Context) -> Result<Complex, String>; } #[macro_export] macro_rules! functions { {$($x:expr => $y:expr), *} => { { - let mut h : HashMap<String, Box<dyn Function>> = HashMap::new(); + let mut h : HashMap<String, Arc<dyn Function>> = HashMap::new(); $( - h.insert($x.to_string(), Box::new($y)); + h.insert($x.to_string(), Arc::new($y)); )* h } @@ -45,7 +45,7 @@ impl<T> Function for T where T: Fn(Complex) -> Complex, { - fn eval(&self, args: FunctionArgument) -> Result<Complex, String> { + fn eval(&self, args: FunctionArgument, _ctx: &Context) -> Result<Complex, String> { if args.len() == 1 { Ok(self(args.get(0))) } else { @@ -53,3 +53,12 @@ where } } } + + +pub struct EmptyFunction {} + +impl Function for EmptyFunction { + fn eval(&self, _args: FunctionArgument, _ctx: &Context) -> Result<Complex, String> { + Err("function not implemented in this scope".to_string()) + } +} |