diff options
Diffstat (limited to 'src/context.rs')
| -rw-r--r-- | src/context.rs | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/src/context.rs b/src/context.rs index de1d09d..04e27f7 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,13 +1,14 @@ +use crate::complex::Complex; use crate::function::Function; use crate::operation::Operation; -use crate::complex::Complex; +use std::sync::Arc; use std::collections::HashMap; -#[derive(Default)] +#[derive(Default, Clone)] pub struct Context { ops: Vec<Operation>, vars: HashMap<String, Complex>, - funcs: HashMap<String, Box<dyn Function>>, + funcs: HashMap<String, Arc<dyn Function>>, } impl Context { @@ -25,7 +26,7 @@ impl Context { self } - pub fn with_functions(mut self, funcs: HashMap<String, Box<dyn Function>>) -> Self { + pub fn with_functions(mut self, funcs: HashMap<String, Arc<dyn Function>>) -> Self { self.funcs = funcs; self } @@ -34,12 +35,12 @@ impl Context { &self.ops } - pub fn variable_mut(&mut self, name: &str) -> Option<&mut Complex> { - self.vars.get_mut(name) + pub fn set_variable(&mut self, name: &str, value: Complex) { + self.vars.insert(name.to_string(), value); } - pub fn function_mut(&mut self, name: &str) -> Option<&mut Box<dyn Function>> { - self.funcs.get_mut(name) + pub fn set_function(&mut self, name: &str, func: Arc<dyn Function>) { + self.funcs.insert(name.to_string(), func); } pub fn variable(&self, name: &str) -> Option<&Complex> { @@ -47,7 +48,7 @@ impl Context { } #[allow(clippy::borrowed_box)] - pub fn function(&self, name: &str) -> Option<&Box<dyn Function>> { + pub fn function(&self, name: &str) -> Option<&Arc<dyn Function>> { self.funcs.get(name) } } @@ -64,4 +65,3 @@ macro_rules! variables { } }; } - |