Skip to content

Commit

Permalink
Optional .system(), part 3 (#2422)
Browse files Browse the repository at this point in the history
# Objective

- Continue work of #2398 and #2403.
- Make `.system()` syntax optional when using `.config()` API.

## Solution

- Introduce new prelude trait, `ConfigurableSystem`, that shorthands `my_system.system().config(...)` as `my_system.config(...)`.
- Expand `configure_system_local` test to also cover the new syntax.
  • Loading branch information
Ratysz committed Jul 1, 2021
1 parent c8e2415 commit afb3323
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/lib.rs
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
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
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

0 comments on commit afb3323

Please sign in to comment.