From 95ba627a9c5e4e6e1d79f7aaff1854eb831511b4 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 17 Jan 2024 23:54:52 +0100 Subject: hand context on to Function trait --- src/context.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/context.rs') 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, vars: HashMap, - funcs: HashMap>, + funcs: HashMap>, } impl Context { @@ -25,7 +26,7 @@ impl Context { self } - pub fn with_functions(mut self, funcs: HashMap>) -> Self { + pub fn with_functions(mut self, funcs: HashMap>) -> 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> { - self.funcs.get_mut(name) + pub fn set_function(&mut self, name: &str, func: Arc) { + 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> { + pub fn function(&self, name: &str) -> Option<&Arc> { self.funcs.get(name) } } @@ -64,4 +65,3 @@ macro_rules! variables { } }; } - -- cgit v1.2.3-70-g09d2