Skip to content

Commit df20dd5

Browse files
committed
use dedicated blanket trait for state data
1 parent a070748 commit df20dd5

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

crates/bevy_app/src/app_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use bevy_ecs::{
77
component::ComponentDescriptor,
88
event::Events,
99
schedule::{
10-
RunOnce, Schedule, Stage, StageLabel, State, SystemDescriptor, SystemSet, SystemStage,
10+
RunOnce, Schedule, Stage, StageLabel, State, StateData, SystemDescriptor, SystemSet,
11+
SystemStage,
1112
},
1213
system::{IntoExclusiveSystem, IntoSystem, Resource},
1314
world::{FromWorld, World},
1415
};
1516
use bevy_utils::tracing::debug;
16-
use std::{fmt::Debug, hash::Hash};
1717

1818
/// Configure [App]s using the builder pattern
1919
pub struct AppBuilder {
@@ -252,7 +252,7 @@ impl AppBuilder {
252252
/// adding [State::get_driver] to additional stages you need it in.
253253
pub fn add_state<T>(&mut self, initial: T) -> &mut Self
254254
where
255-
T: Resource + Debug + Clone + Eq + Hash,
255+
T: StateData,
256256
{
257257
self.add_state_to_stage(CoreStage::Update, initial)
258258
}
@@ -264,7 +264,7 @@ impl AppBuilder {
264264
/// stages you need it in.
265265
pub fn add_state_to_stage<T>(&mut self, stage: impl StageLabel, initial: T) -> &mut Self
266266
where
267-
T: Resource + Debug + Clone + Eq + Hash,
267+
T: StateData,
268268
{
269269
self.insert_resource(State::new(initial))
270270
.add_system_set_to_stage(stage, State::<T>::get_driver())

crates/bevy_ecs/src/schedule/state.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ use crate::{
33
RunCriteriaDescriptor, RunCriteriaDescriptorCoercion, RunCriteriaLabel, ShouldRun,
44
SystemSet,
55
},
6-
system::{In, IntoChainSystem, IntoSystem, Local, Res, ResMut, Resource},
6+
system::{In, IntoChainSystem, IntoSystem, Local, Res, ResMut},
77
};
88
use std::{any::TypeId, fmt::Debug, hash::Hash};
99
use thiserror::Error;
1010

11+
pub trait StateData: Send + Sync + Clone + Eq + Debug + Hash + 'static {}
12+
impl<T> StateData for T where T: Send + Sync + Clone + Eq + Debug + Hash + 'static {}
13+
1114
/// ### Stack based state machine
1215
///
1316
/// This state machine has four operations: Push, Pop, Set and Replace.
@@ -16,15 +19,15 @@ use thiserror::Error;
1619
/// * Set replaces the active state with a new one
1720
/// * Replace unwinds the state stack, and replaces the entire stack with a single new state
1821
#[derive(Debug)]
19-
pub struct State<T: Resource + Clone + Eq> {
22+
pub struct State<T: StateData> {
2023
transition: Option<StateTransition<T>>,
2124
stack: Vec<T>,
2225
scheduled: Option<ScheduledOperation<T>>,
2326
end_next_loop: bool,
2427
}
2528

2629
#[derive(Debug)]
27-
enum StateTransition<T: Resource + Clone + Eq> {
30+
enum StateTransition<T: StateData> {
2831
PreStartup,
2932
Startup,
3033
// The parameter order is always (leaving, entering)
@@ -36,7 +39,7 @@ enum StateTransition<T: Resource + Clone + Eq> {
3639
}
3740

3841
#[derive(Debug)]
39-
enum ScheduledOperation<T: Resource + Clone + Eq> {
42+
enum ScheduledOperation<T: StateData> {
4043
Set(T),
4144
Replace(T),
4245
Pop,
@@ -57,7 +60,7 @@ enum StateCallback {
5760
impl StateCallback {
5861
fn into_label<T>(self, state: T) -> StateRunCriteriaLabel<T>
5962
where
60-
T: Resource + Debug + Clone + Eq + Hash,
63+
T: StateData,
6164
{
6265
StateRunCriteriaLabel(state, self)
6366
}
@@ -67,7 +70,7 @@ impl StateCallback {
6770
struct StateRunCriteriaLabel<T>(T, StateCallback);
6871
impl<T> RunCriteriaLabel for StateRunCriteriaLabel<T>
6972
where
70-
T: Resource + Debug + Clone + Eq + Hash,
73+
T: StateData,
7174
{
7275
fn dyn_clone(&self) -> Box<dyn RunCriteriaLabel> {
7376
Box::new(self.clone())
@@ -90,7 +93,7 @@ impl DriverLabel {
9093

9194
impl<T> State<T>
9295
where
93-
T: Resource + Debug + Clone + Eq + Hash,
96+
T: StateData,
9497
{
9598
pub fn on_update(s: T) -> RunCriteriaDescriptor {
9699
(|state: Res<State<T>>, pred: Local<Option<T>>| {
@@ -393,10 +396,7 @@ pub enum StateError {
393396
StackEmpty,
394397
}
395398

396-
fn should_run_adapter<T: Resource + Clone + Eq>(
397-
In(cmp_result): In<bool>,
398-
state: Res<State<T>>,
399-
) -> ShouldRun {
399+
fn should_run_adapter<T: StateData>(In(cmp_result): In<bool>, state: Res<State<T>>) -> ShouldRun {
400400
if state.end_next_loop {
401401
return ShouldRun::No;
402402
}
@@ -407,7 +407,7 @@ fn should_run_adapter<T: Resource + Clone + Eq>(
407407
}
408408
}
409409

410-
fn state_cleaner<T: Resource + Clone + Eq>(
410+
fn state_cleaner<T: StateData>(
411411
mut state: ResMut<State<T>>,
412412
mut prep_exit: Local<bool>,
413413
) -> ShouldRun {

crates/bevy_ecs/src/schedule/system_set.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
use crate::{
2-
schedule::{
3-
AmbiguitySetLabel, BoxedAmbiguitySetLabel, BoxedSystemLabel, IntoRunCriteria,
4-
RunCriteriaDescriptorOrLabel, State, SystemDescriptor, SystemLabel,
5-
},
6-
system::Resource,
1+
use crate::schedule::{
2+
AmbiguitySetLabel, BoxedAmbiguitySetLabel, BoxedSystemLabel, IntoRunCriteria,
3+
RunCriteriaDescriptorOrLabel, State, StateData, SystemDescriptor, SystemLabel,
74
};
8-
use std::{fmt::Debug, hash::Hash};
95

106
/// A builder for describing several systems at the same time.
117
pub struct SystemSet {
@@ -37,49 +33,49 @@ impl SystemSet {
3733

3834
pub fn on_update<T>(s: T) -> SystemSet
3935
where
40-
T: Resource + Debug + Clone + Eq + Hash,
36+
T: StateData,
4137
{
4238
Self::new().with_run_criteria(State::<T>::on_update(s))
4339
}
4440

4541
pub fn on_inactive_update<T>(s: T) -> SystemSet
4642
where
47-
T: Resource + Debug + Clone + Eq + Hash,
43+
T: StateData,
4844
{
4945
Self::new().with_run_criteria(State::<T>::on_inactive_update(s))
5046
}
5147

5248
pub fn on_in_stack_update<T>(s: T) -> SystemSet
5349
where
54-
T: Resource + Debug + Clone + Eq + Hash,
50+
T: StateData,
5551
{
5652
Self::new().with_run_criteria(State::<T>::on_in_stack_update(s))
5753
}
5854

5955
pub fn on_enter<T>(s: T) -> SystemSet
6056
where
61-
T: Resource + Debug + Clone + Eq + Hash,
57+
T: StateData,
6258
{
6359
Self::new().with_run_criteria(State::<T>::on_enter(s))
6460
}
6561

6662
pub fn on_exit<T>(s: T) -> SystemSet
6763
where
68-
T: Resource + Debug + Clone + Eq + Hash,
64+
T: StateData,
6965
{
7066
Self::new().with_run_criteria(State::<T>::on_exit(s))
7167
}
7268

7369
pub fn on_pause<T>(s: T) -> SystemSet
7470
where
75-
T: Resource + Debug + Clone + Eq + Hash,
71+
T: StateData,
7672
{
7773
Self::new().with_run_criteria(State::<T>::on_pause(s))
7874
}
7975

8076
pub fn on_resume<T>(s: T) -> SystemSet
8177
where
82-
T: Resource + Debug + Clone + Eq + Hash,
78+
T: StateData,
8379
{
8480
Self::new().with_run_criteria(State::<T>::on_resume(s))
8581
}

0 commit comments

Comments
 (0)