Skip to content

Commit f41745f

Browse files
committed
simplify bounds
1 parent 88578ef commit f41745f

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

crates/bevy_app/src/app_builder.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{any::Any, hash::Hash};
1+
use std::any::Any;
22

33
use crate::{
44
app::{App, AppExit},
@@ -211,7 +211,7 @@ impl AppBuilder {
211211
format!("state({})", std::any::type_name::<T>())
212212
}
213213

214-
pub fn add_state<T: Clone + Eq + Hash + Resource>(&mut self, initial: T) -> &mut Self {
214+
pub fn add_state<T: Clone + Resource>(&mut self, initial: T) -> &mut Self {
215215
self.add_resource(State::new(initial));
216216
self.app.schedule.add_stage_after(
217217
stage::UPDATE,
@@ -221,7 +221,7 @@ impl AppBuilder {
221221
self
222222
}
223223

224-
pub fn on_state_enter<T: Clone + Eq + Hash + Resource, Params, S: IntoStage<Params>>(
224+
pub fn on_state_enter<T: Clone + Resource, Params, S: IntoStage<Params>>(
225225
&mut self,
226226
value: T,
227227
stage: S,
@@ -232,7 +232,7 @@ impl AppBuilder {
232232
)
233233
}
234234

235-
pub fn on_state_update<T: Clone + Eq + Hash + Resource, Params, S: IntoStage<Params>>(
235+
pub fn on_state_update<T: Clone + Resource, Params, S: IntoStage<Params>>(
236236
&mut self,
237237
value: T,
238238
stage: S,
@@ -243,7 +243,7 @@ impl AppBuilder {
243243
)
244244
}
245245

246-
pub fn on_state_exit<T: Clone + Eq + Hash + Resource, Params, S: IntoStage<Params>>(
246+
pub fn on_state_exit<T: Clone + Resource, Params, S: IntoStage<Params>>(
247247
&mut self,
248248
value: T,
249249
stage: S,

crates/bevy_ecs/src/schedule/state.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{IntoStage, Resource, Resources, Stage, World};
22
use bevy_utils::HashMap;
3-
use std::{hash::Hash, mem::Discriminant, ops::Deref};
3+
use std::{mem::Discriminant, ops::Deref};
44
use thiserror::Error;
55

66
#[derive(Default)]
@@ -22,7 +22,8 @@ impl<T> Default for StateStage<T> {
2222
}
2323
}
2424

25-
impl<T: Eq + Hash> StateStage<T> {
25+
#[allow(clippy::mem_discriminant_non_enum)]
26+
impl<T> StateStage<T> {
2627
pub fn with_on_state_enter<Params, S: IntoStage<Params>>(mut self, state: T, stage: S) -> Self {
2728
self.on_state_enter(state, stage);
2829
self
@@ -78,7 +79,8 @@ impl<T: Eq + Hash> StateStage<T> {
7879
}
7980
}
8081

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> {
8284
fn run(&mut self, world: &mut World, resources: &mut Resources) {
8385
loop {
8486
let (next_stage, current_stage) = {
@@ -114,15 +116,13 @@ impl<T: Resource + Clone + Eq + Hash> Stage for StateStage<T> {
114116
{
115117
enter_next.run(world, resources);
116118
}
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;
126126
}
127127
}
128128
}
@@ -136,13 +136,14 @@ pub enum StateError {
136136
}
137137

138138
#[derive(Debug)]
139-
pub struct State<T: Clone + Hash + Eq + PartialEq> {
139+
pub struct State<T: Clone> {
140140
previous: Option<T>,
141141
current: T,
142142
next: Option<T>,
143143
}
144144

145-
impl<T: Clone + Hash + Eq + PartialEq> State<T> {
145+
#[allow(clippy::mem_discriminant_non_enum)]
146+
impl<T: Clone> State<T> {
146147
pub fn new(state: T) -> Self {
147148
Self {
148149
current: state.clone(),
@@ -166,7 +167,7 @@ impl<T: Clone + Hash + Eq + PartialEq> State<T> {
166167

167168
/// 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
168169
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) {
170171
return Err(StateError::AlreadyInState);
171172
}
172173

@@ -180,7 +181,7 @@ impl<T: Clone + Hash + Eq + PartialEq> State<T> {
180181

181182
/// Same as [Self::queue], but there is already a next state, it will be overwritten instead of failing
182183
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) {
184185
return Err(StateError::AlreadyInState);
185186
}
186187

@@ -190,12 +191,15 @@ impl<T: Clone + Hash + Eq + PartialEq> State<T> {
190191

191192
fn apply_next(&mut self) {
192193
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+
}
194198
}
195199
}
196200
}
197201

198-
impl<T: Clone + Hash + Eq + PartialEq> Deref for State<T> {
202+
impl<T: Clone> Deref for State<T> {
199203
type Target = T;
200204

201205
fn deref(&self) -> &Self::Target {

examples/2d/texture_atlas.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ fn main() {
66
.init_resource::<RpgSpriteHandles>()
77
.add_plugins(DefaultPlugins)
88
.add_state(AppState::Setup)
9-
.state_enter(AppState::Setup, load_textures)
10-
.state_update(AppState::Setup, check_textures)
11-
.state_enter(AppState::Finshed, setup)
9+
.on_state_enter(AppState::Setup, load_textures)
10+
.on_state_update(AppState::Setup, check_textures)
11+
.on_state_enter(AppState::Finshed, setup)
1212
.run();
1313
}
1414

15-
#[derive(Clone, Hash, Eq, PartialEq)]
15+
#[derive(Clone)]
1616
enum AppState {
1717
Setup,
1818
Finshed,
@@ -28,14 +28,14 @@ fn load_textures(mut rpg_sprite_handles: ResMut<RpgSpriteHandles>, asset_server:
2828
}
2929

3030
fn check_textures(
31-
state: Res<State<AppState>>,
31+
mut state: ResMut<State<AppState>>,
3232
rpg_sprite_handles: ResMut<RpgSpriteHandles>,
3333
asset_server: Res<AssetServer>,
3434
) {
3535
if let LoadState::Loaded =
3636
asset_server.get_group_load_state(rpg_sprite_handles.handles.iter().map(|handle| handle.id))
3737
{
38-
state.queue(AppState::Finshed);
38+
state.set_next(AppState::Finshed).unwrap();
3939
}
4040
}
4141

examples/ecs/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
.run();
2020
}
2121

22-
#[derive(Clone, Hash, Eq, PartialEq)]
22+
#[derive(Clone)]
2323
enum AppState {
2424
Menu,
2525
InGame,

0 commit comments

Comments
 (0)