Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Optional .system(), part 3 #2422

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub mod prelude {
Schedule, Stage, StageLabel, State, SystemLabel, SystemSet, SystemStage,
},
system::{
Commands, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem, Local, NonSend,
NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System,
Commands, ConfigurableSystem, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem,
Local, NonSend, NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System,
},
world::{FromWorld, Mut, World},
};
Expand Down
36 changes: 36 additions & 0 deletions crates/bevy_ecs/src/system/function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,42 @@ impl<In, Out, Param: SystemParam, Marker, F> FunctionSystem<In, Out, Param, Mark
self
}
}

/// Provides `my_system.config(...)` API.
pub trait ConfigurableSystem<In, Out, Param: SystemParam, Marker>:
IntoSystem<In, Out, (IsFunctionSystem, Param, Marker)>
{
/// See [`FunctionSystem::config()`](crate::system::FunctionSystem::config).
fn config(
self,
f: impl FnOnce(&mut <Param::Fetch as SystemParamState>::Config),
) -> Self::System;
}

impl<In, Out, Param: SystemParam, Marker, F> ConfigurableSystem<In, Out, Param, Marker> for F
where
In: 'static,
Out: 'static,
Param: SystemParam + 'static,
Marker: 'static,
F: SystemParamFunction<In, Out, Param, Marker>
+ IntoSystem<
In,
Out,
(IsFunctionSystem, Param, Marker),
System = FunctionSystem<In, Out, Param, Marker, F>,
> + Send
+ Sync
+ 'static,
{
fn config(
self,
f: impl FnOnce(&mut <<Param as SystemParam>::Fetch as SystemParamState>::Config),
) -> Self::System {
self.system().config(f)
}
}

pub struct IsFunctionSystem;

impl<In, Out, Param, Marker, F> IntoSystem<In, Out, (IsFunctionSystem, Param, Marker)> for F
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ mod tests {
query::{Added, Changed, Or, With, Without},
schedule::{Schedule, Stage, SystemStage},
system::{
IntoExclusiveSystem, IntoSystem, Local, Query, QuerySet, RemovedComponents, Res,
ResMut, System, SystemState,
ConfigurableSystem, IntoExclusiveSystem, IntoSystem, Local, Query, QuerySet,
RemovedComponents, Res, ResMut, System, SystemState,
},
world::{FromWorld, World},
};
Expand Down Expand Up @@ -372,7 +372,13 @@ mod tests {

// ensure the system actually ran
assert!(*world.get_resource::<bool>().unwrap());

// Now do the same with omitted `.system()`.
world.insert_resource(false);
run_system(&mut world, sys.config(|config| config.0 = Some(42)));
assert!(*world.get_resource::<bool>().unwrap());
}

#[test]
fn world_collections_system() {
let mut world = World::default();
Expand Down