3
3
4
4
use std:: ops:: { Add , AddAssign } ;
5
5
6
- use discro:: Publisher ;
6
+ use discro:: { ModifyStatus , Publisher } ;
7
7
use tokio:: task:: JoinHandle ;
8
8
9
9
pub use aoide_backend_embedded:: Environment ;
@@ -87,37 +87,38 @@ impl AddAssign for ActionEffect {
87
87
}
88
88
}
89
89
90
+ impl ModifyStatus for ActionEffect {
91
+ fn is_modified ( & self ) -> bool {
92
+ match self {
93
+ Self :: Unchanged => false ,
94
+ Self :: MaybeChanged | Self :: Changed => true ,
95
+ }
96
+ }
97
+ }
98
+
90
99
pub ( crate ) fn modify_shared_state_action_effect < State > (
91
100
shared_state : & Publisher < State > ,
92
101
modify_state : impl FnOnce ( & mut State ) -> ActionEffect ,
93
102
) -> ActionEffect {
94
- let mut effect = ActionEffect :: MaybeChanged ;
95
- shared_state . modify ( |state| {
96
- effect = modify_state ( state ) ;
97
- match effect {
98
- ActionEffect :: Unchanged => false ,
99
- ActionEffect :: MaybeChanged | ActionEffect :: Changed => true ,
100
- }
101
- } ) ;
102
- effect
103
+ shared_state . modify ( modify_state )
104
+ }
105
+
106
+ struct ActionEffectResult < Result > ( ActionEffect , Result ) ;
107
+
108
+ impl < Result > ModifyStatus for ActionEffectResult < Result > {
109
+ fn is_modified ( & self ) -> bool {
110
+ self . 0 . is_modified ( )
111
+ }
103
112
}
104
113
105
114
pub ( crate ) fn modify_shared_state_action_effect_result < State , Result > (
106
115
shared_state : & Publisher < State > ,
107
116
modify_state : impl FnOnce ( & mut State ) -> ( ActionEffect , Result ) ,
108
117
) -> ( ActionEffect , Result ) {
109
- let mut effect = ActionEffect :: MaybeChanged ;
110
- let mut result = None ;
111
- shared_state. modify ( |state| {
112
- let ( modify_effect, modify_result) = modify_state ( state) ;
113
- effect = modify_effect;
114
- result = Some ( modify_result) ;
115
- match modify_effect {
116
- ActionEffect :: Unchanged => false ,
117
- ActionEffect :: MaybeChanged | ActionEffect :: Changed => true ,
118
- }
118
+ let ActionEffectResult ( effect, result) = shared_state. modify ( |state| {
119
+ let ( effect, result) = modify_state ( state) ;
120
+ ActionEffectResult ( effect, result)
119
121
} ) ;
120
- let result = result. expect ( "has been set" ) ;
121
122
( effect, result)
122
123
}
123
124
@@ -126,15 +127,10 @@ pub(crate) fn modify_shared_state_result<State, Result>(
126
127
modify_state : impl FnOnce ( & mut State ) -> Result ,
127
128
action_effect : impl FnOnce ( & Result ) -> ActionEffect ,
128
129
) -> Result {
129
- let mut result = None ;
130
- shared_state. modify ( |state| {
131
- let modify_result = modify_state ( state) ;
132
- let modified = match action_effect ( & modify_result) {
133
- ActionEffect :: Unchanged => false ,
134
- ActionEffect :: MaybeChanged | ActionEffect :: Changed => true ,
135
- } ;
136
- result = Some ( modify_result) ;
137
- modified
130
+ let ActionEffectResult ( _effect, result) = shared_state. modify ( |state| {
131
+ let result = modify_state ( state) ;
132
+ let effect = action_effect ( & result) ;
133
+ ActionEffectResult ( effect, result)
138
134
} ) ;
139
- result. expect ( "has been set" )
135
+ result
140
136
}
0 commit comments