state fix
This commit is contained in:
+38
-14
@@ -1,7 +1,7 @@
|
|||||||
use std::{
|
use std::{
|
||||||
any::Any,
|
any::Any,
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fmt::Display,
|
fmt::{Debug, Display},
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
ops::{Add, AddAssign, Div, Sub, SubAssign},
|
ops::{Add, AddAssign, Div, Sub, SubAssign},
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
@@ -16,6 +16,7 @@ pub struct StateManager {
|
|||||||
dirty: Mutex<bool>,
|
dirty: Mutex<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct State<S> {
|
pub struct State<S> {
|
||||||
manager: Arc<StateManager>,
|
manager: Arc<StateManager>,
|
||||||
id: usize,
|
id: usize,
|
||||||
@@ -67,6 +68,18 @@ impl<S: 'static> State<S> {
|
|||||||
pub fn dirty(&self) {
|
pub fn dirty(&self) {
|
||||||
*self.manager.dirty.lock().unwrap() = true;
|
*self.manager.dirty.lock().unwrap() = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get(&self) -> S
|
||||||
|
where
|
||||||
|
S: Clone,
|
||||||
|
{
|
||||||
|
self.manager.store.lock().unwrap()[&self.id]
|
||||||
|
.downcast_ref::<Mutex<S>>()
|
||||||
|
.unwrap()
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Display + 'static> Display for State<S> {
|
impl<S: Display + 'static> Display for State<S> {
|
||||||
@@ -80,10 +93,21 @@ impl<S: Display + 'static> Display for State<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Add + Clone + 'static> Add<S> for State<S> {
|
impl<S: Debug + 'static> Debug for State<S> {
|
||||||
type Output = <S as Add>::Output;
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.manager.store.lock().unwrap()[&self.id]
|
||||||
|
.downcast_ref::<Mutex<S>>()
|
||||||
|
.unwrap()
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn add(self, rhs: S) -> Self::Output {
|
impl<Sv, S: Add<Sv> + Clone + 'static> Add<Sv> for State<S> {
|
||||||
|
type Output = <S as Add<Sv>>::Output;
|
||||||
|
|
||||||
|
fn add(self, rhs: Sv) -> Self::Output {
|
||||||
self.manager.store.lock().unwrap()[&self.id]
|
self.manager.store.lock().unwrap()[&self.id]
|
||||||
.downcast_ref::<Mutex<S>>()
|
.downcast_ref::<Mutex<S>>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -94,10 +118,10 @@ impl<S: Add + Clone + 'static> Add<S> for State<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Sub + Clone + 'static> Sub<S> for State<S> {
|
impl<Sv, S: Sub<Sv> + Clone + 'static> Sub<Sv> for State<S> {
|
||||||
type Output = <S as Sub>::Output;
|
type Output = <S as Sub<Sv>>::Output;
|
||||||
|
|
||||||
fn sub(self, rhs: S) -> Self::Output {
|
fn sub(self, rhs: Sv) -> Self::Output {
|
||||||
self.manager.store.lock().unwrap()[&self.id]
|
self.manager.store.lock().unwrap()[&self.id]
|
||||||
.downcast_ref::<Mutex<S>>()
|
.downcast_ref::<Mutex<S>>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -108,10 +132,10 @@ impl<S: Sub + Clone + 'static> Sub<S> for State<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Div + Clone + 'static> Div<S> for State<S> {
|
impl<Sv, S: Div<Sv> + Clone + 'static> Div<Sv> for State<S> {
|
||||||
type Output = <S as Div>::Output;
|
type Output = <S as Div<Sv>>::Output;
|
||||||
|
|
||||||
fn div(self, rhs: S) -> Self::Output {
|
fn div(self, rhs: Sv) -> Self::Output {
|
||||||
self.manager.store.lock().unwrap()[&self.id]
|
self.manager.store.lock().unwrap()[&self.id]
|
||||||
.downcast_ref::<Mutex<S>>()
|
.downcast_ref::<Mutex<S>>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -122,8 +146,8 @@ impl<S: Div + Clone + 'static> Div<S> for State<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: AddAssign + 'static> AddAssign<S> for State<S> {
|
impl<Sv, S: AddAssign<Sv> + 'static> AddAssign<Sv> for State<S> {
|
||||||
fn add_assign(&mut self, rhs: S) {
|
fn add_assign(&mut self, rhs: Sv) {
|
||||||
self.manager.store.lock().unwrap()[&self.id]
|
self.manager.store.lock().unwrap()[&self.id]
|
||||||
.downcast_ref::<Mutex<S>>()
|
.downcast_ref::<Mutex<S>>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -134,8 +158,8 @@ impl<S: AddAssign + 'static> AddAssign<S> for State<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: SubAssign + 'static> SubAssign<S> for State<S> {
|
impl<Sv, S: SubAssign<Sv> + 'static> SubAssign<Sv> for State<S> {
|
||||||
fn sub_assign(&mut self, rhs: S) {
|
fn sub_assign(&mut self, rhs: Sv) {
|
||||||
self.manager.store.lock().unwrap()[&self.id]
|
self.manager.store.lock().unwrap()[&self.id]
|
||||||
.downcast_ref::<Mutex<S>>()
|
.downcast_ref::<Mutex<S>>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|||||||
Reference in New Issue
Block a user