1
1
use crate :: { IntoStage , Resource , Resources , Stage , World } ;
2
2
use bevy_utils:: HashMap ;
3
- use std:: { hash :: Hash , mem:: Discriminant , ops:: Deref } ;
3
+ use std:: { mem:: Discriminant , ops:: Deref } ;
4
4
use thiserror:: Error ;
5
5
6
6
#[ derive( Default ) ]
@@ -22,7 +22,8 @@ impl<T> Default for StateStage<T> {
22
22
}
23
23
}
24
24
25
- impl < T : Eq + Hash > StateStage < T > {
25
+ #[ allow( clippy:: mem_discriminant_non_enum) ]
26
+ impl < T > StateStage < T > {
26
27
pub fn with_on_state_enter < Params , S : IntoStage < Params > > ( mut self , state : T , stage : S ) -> Self {
27
28
self . on_state_enter ( state, stage) ;
28
29
self
@@ -78,7 +79,8 @@ impl<T: Eq + Hash> StateStage<T> {
78
79
}
79
80
}
80
81
81
- impl < T : Resource + Clone + Eq + Hash > Stage for StateStage < T > {
82
+ #[ allow( clippy:: mem_discriminant_non_enum) ]
83
+ impl < T : Resource + Clone > Stage for StateStage < T > {
82
84
fn run ( & mut self , world : & mut World , resources : & mut Resources ) {
83
85
loop {
84
86
let ( next_stage, current_stage) = {
@@ -114,15 +116,13 @@ impl<T: Resource + Clone + Eq + Hash> Stage for StateStage<T> {
114
116
{
115
117
enter_next. run ( world, resources) ;
116
118
}
117
- } else {
118
- if let Some ( update_current) = self
119
- . stages
120
- . get_mut ( & current_stage)
121
- . and_then ( |stage| stage. update . as_mut ( ) )
122
- {
123
- update_current. run ( world, resources) ;
124
- break ;
125
- }
119
+ } else if let Some ( update_current) = self
120
+ . stages
121
+ . get_mut ( & current_stage)
122
+ . and_then ( |stage| stage. update . as_mut ( ) )
123
+ {
124
+ update_current. run ( world, resources) ;
125
+ break ;
126
126
}
127
127
}
128
128
}
@@ -136,13 +136,14 @@ pub enum StateError {
136
136
}
137
137
138
138
#[ derive( Debug ) ]
139
- pub struct State < T : Clone + Hash + Eq + PartialEq > {
139
+ pub struct State < T : Clone > {
140
140
previous : Option < T > ,
141
141
current : T ,
142
142
next : Option < T > ,
143
143
}
144
144
145
- impl < T : Clone + Hash + Eq + PartialEq > State < T > {
145
+ #[ allow( clippy:: mem_discriminant_non_enum) ]
146
+ impl < T : Clone > State < T > {
146
147
pub fn new ( state : T ) -> Self {
147
148
Self {
148
149
current : state. clone ( ) ,
@@ -166,7 +167,7 @@ impl<T: Clone + Hash + Eq + PartialEq> State<T> {
166
167
167
168
/// Queue a state change. This will fail if there is already a state in the queue, or if the given `state` matches the current state
168
169
pub fn set_next ( & mut self , state : T ) -> Result < ( ) , StateError > {
169
- if self . current == state {
170
+ if std :: mem :: discriminant ( & self . current ) == std :: mem :: discriminant ( & state) {
170
171
return Err ( StateError :: AlreadyInState ) ;
171
172
}
172
173
@@ -180,7 +181,7 @@ impl<T: Clone + Hash + Eq + PartialEq> State<T> {
180
181
181
182
/// Same as [Self::queue], but there is already a next state, it will be overwritten instead of failing
182
183
pub fn overwrite_next ( & mut self , state : T ) -> Result < ( ) , StateError > {
183
- if self . current == state {
184
+ if std :: mem :: discriminant ( & self . current ) == std :: mem :: discriminant ( & state) {
184
185
return Err ( StateError :: AlreadyInState ) ;
185
186
}
186
187
@@ -190,12 +191,15 @@ impl<T: Clone + Hash + Eq + PartialEq> State<T> {
190
191
191
192
fn apply_next ( & mut self ) {
192
193
if let Some ( next) = self . next . take ( ) {
193
- self . previous = Some ( std:: mem:: replace ( & mut self . current , next) )
194
+ let previous = std:: mem:: replace ( & mut self . current , next) ;
195
+ if std:: mem:: discriminant ( & previous) != std:: mem:: discriminant ( & self . current ) {
196
+ self . previous = Some ( previous)
197
+ }
194
198
}
195
199
}
196
200
}
197
201
198
- impl < T : Clone + Hash + Eq + PartialEq > Deref for State < T > {
202
+ impl < T : Clone > Deref for State < T > {
199
203
type Target = T ;
200
204
201
205
fn deref ( & self ) -> & Self :: Target {
0 commit comments