Skip to content

Commit

Permalink
Adopt a Fetch pattern for SystemParams (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
cart authored Dec 16, 2020
1 parent 51650f1 commit 841755a
Show file tree
Hide file tree
Showing 94 changed files with 633 additions and 482 deletions.
66 changes: 19 additions & 47 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Default for AppBuilder {
app_builder
.add_default_stages()
.add_event::<AppExit>()
.add_system_to_stage(stage::LAST, clear_trackers_system);
.add_system_to_stage(stage::LAST, clear_trackers_system.system());
app_builder
}
}
Expand Down Expand Up @@ -125,68 +125,48 @@ impl AppBuilder {
self
}

pub fn add_system<S, Params, IntoS>(&mut self, system: IntoS) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
pub fn add_system<S: System<In = (), Out = ()>>(&mut self, system: S) -> &mut Self {
self.add_system_to_stage(stage::UPDATE, system)
}

pub fn on_state_enter<T: Clone + Resource, S, Params, IntoS>(
pub fn on_state_enter<T: Clone + Resource, S: System<In = (), Out = ()>>(
&mut self,
stage: &str,
state: T,
system: IntoS,
) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
system: S,
) -> &mut Self {
self.stage(stage, |stage: &mut StateStage<T>| {
stage.on_state_enter(state, system)
})
}

pub fn on_state_update<T: Clone + Resource, S, Params, IntoS>(
pub fn on_state_update<T: Clone + Resource, S: System<In = (), Out = ()>>(
&mut self,
stage: &str,
state: T,
system: IntoS,
) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
system: S,
) -> &mut Self {
self.stage(stage, |stage: &mut StateStage<T>| {
stage.on_state_update(state, system)
})
}

pub fn on_state_exit<T: Clone + Resource, S, Params, IntoS>(
pub fn on_state_exit<T: Clone + Resource, S: System<In = (), Out = ()>>(
&mut self,
stage: &str,
state: T,
system: IntoS,
) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
system: S,
) -> &mut Self {
self.stage(stage, |stage: &mut StateStage<T>| {
stage.on_state_exit(state, system)
})
}

pub fn add_startup_system_to_stage<S, Params, IntoS>(
pub fn add_startup_system_to_stage<S: System<In = (), Out = ()>>(
&mut self,
stage_name: &'static str,
system: IntoS,
) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
system: S,
) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
Expand All @@ -195,11 +175,7 @@ impl AppBuilder {
self
}

pub fn add_startup_system<S, Params, IntoS>(&mut self, system: IntoS) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
pub fn add_startup_system<S: System<In = (), Out = ()>>(&mut self, system: S) -> &mut Self {
self.add_startup_system_to_stage(startup_stage::STARTUP, system)
}

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

pub fn add_system_to_stage<S, Params, IntoS>(
pub fn add_system_to_stage<S: System<In = (), Out = ()>>(
&mut self,
stage_name: &'static str,
system: IntoS,
) -> &mut Self
where
S: System<In = (), Out = ()>,
IntoS: IntoSystem<Params, S>,
{
system: S,
) -> &mut Self {
self.app.schedule.add_system_to_stage(stage_name, system);
self
}
Expand All @@ -239,7 +211,7 @@ impl AppBuilder {
T: Send + Sync + 'static,
{
self.add_resource(Events::<T>::default())
.add_system_to_stage(stage::EVENT, Events::<T>::update_system)
.add_system_to_stage(stage::EVENT, Events::<T>::update_system.system())
}

/// Adds a resource to the current [App] and overwrites any resource previously added of the same type.
Expand Down
12 changes: 9 additions & 3 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
update_asset_storage_system, Asset, AssetLoader, AssetServer, Handle, HandleId, RefChange,
};
use bevy_app::{prelude::Events, AppBuilder};
use bevy_ecs::{FromResources, ResMut};
use bevy_ecs::{FromResources, IntoSystem, ResMut};
use bevy_reflect::RegisterTypeBuilder;
use bevy_utils::HashMap;
use crossbeam_channel::Sender;
Expand Down Expand Up @@ -218,8 +218,14 @@ impl AddAsset for AppBuilder {
};

self.add_resource(assets)
.add_system_to_stage(super::stage::ASSET_EVENTS, Assets::<T>::asset_event_system)
.add_system_to_stage(crate::stage::LOAD_ASSETS, update_asset_storage_system::<T>)
.add_system_to_stage(
super::stage::ASSET_EVENTS,
Assets::<T>::asset_event_system.system(),
)
.add_system_to_stage(
crate::stage::LOAD_ASSETS,
update_asset_storage_system::<T>.system(),
)
.register_type::<Handle<T>>()
.add_event::<AssetEvent<T>>()
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod path;

pub use asset_server::*;
pub use assets::*;
use bevy_ecs::SystemStage;
use bevy_ecs::{IntoSystem, SystemStage};
use bevy_reflect::RegisterTypeBuilder;
use bevy_tasks::IoTaskPool;
pub use handle::*;
Expand Down Expand Up @@ -88,13 +88,13 @@ impl Plugin for AssetPlugin {
.register_type::<HandleId>()
.add_system_to_stage(
bevy_app::stage::PRE_UPDATE,
asset_server::free_unused_assets_system,
asset_server::free_unused_assets_system.system(),
);

#[cfg(all(
feature = "filesystem_watcher",
all(not(target_arch = "wasm32"), not(target_os = "android"))
))]
app.add_system_to_stage(stage::LOAD_ASSETS, io::filesystem_watcher_system);
app.add_system_to_stage(stage::LOAD_ASSETS, io::filesystem_watcher_system.system());
}
}
6 changes: 5 additions & 1 deletion crates/bevy_audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod prelude {

use bevy_app::prelude::*;
use bevy_asset::AddAsset;
use bevy_ecs::IntoSystem;

/// Adds support for audio playback to an App
#[derive(Default)]
Expand All @@ -23,6 +24,9 @@ impl Plugin for AudioPlugin {
.add_asset::<AudioSource>()
.init_asset_loader::<Mp3Loader>()
.init_resource::<Audio<AudioSource>>()
.add_system_to_stage(stage::POST_UPDATE, play_queued_audio_system::<AudioSource>);
.add_system_to_stage(
stage::POST_UPDATE,
play_queued_audio_system::<AudioSource>.system(),
);
}
}
5 changes: 3 additions & 2 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod time;

