1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
use crate::prelude::GlobalState; use serde::Serialize; use std::fmt::Debug; use crate::state::state::State; use uuid::Uuid; use crate::state::environment::Environment; use serde::de::DeserializeOwned; #[derive(Clone)] pub struct MappedState<T, U, GS> where T: Serialize + Clone + Debug, U: Serialize + Clone + Debug, GS: GlobalState { id: Option<Uuid>, mapped_state: Box<dyn State<U, GS>>, map: fn(&U) -> T, latest_value: T, } impl<T: Serialize + Clone + Debug, U: Serialize + Clone + Debug, GS: GlobalState> MappedState<T, U, GS> { pub fn new_local(state: Box<dyn State<U, GS>>, map: fn(&U) -> T, start: T) -> MappedState<T, U, GS> { MappedState { id: Some(Uuid::new_v4()), mapped_state: state, map, latest_value: start } } pub fn new(state: Box<dyn State<U, GS>>, map: fn(&U) -> T, start: T) -> MappedState<T, U, GS> { MappedState { id: None, mapped_state: state, map, latest_value: start } } } impl<T: Serialize + Clone + Debug, U: Serialize + Clone + Debug + DeserializeOwned, GS: GlobalState> State<T, GS> for MappedState<T, U, GS> { fn get_value_mut(&mut self, global_state: &mut GS) -> &mut T { self.latest_value = (self.map)(self.mapped_state.get_value_mut(global_state)); &mut self.latest_value } fn get_value(&mut self, global_state: &GS) -> &T { self.latest_value = (self.map)(self.mapped_state.get_value(global_state)); &self.latest_value } fn get_latest_value(&self) -> &T { &self.latest_value } fn get_latest_value_mut(&mut self) -> &mut T { &mut self.latest_value } fn get_key(&self) -> Option<&String> { None } fn update_dependent_states(&mut self, env: &Environment<GS>) { env.update_local_state(&mut self.mapped_state) } }