Skip to content

Commit 841755a

Browse files
authored
Adopt a Fetch pattern for SystemParams (#1074)
1 parent 51650f1 commit 841755a

File tree

94 files changed

+633
-482
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+633
-482
lines changed

crates/bevy_app/src/app_builder.rs

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Default for AppBuilder {
2424
app_builder
2525
.add_default_stages()
2626
.add_event::<AppExit>()
27-
.add_system_to_stage(stage::LAST, clear_trackers_system);
27+
.add_system_to_stage(stage::LAST, clear_trackers_system.system());
2828
app_builder
2929
}
3030
}
@@ -125,68 +125,48 @@ impl AppBuilder {
125125
self
126126
}
127127

128-
pub fn add_system<S, Params, IntoS>(&mut self, system: IntoS) -> &mut Self
129-
where
130-
S: System<In = (), Out = ()>,
131-
IntoS: IntoSystem<Params, S>,
132-
{
128+
pub fn add_system<S: System<In = (), Out = ()>>(&mut self, system: S) -> &mut Self {
133129
self.add_system_to_stage(stage::UPDATE, system)
134130
}
135131

136-
pub fn on_state_enter<T: Clone + Resource, S, Params, IntoS>(
132+
pub fn on_state_enter<T: Clone + Resource, S: System<In = (), Out = ()>>(
137133
&mut self,
138134
stage: &str,
139135
state: T,
140-
system: IntoS,
141-
) -> &mut Self
142-
where
143-
S: System<In = (), Out = ()>,
144-
IntoS: IntoSystem<Params, S>,
145-
{
136+
system: S,
137+
) -> &mut Self {
146138
self.stage(stage, |stage: &mut StateStage<T>| {
147139
stage.on_state_enter(state, system)
148140
})
149141
}
150142

151-
pub fn on_state_update<T: Clone + Resource, S, Params, IntoS>(
143+
pub fn on_state_update<T: Clone + Resource, S: System<In = (), Out = ()>>(
152144
&mut self,
153145
stage: &str,
154146
state: T,
155-
system: IntoS,
156-
) -> &mut Self
157-
where
158-
S: System<In = (), Out = ()>,
159-
IntoS: IntoSystem<Params, S>,
160-
{
147+
system: S,
148+
) -> &mut Self {
161149
self.stage(stage, |stage: &mut StateStage<T>| {
162150
stage.on_state_update(state, system)
163151
})
164152
}
165153

166-
pub fn on_state_exit<T: Clone + Resource, S, Params, IntoS>(
154+
pub fn on_state_exit<T: Clone + Resource, S: System<In = (), Out = ()>>(
167155
&mut self,
168156
stage: &str,
169157
state: T,
170-
system: IntoS,
171-
) -> &mut Self
172-
where
173-
S: System<In = (), Out = ()>,
174-
IntoS: IntoSystem<Params, S>,
175-
{
158+
system: S,
159+
) -> &mut Self {
176160
self.stage(stage, |stage: &mut StateStage<T>| {
177161
stage.on_state_exit(state, system)
178162
})
179163
}
180164

181-
pub fn add_startup_system_to_stage<S, Params, IntoS>(
165+
pub fn add_startup_system_to_stage<S: System<In = (), Out = ()>>(
182166
&mut self,
183167
stage_name: &'static str,
184-
system: IntoS,
185-
) -> &mut Self
186-
where
187-
S: System<In = (), Out = ()>,
188-
IntoS: IntoSystem<Params, S>,
189-
{
168+
system: S,
169+
) -> &mut Self {
190170
self.app
191171
.schedule
192172
.stage(stage::STARTUP, |schedule: &mut Schedule| {
@@ -195,11 +175,7 @@ impl AppBuilder {
195175
self
196176
}
197177

198-
pub fn add_startup_system<S, Params, IntoS>(&mut self, system: IntoS) -> &mut Self
199-
where
200-
S: System<In = (), Out = ()>,
201-
IntoS: IntoSystem<Params, S>,
202-
{
178+
pub fn add_startup_system<S: System<In = (), Out = ()>>(&mut self, system: S) -> &mut Self {
203179
self.add_startup_system_to_stage(startup_stage::STARTUP, system)
204180
}
205181

@@ -221,15 +197,11 @@ impl AppBuilder {
221197
.add_stage(stage::LAST, SystemStage::parallel())
222198
}
223199

224-
pub fn add_system_to_stage<S, Params, IntoS>(
200+
pub fn add_system_to_stage<S: System<In = (), Out = ()>>(
225201
&mut self,
226202
stage_name: &'static str,
227-
system: IntoS,
228-
) -> &mut Self
229-
where
230-
S: System<In = (), Out = ()>,
231-
IntoS: IntoSystem<Params, S>,
232-
{
203+
system: S,
204+
) -> &mut Self {
233205
self.app.schedule.add_system_to_stage(stage_name, system);
234206
self
235207
}
@@ -239,7 +211,7 @@ impl AppBuilder {
239211
T: Send + Sync + 'static,
240212
{
241213
self.add_resource(Events::<T>::default())
242-
.add_system_to_stage(stage::EVENT, Events::<T>::update_system)
214+
.add_system_to_stage(stage::EVENT, Events::<T>::update_system.system())
243215
}
244216

245217
/// Adds a resource to the current [App] and overwrites any resource previously added of the same type.

crates/bevy_asset/src/assets.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
update_asset_storage_system, Asset, AssetLoader, AssetServer, Handle, HandleId, RefChange,
33
};
44
use bevy_app::{prelude::Events, AppBuilder};
5-
use bevy_ecs::{FromResources, ResMut};
5+
use bevy_ecs::{FromResources, IntoSystem, ResMut};
66
use bevy_reflect::RegisterTypeBuilder;
77
use bevy_utils::HashMap;
88
use crossbeam_channel::Sender;
@@ -218,8 +218,14 @@ impl AddAsset for AppBuilder {
218218
};
219219

220220
self.add_resource(assets)
221-
.add_system_to_stage(super::stage::ASSET_EVENTS, Assets::<T>::asset_event_system)
222-
.add_system_to_stage(crate::stage::LOAD_ASSETS, update_asset_storage_system::<T>)
221+
.add_system_to_stage(
222+
super::stage::ASSET_EVENTS,
223+
Assets::<T>::asset_event_system.system(),
224+
)
225+
.add_system_to_stage(
226+
crate::stage::LOAD_ASSETS,
227+
update_asset_storage_system::<T>.system(),
228+
)
223229
.register_type::<Handle<T>>()
224230
.add_event::<AssetEvent<T>>()
225231
}

crates/bevy_asset/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod path;
1313

1414
pub use asset_server::*;
1515
pub use assets::*;
16-
use bevy_ecs::SystemStage;
16+
use bevy_ecs::{IntoSystem, SystemStage};
1717
use bevy_reflect::RegisterTypeBuilder;
1818
use bevy_tasks::IoTaskPool;
1919
pub use handle::*;
@@ -88,13 +88,13 @@ impl Plugin for AssetPlugin {
8888
.register_type::<HandleId>()
8989
.add_system_to_stage(
9090
bevy_app::stage::PRE_UPDATE,
91-
asset_server::free_unused_assets_system,
91+
asset_server::free_unused_assets_system.system(),
9292
);
9393

9494
#[cfg(all(
9595
feature = "filesystem_watcher",
9696
all(not(target_arch = "wasm32"), not(target_os = "android"))
9797
))]
98-
app.add_system_to_stage(stage::LOAD_ASSETS, io::filesystem_watcher_system);
98+
app.add_system_to_stage(stage::LOAD_ASSETS, io::filesystem_watcher_system.system());
9999
}
100100
}

crates/bevy_audio/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod prelude {
1212

1313
use bevy_app::prelude::*;
1414
use bevy_asset::AddAsset;
15+
use bevy_ecs::IntoSystem;
1516

1617
/// Adds support for audio playback to an App
1718
#[derive(Default)]
@@ -23,6 +24,9 @@ impl Plugin for AudioPlugin {
2324
.add_asset::<AudioSource>()
2425
.init_asset_loader::<Mp3Loader>()
2526
.init_resource::<Audio<AudioSource>>()
26-
.add_system_to_stage(stage::POST_UPDATE, play_queued_audio_system::<AudioSource>);
27+
.add_system_to_stage(
28+
stage::POST_UPDATE,
29+
play_queued_audio_system::<AudioSource>.system(),
30+
);
2731
}
2832
}

crates/bevy_core/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod time;
66

77
use std::ops::Range;
88

9+
use bevy_ecs::IntoSystem;
910
use bevy_reflect::RegisterTypeBuilder;
1011
pub use bytes::*;
1112
pub use float_ord::*;
@@ -37,7 +38,7 @@ impl Plugin for CorePlugin {
3738
.register_type::<Option<String>>()
3839
.register_type::<Range<f32>>()
3940
.register_type::<Timer>()
40-
.add_system_to_stage(stage::FIRST, time_system)
41-
.add_system_to_stage(stage::PRE_UPDATE, entity_labels_system);
41+
.add_system_to_stage(stage::FIRST, time_system.system())
42+
.add_system_to_stage(stage::PRE_UPDATE, entity_labels_system.system());
4243
}
4344
}

crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{Diagnostic, DiagnosticId, Diagnostics};
22
use bevy_app::prelude::*;
33
use bevy_core::Time;
4-
use bevy_ecs::{Res, ResMut};
4+
use bevy_ecs::{IntoSystem, Res, ResMut};
55

66
/// Adds "frame time" diagnostic to an App, specifically "frame time", "fps" and "frame count"
77
#[derive(Default)]
@@ -13,9 +13,9 @@ pub struct FrameTimeDiagnosticsState {
1313

1414
impl Plugin for FrameTimeDiagnosticsPlugin {
1515
fn build(&self, app: &mut bevy_app::AppBuilder) {
16-
app.add_startup_system(Self::setup_system)
16+
app.add_startup_system(Self::setup_system.system())
1717
.add_resource(FrameTimeDiagnosticsState { frame_count: 0.0 })
18-
.add_system(Self::diagnostic_system);
18+
.add_system(Self::diagnostic_system.system());
1919
}
2020
}
2121

crates/bevy_diagnostic/src/print_diagnostics_plugin.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{Diagnostic, DiagnosticId, Diagnostics};
22
use bevy_app::prelude::*;
33
use bevy_core::{Time, Timer};
4-
use bevy_ecs::{Res, ResMut};
4+
use bevy_ecs::{IntoSystem, Res, ResMut};
55
use bevy_utils::Duration;
66

77
/// An App Plugin that prints diagnostics to the console
@@ -35,9 +35,12 @@ impl Plugin for PrintDiagnosticsPlugin {
3535
});
3636

3737
if self.debug {
38-
app.add_system_to_stage(stage::POST_UPDATE, Self::print_diagnostics_debug_system);
38+
app.add_system_to_stage(
39+
stage::POST_UPDATE,
40+
Self::print_diagnostics_debug_system.system(),
41+
);
3942
} else {
40-
app.add_system_to_stage(stage::POST_UPDATE, Self::print_diagnostics_system);
43+
app.add_system_to_stage(stage::POST_UPDATE, Self::print_diagnostics_system.system());
4144
}
4245
}
4346
}

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,21 +411,27 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
411411
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
412412

413413
let struct_name = &ast.ident;
414+
let fetch_struct_name = Ident::new(&format!("Fetch{}", struct_name), Span::call_site());
414415

415416
TokenStream::from(quote! {
416-
impl #impl_generics #path::SystemParam<()> for #struct_name#ty_generics #where_clause {
417+
pub struct #fetch_struct_name;
418+
impl #impl_generics #path::SystemParam for #struct_name#ty_generics #where_clause {
419+
type Fetch = #fetch_struct_name;
420+
}
421+
422+
impl #impl_generics #path::FetchSystemParam<'a> for #fetch_struct_name {
423+
type Item = #struct_name#ty_generics;
417424
fn init(system_state: &mut #path::SystemState, world: &#path::World, resources: &mut #path::Resources) {
418-
#(<#field_types as SystemParam<()>>::init(system_state, world, resources);)*
425+
#(<<#field_types as SystemParam>::Fetch as #path::FetchSystemParam>::init(system_state, world, resources);)*
419426
}
420427

421428
unsafe fn get_param(
422-
input: &mut Option<()>,
423-
system_state: &mut #path::SystemState,
424-
world: &#path::World,
425-
resources: &#path::Resources,
426-
) -> Option<Self> {
429+
system_state: &'a #path::SystemState,
430+
world: &'a #path::World,
431+
resources: &'a #path::Resources,
432+
) -> Option<Self::Item> {
427433
Some(#struct_name {
428-
#(#fields: <#field_types as SystemParam<()>>::get_param(input, system_state, world, resources)?,)*
434+
#(#fields: <<#field_types as SystemParam>::Fetch as #path::FetchSystemParam>::get_param(system_state, world, resources)?,)*
429435
#(#ignored_fields: <#ignored_field_types>::default(),)*
430436
})
431437
}

0 commit comments

Comments
 (0)