Skip to content

Commit c1a4a2f

Browse files
committed
Remove the config api (#3633)
# Objective - Fix the ugliness of the `config` api. - Supercedes #2440, #2463, #2491 ## Solution - Since #2398, capturing closure systems have worked. - Use those instead where we needed config before - Remove the rest of the config api. - Related: #2777
1 parent 5191482 commit c1a4a2f

File tree

7 files changed

+107
-284
lines changed

7 files changed

+107
-284
lines changed

crates/bevy_core/src/time/fixed_timestep.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy_ecs::{
44
component::ComponentId,
55
query::Access,
66
schedule::ShouldRun,
7-
system::{ConfigurableSystem, IntoSystem, Local, Res, ResMut, System},
7+
system::{IntoSystem, Res, ResMut, System},
88
world::World,
99
};
1010
use bevy_utils::HashMap;
@@ -79,7 +79,9 @@ impl Default for FixedTimestep {
7979
fn default() -> Self {
8080
Self {
8181
state: LocalFixedTimestepState::default(),
82-
internal_system: Box::new(IntoSystem::into_system(Self::prepare_system)),
82+
internal_system: Box::new(IntoSystem::into_system(Self::prepare_system(
83+
Default::default(),
84+
))),
8385
}
8486
}
8587
}
@@ -116,18 +118,18 @@ impl FixedTimestep {
116118
}
117119

118120
fn prepare_system(
119-
mut state: Local<LocalFixedTimestepState>,
120-
time: Res<Time>,
121-
mut fixed_timesteps: ResMut<FixedTimesteps>,
122-
) -> ShouldRun {
123-
let should_run = state.update(&time);
124-
if let Some(ref label) = state.label {
125-
let res_state = fixed_timesteps.fixed_timesteps.get_mut(label).unwrap();
126-
res_state.step = state.step;
127-
res_state.accumulator = state.accumulator;
121+
mut state: LocalFixedTimestepState,
122+
) -> impl FnMut(Res<Time>, ResMut<FixedTimesteps>) -> ShouldRun {
123+
move |time, mut fixed_timesteps| {
124+
let should_run = state.update(&time);
125+
if let Some(ref label) = state.label {
126+
let res_state = fixed_timesteps.fixed_timesteps.get_mut(label).unwrap();
127+
res_state.step = state.step;
128+
res_state.accumulator = state.accumulator;
129+
}
130+
131+
should_run
128132
}
129-
130-
should_run
131133
}
132134
}
133135

@@ -202,8 +204,9 @@ impl System for FixedTimestep {
202204
}
203205

204206
fn initialize(&mut self, world: &mut World) {
205-
self.internal_system =
206-
Box::new(Self::prepare_system.config(|c| c.0 = Some(self.state.clone())));
207+
self.internal_system = Box::new(IntoSystem::into_system(Self::prepare_system(
208+
self.state.clone(),
209+
)));
207210
self.internal_system.initialize(world);
208211
if let Some(ref label) = self.state.label {
209212
let mut fixed_timesteps = world.get_resource_mut::<FixedTimesteps>().unwrap();

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ pub fn impl_query_set(_input: TokenStream) -> TokenStream {
222222
unsafe impl<#(#query: WorldQuery + 'static,)* #(#filter: WorldQuery + 'static,)*> SystemParamState for QuerySetState<(#(QueryState<#query, #filter>,)*)>
223223
where #(#filter::Fetch: FilterFetch,)*
224224
{
225-
type Config = ();
226-
fn init(world: &mut World, system_meta: &mut SystemMeta, config: Self::Config) -> Self {
225+
fn init(world: &mut World, system_meta: &mut SystemMeta) -> Self {
227226
#(
228227
let mut #query = QueryState::<#query, #filter>::new(world);
229228
assert_component_access_compatibility(
@@ -255,8 +254,6 @@ pub fn impl_query_set(_input: TokenStream) -> TokenStream {
255254
.extend(&#query.archetype_component_access);
256255
)*
257256
}
258-
259-
fn default_config() {}
260257
}
261258

262259
impl<'w, 's, #(#query: WorldQuery + 'static,)* #(#filter: WorldQuery + 'static,)*> SystemParamFetch<'w, 's> for QuerySetState<(#(QueryState<#query, #filter>,)*)>
@@ -389,10 +386,9 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
389386
}
390387

391388
unsafe impl<TSystemParamState: #path::system::SystemParamState, #punctuated_generics> #path::system::SystemParamState for #fetch_struct_name<TSystemParamState, #punctuated_generic_idents> {
392-
type Config = TSystemParamState::Config;
393-
fn init(world: &mut #path::world::World, system_meta: &mut #path::system::SystemMeta, config: Self::Config) -> Self {
389+
fn init(world: &mut #path::world::World, system_meta: &mut #path::system::SystemMeta) -> Self {
394390
Self {
395-
state: TSystemParamState::init(world, system_meta, config),
391+
state: TSystemParamState::init(world, system_meta),
396392
marker: std::marker::PhantomData,
397393
}
398394
}
@@ -401,10 +397,6 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
401397
self.state.new_archetype(archetype, system_meta)
402398
}
403399

404-
fn default_config() -> TSystemParamState::Config {
405-
TSystemParamState::default_config()
406-
}
407-
408400
fn apply(&mut self, world: &mut #path::world::World) {
409401
self.state.apply(world)
410402
}

crates/bevy_ecs/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ pub mod prelude {
3333
Schedule, Stage, StageLabel, State, SystemLabel, SystemSet, SystemStage,
3434
},
3535
system::{
36-
Commands, ConfigurableSystem, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem,
37-
Local, NonSend, NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System,
36+
Commands, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem, Local, NonSend,
37+
NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System,
3838
},
3939
world::{FromWorld, Mut, World},
4040
};

crates/bevy_ecs/src/schedule/state.rs

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
RunCriteriaDescriptor, RunCriteriaDescriptorCoercion, RunCriteriaLabel, ShouldRun,
44
SystemSet,
55
},
6-
system::{ConfigurableSystem, In, IntoChainSystem, Local, Res, ResMut},
6+
system::{In, IntoChainSystem, Local, Res, ResMut},
77
};
88
use std::{any::TypeId, fmt::Debug, hash::Hash};
99
use thiserror::Error;
@@ -98,134 +98,128 @@ impl<T> State<T>
9898
where
9999
T: StateData,
100100
{
101-
pub fn on_update(s: T) -> RunCriteriaDescriptor {
102-
(|state: Res<State<T>>, pred: Local<Option<T>>| {
103-
state.stack.last().unwrap() == pred.as_ref().unwrap() && state.transition.is_none()
101+
pub fn on_update(pred: T) -> RunCriteriaDescriptor {
102+
let pred_clone = pred.clone();
103+
(move |state: Res<State<T>>| {
104+
state.stack.last().unwrap() == &pred && state.transition.is_none()
104105
})
105-
.config(|(_, pred)| *pred = Some(Some(s.clone())))
106106
.chain(should_run_adapter::<T>)
107107
.after(DriverLabel::of::<T>())
108-
.label_discard_if_duplicate(StateCallback::Update.into_label(s))
108+
.label_discard_if_duplicate(StateCallback::Update.into_label(pred_clone))
109109
}
110110

111-
pub fn on_inactive_update(s: T) -> RunCriteriaDescriptor {
112-
(|state: Res<State<T>>, mut is_inactive: Local<bool>, pred: Local<Option<T>>| match &state
113-
.transition
114-
{
111+
pub fn on_inactive_update(pred: T) -> RunCriteriaDescriptor {
112+
let pred_clone = pred.clone();
113+
(move |state: Res<State<T>>, mut is_inactive: Local<bool>| match &state.transition {
115114
Some(StateTransition::Pausing(ref relevant, _))
116115
| Some(StateTransition::Resuming(_, ref relevant)) => {
117-
if relevant == pred.as_ref().unwrap() {
116+
if relevant == &pred {
118117
*is_inactive = !*is_inactive;
119118
}
120119
false
121120
}
122121
Some(_) => false,
123122
None => *is_inactive,
124123
})
125-
.config(|(_, _, pred)| *pred = Some(Some(s.clone())))
126124
.chain(should_run_adapter::<T>)
127125
.after(DriverLabel::of::<T>())
128-
.label_discard_if_duplicate(StateCallback::InactiveUpdate.into_label(s))
126+
.label_discard_if_duplicate(StateCallback::InactiveUpdate.into_label(pred_clone))
129127
}
130128

131-
pub fn on_in_stack_update(s: T) -> RunCriteriaDescriptor {
132-
(|state: Res<State<T>>, mut is_in_stack: Local<bool>, pred: Local<Option<T>>| match &state
133-
.transition
134-
{
129+
pub fn on_in_stack_update(pred: T) -> RunCriteriaDescriptor {
130+
let pred_clone = pred.clone();
131+
(move |state: Res<State<T>>, mut is_in_stack: Local<bool>| match &state.transition {
135132
Some(StateTransition::Entering(ref relevant, _))
136133
| Some(StateTransition::ExitingToResume(_, ref relevant)) => {
137-
if relevant == pred.as_ref().unwrap() {
134+
if relevant == &pred {
138135
*is_in_stack = !*is_in_stack;
139136
}
140137
false
141138
}
142139
Some(StateTransition::ExitingFull(_, ref relevant)) => {
143-
if relevant == pred.as_ref().unwrap() {
140+
if relevant == &pred {
144141
*is_in_stack = !*is_in_stack;
145142
}
146143
false
147144
}
148145
Some(StateTransition::Startup) => {
149-
if state.stack.last().unwrap() == pred.as_ref().unwrap() {
146+
if state.stack.last().unwrap() == &pred {
150147
*is_in_stack = !*is_in_stack;
151148
}
152149
false
153150
}
154151
Some(_) => false,
155152
None => *is_in_stack,
156153
})
157-
.config(|(_, _, pred)| *pred = Some(Some(s.clone())))
158154
.chain(should_run_adapter::<T>)
159155
.after(DriverLabel::of::<T>())
160-
.label_discard_if_duplicate(StateCallback::InStackUpdate.into_label(s))
156+
.label_discard_if_duplicate(StateCallback::InStackUpdate.into_label(pred_clone))
161157
}
162158

163-
pub fn on_enter(s: T) -> RunCriteriaDescriptor {
164-
(|state: Res<State<T>>, pred: Local<Option<T>>| {
159+
pub fn on_enter(pred: T) -> RunCriteriaDescriptor {
160+
let pred_clone = pred.clone();
161+
(move |state: Res<State<T>>| {
165162
state
166163
.transition
167164
.as_ref()
168165
.map_or(false, |transition| match transition {
169-
StateTransition::Entering(_, entering) => entering == pred.as_ref().unwrap(),
170-
StateTransition::Startup => {
171-
state.stack.last().unwrap() == pred.as_ref().unwrap()
172-
}
166+
StateTransition::Entering(_, entering) => entering == &pred,
167+
StateTransition::Startup => state.stack.last().unwrap() == &pred,
173168
_ => false,
174169
})
175170
})
176-
.config(|(_, pred)| *pred = Some(Some(s.clone())))
177171
.chain(should_run_adapter::<T>)
178172
.after(DriverLabel::of::<T>())
179-
.label_discard_if_duplicate(StateCallback::Enter.into_label(s))
173+
.label_discard_if_duplicate(StateCallback::Enter.into_label(pred_clone))
180174
}
181175

182-
pub fn on_exit(s: T) -> RunCriteriaDescriptor {
183-
(|state: Res<State<T>>, pred: Local<Option<T>>| {
176+
pub fn on_exit(pred: T) -> RunCriteriaDescriptor {
177+
let pred_clone = pred.clone();
178+
(move |state: Res<State<T>>| {
184179
state
185180
.transition
186181
.as_ref()
187182
.map_or(false, |transition| match transition {
188183
StateTransition::ExitingToResume(exiting, _)
189-
| StateTransition::ExitingFull(exiting, _) => exiting == pred.as_ref().unwrap(),
184+
| StateTransition::ExitingFull(exiting, _) => exiting == &pred,
190185
_ => false,
191186
})
192187
})
193-
.config(|(_, pred)| *pred = Some(Some(s.clone())))
194188
.chain(should_run_adapter::<T>)
195189
.after(DriverLabel::of::<T>())
196-
.label_discard_if_duplicate(StateCallback::Exit.into_label(s))
190+
.label_discard_if_duplicate(StateCallback::Exit.into_label(pred_clone))
197191
}
198192

199-
pub fn on_pause(s: T) -> RunCriteriaDescriptor {
200-
(|state: Res<State<T>>, pred: Local<Option<T>>| {
193+
pub fn on_pause(pred: T) -> RunCriteriaDescriptor {
194+
let pred_clone = pred.clone();
195+
(move |state: Res<State<T>>| {
201196
state
202197
.transition
203198
.as_ref()
204199
.map_or(false, |transition| match transition {
205-
StateTransition::Pausing(pausing, _) => pausing == pred.as_ref().unwrap(),
200+
StateTransition::Pausing(pausing, _) => pausing == &pred,
206201
_ => false,
207202
})
208203
})
209-
.config(|(_, pred)| *pred = Some(Some(s.clone())))
210204
.chain(should_run_adapter::<T>)
211205
.after(DriverLabel::of::<T>())
212-
.label_discard_if_duplicate(StateCallback::Pause.into_label(s))
206+
.label_discard_if_duplicate(StateCallback::Pause.into_label(pred_clone))
213207
}
214208

215-
pub fn on_resume(s: T) -> RunCriteriaDescriptor {
216-
(|state: Res<State<T>>, pred: Local<Option<T>>| {
209+
pub fn on_resume(pred: T) -> RunCriteriaDescriptor {
210+
let pred_clone = pred.clone();
211+
(move |state: Res<State<T>>| {
217212
state
218213
.transition
219214
.as_ref()
220215
.map_or(false, |transition| match transition {
221-
StateTransition::Resuming(_, resuming) => resuming == pred.as_ref().unwrap(),
216+
StateTransition::Resuming(_, resuming) => resuming == &pred,
222217
_ => false,
223218
})
224219
})
225-
.config(|(_, pred)| *pred = Some(Some(s.clone())))
226220
.chain(should_run_adapter::<T>)
227221
.after(DriverLabel::of::<T>())
228-
.label_discard_if_duplicate(StateCallback::Resume.into_label(s))
222+
.label_discard_if_duplicate(StateCallback::Resume.into_label(pred_clone))
229223
}
230224

231225
pub fn on_update_set(s: T) -> SystemSet {

0 commit comments

Comments
 (0)