From eb64a77aac8f8b8339d80a9bfb5c9a6f8867aaa5 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 28 Jun 2025 16:00:16 -0500 Subject: [PATCH] state fix --- src/state.rs | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/state.rs b/src/state.rs index 45c500c..d3807fa 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,7 +1,7 @@ use std::{ any::Any, collections::HashMap, - fmt::Display, + fmt::{Debug, Display}, marker::PhantomData, ops::{Add, AddAssign, Div, Sub, SubAssign}, sync::{Arc, Mutex}, @@ -16,6 +16,7 @@ pub struct StateManager { dirty: Mutex, } +#[derive(Clone)] pub struct State { manager: Arc, id: usize, @@ -67,6 +68,18 @@ impl State { pub fn dirty(&self) { *self.manager.dirty.lock().unwrap() = true; } + + pub fn get(&self) -> S + where + S: Clone, + { + self.manager.store.lock().unwrap()[&self.id] + .downcast_ref::>() + .unwrap() + .lock() + .unwrap() + .clone() + } } impl Display for State { @@ -80,10 +93,21 @@ impl Display for State { } } -impl Add for State { - type Output = ::Output; +impl Debug for State { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.manager.store.lock().unwrap()[&self.id] + .downcast_ref::>() + .unwrap() + .lock() + .unwrap() + .fmt(f) + } +} - fn add(self, rhs: S) -> Self::Output { +impl + Clone + 'static> Add for State { + type Output = >::Output; + + fn add(self, rhs: Sv) -> Self::Output { self.manager.store.lock().unwrap()[&self.id] .downcast_ref::>() .unwrap() @@ -94,10 +118,10 @@ impl Add for State { } } -impl Sub for State { - type Output = ::Output; +impl + Clone + 'static> Sub for State { + type Output = >::Output; - fn sub(self, rhs: S) -> Self::Output { + fn sub(self, rhs: Sv) -> Self::Output { self.manager.store.lock().unwrap()[&self.id] .downcast_ref::>() .unwrap() @@ -108,10 +132,10 @@ impl Sub for State { } } -impl Div for State { - type Output = ::Output; +impl + Clone + 'static> Div for State { + type Output = >::Output; - fn div(self, rhs: S) -> Self::Output { + fn div(self, rhs: Sv) -> Self::Output { self.manager.store.lock().unwrap()[&self.id] .downcast_ref::>() .unwrap() @@ -122,8 +146,8 @@ impl Div for State { } } -impl AddAssign for State { - fn add_assign(&mut self, rhs: S) { +impl + 'static> AddAssign for State { + fn add_assign(&mut self, rhs: Sv) { self.manager.store.lock().unwrap()[&self.id] .downcast_ref::>() .unwrap() @@ -134,8 +158,8 @@ impl AddAssign for State { } } -impl SubAssign for State { - fn sub_assign(&mut self, rhs: S) { +impl + 'static> SubAssign for State { + fn sub_assign(&mut self, rhs: Sv) { self.manager.store.lock().unwrap()[&self.id] .downcast_ref::>() .unwrap()