use std::ops::Range;

use bevy_ecs::IntoSystem;
use bevy_reflect::RegisterTypeBuilder;
pub use bytes::*;
pub use float_ord::*;
Expand Down Expand Up @@ -37,7 +38,7 @@ impl Plugin for CorePlugin {
.register_type::<Option<String>>()
.register_type::<Range<f32>>()
.register_type::<Timer>()
.add_system_to_stage(stage::FIRST, time_system)
.add_system_to_stage(stage::PRE_UPDATE, entity_labels_system);
.add_system_to_stage(stage::FIRST, time_system.system())
.add_system_to_stage(stage::PRE_UPDATE, entity_labels_system.system());
}
}
6 changes: 3 additions & 3 deletions crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_app::prelude::*;
use bevy_core::Time;
use bevy_ecs::{Res, ResMut};
use bevy_ecs::{IntoSystem, Res, ResMut};

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

impl Plugin for FrameTimeDiagnosticsPlugin {
fn build(&self, app: &mut bevy_app::AppBuilder) {
app.add_startup_system(Self::setup_system)
app.add_startup_system(Self::setup_system.system())
.add_resource(FrameTimeDiagnosticsState { frame_count: 0.0 })
.add_system(Self::diagnostic_system);
.add_system(Self::diagnostic_system.system());
}
}

Expand Down
9 changes: 6 additions & 3 deletions crates/bevy_diagnostic/src/print_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_app::prelude::*;
use bevy_core::{Time, Timer};
use bevy_ecs::{Res, ResMut};
use bevy_ecs::{IntoSystem, Res, ResMut};
use bevy_utils::Duration;

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

if self.debug {
app.add_system_to_stage(stage::POST_UPDATE, Self::print_diagnostics_debug_system);
app.add_system_to_stage(
stage::POST_UPDATE,
Self::print_diagnostics_debug_system.system(),
);
} else {
app.add_system_to_stage(stage::POST_UPDATE, Self::print_diagnostics_system);
app.add_system_to_stage(stage::POST_UPDATE, Self::print_diagnostics_system.system());
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,27 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

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

TokenStream::from(quote! {
impl #impl_generics #path::SystemParam<()> for #struct_name#ty_generics #where_clause {
pub struct #fetch_struct_name;
impl #impl_generics #path::SystemParam for #struct_name#ty_generics #where_clause {
type Fetch = #fetch_struct_name;
}

impl #impl_generics #path::FetchSystemParam<'a> for #fetch_struct_name {
type Item = #struct_name#ty_generics;
fn init(system_state: &mut #path::SystemState, world: &#path::World, resources: &mut #path::Resources) {
#(<#field_types as SystemParam<()>>::init(system_state, world, resources);)*
#(<<#field_types as SystemParam>::Fetch as #path::FetchSystemParam>::init(system_state, world, resources);)*
}

unsafe fn get_param(
input: &mut Option<()>,
system_state: &mut #path::SystemState,
world: &#path::World,
resources: &#path::Resources,
) -> Option<Self> {
system_state: &'a #path::SystemState,
world: &'a #path::World,
resources: &'a #path::Resources,
) -> Option<Self::Item> {
Some(#struct_name {
#(#fields: <#field_types as SystemParam<()>>::get_param(input, system_state, world, resources)?,)*
#(#fields: <<#field_types as SystemParam>::Fetch as #path::FetchSystemParam>::get_param(system_state, world, resources)?,)*
#(#ignored_fields: <#ignored_field_types>::default(),)*
})
}
Expand Down
Loading

0 comments on commit 841755a

Please sign in to comment.