Skip to content

Commit 80b08ea

Browse files
committed
Allow higher order systems (#4833)
# Objective - Higher order system could not be created by users. - However, a simple change to `SystemParamFunction` allows this. - Higher order systems in this case mean functions which return systems created using other systems, such as `chain` (which is basically equivalent to map) ## Solution - Change `SystemParamFunction` to be a safe abstraction over `FnMut([In<In>,] ...params)->Out`. - Note that I believe `SystemParamFunction` should not have been counted as part of our public api before this PR. - This is because its only use was an unsafe function without an actionable safety comment. - The safety comment was basically 'call this within bevy code'. - I also believe that there are no external users in its current form. - A quick search on Google and in the discord confirmed this. ## See also - #4666, which uses this and subsumes the example here --- ## Changelog ### Added - `SystemParamFunction`, which can be used to create higher order systems.
1 parent c46691c commit 80b08ea

File tree

2 files changed

+100
-24
lines changed

2 files changed

+100
-24
lines changed

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 94 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
schedule::SystemLabel,
88
system::{
99
check_system_change_tick, ReadOnlySystemParamFetch, System, SystemParam, SystemParamFetch,
10-
SystemParamState,
10+
SystemParamItem, SystemParamState,
1111
},
1212
world::{World, WorldId},
1313
};
@@ -346,6 +346,16 @@ where
346346
}
347347
}
348348

349+
impl<In, Out, Param, Marker, F> FunctionSystem<In, Out, Param, Marker, F>
350+
where
351+
Param: SystemParam,
352+
{
353+
/// Message shown when a system isn't initialised
354+
// When lines get too long, rustfmt can sometimes refuse to format them.
355+
// Work around this by storing the message separately.
356+
const PARAM_MESSAGE: &'static str = "System's param_state was not found. Did you forget to initialize this system before running it?";
357+
}
358+
349359
impl<In, Out, Param, Marker, F> System for FunctionSystem<In, Out, Param, Marker, F>
350360
where
351361
In: 'static,
@@ -380,20 +390,25 @@ where
380390
#[inline]
381391
unsafe fn run_unsafe(&mut self, input: Self::In, world: &World) -> Self::Out {
382392
let change_tick = world.increment_change_tick();
383-
let out = self.func.run(
384-
input,
385-
self.param_state.as_mut().expect("System's param_state was not found. Did you forget to initialize this system before running it?"),
393+
394+
// Safety:
395+
// We update the archetype component access correctly based on `Param`'s requirements
396+
// in `update_archetype_component_access`.
397+
// Our caller upholds the requirements.
398+
let params = <Param as SystemParam>::Fetch::get_param(
399+
self.param_state.as_mut().expect(Self::PARAM_MESSAGE),
386400
&self.system_meta,
387401
world,
388402
change_tick,
389403
);
404+
let out = self.func.run(input, params);
390405
self.system_meta.last_change_tick = change_tick;
391406
out
392407
}
393408

394409
#[inline]
395410
fn apply_buffers(&mut self, world: &mut World) {
396-
let param_state = self.param_state.as_mut().expect("System's param_state was not found. Did you forget to initialize this system before running it?");
411+
let param_state = self.param_state.as_mut().expect(Self::PARAM_MESSAGE);
397412
param_state.apply(world);
398413
}
399414

@@ -474,19 +489,71 @@ impl<T> SystemLabel for SystemTypeIdLabel<T> {
474489
}
475490

476491
/// A trait implemented for all functions that can be used as [`System`]s.
492+
///
493+
/// This trait can be useful for making your own systems which accept other systems,
494+
/// sometimes called higher order systems.
495+
///
496+
/// This should be used in combination with [`ParamSet`] when calling other systems
497+
/// within your system.
498+
/// Using [`ParamSet`] in this case avoids [`SystemParam`] collisions.
499+
///
500+
/// # Example
501+
///
502+
/// To create something like [`ChainSystem`], but in entirely safe code.
503+
///
504+
/// ```rust
505+
/// use std::num::ParseIntError;
506+
///
507+
/// use bevy_ecs::prelude::*;
508+
/// use bevy_ecs::system::{SystemParam, SystemParamItem};
509+
///
510+
/// // Unfortunately, we need all of these generics. `A` is the first system, with its
511+
/// // parameters and marker type required for coherence. `B` is the second system, and
512+
/// // the other generics are for the input/output types of A and B.
513+
/// /// Chain creates a new system which calls `a`, then calls `b` with the output of `a`
514+
/// pub fn chain<AIn, Shared, BOut, A, AParam, AMarker, B, BParam, BMarker>(
515+
/// mut a: A,
516+
/// mut b: B,
517+
/// ) -> impl FnMut(In<AIn>, ParamSet<(SystemParamItem<AParam>, SystemParamItem<BParam>)>) -> BOut
518+
/// where
519+
/// // We need A and B to be systems, add those bounds
520+
/// A: SystemParamFunction<AIn, Shared, AParam, AMarker>,
521+
/// B: SystemParamFunction<Shared, BOut, BParam, BMarker>,
522+
/// AParam: SystemParam,
523+
/// BParam: SystemParam,
524+
/// {
525+
/// // The type of `params` is inferred based on the return of this function above
526+
/// move |In(a_in), mut params| {
527+
/// let shared = a.run(a_in, params.p0());
528+
/// b.run(shared, params.p1())
529+
/// }
530+
/// }
531+
///
532+
/// // Usage example for `chain`:
533+
/// fn main() {
534+
/// let mut world = World::default();
535+
/// world.insert_resource(Message("42".to_string()));
536+
///
537+
/// // chain the `parse_message_system`'s output into the `filter_system`s input
538+
/// let mut chained_system = IntoSystem::into_system(chain(parse_message, filter));
539+
/// chained_system.initialize(&mut world);
540+
/// assert_eq!(chained_system.run((), &mut world), Some(42));
541+
/// }
542+
///
543+
/// struct Message(String);
544+
///
545+
/// fn parse_message(message: Res<Message>) -> Result<usize, ParseIntError> {
546+
/// message.0.parse::<usize>()
547+
/// }
548+
///
549+
/// fn filter(In(result): In<Result<usize, ParseIntError>>) -> Option<usize> {
550+
/// result.ok().filter(|&n| n < 100)
551+
/// }
552+
/// ```
553+
/// [`ChainSystem`]: crate::system::ChainSystem
554+
/// [`ParamSet`]: crate::system::ParamSet
477555
pub trait SystemParamFunction<In, Out, Param: SystemParam, Marker>: Send + Sync + 'static {
478-
/// # Safety
479-
///
480-
/// This call might access any of the input parameters in an unsafe way. Make sure the data
481-
/// access is safe in the context of the system scheduler.
482-
unsafe fn run(
483-
&mut self,
484-
input: In,
485-
state: &mut Param::Fetch,
486-
system_meta: &SystemMeta,
487-
world: &World,
488-
change_tick: u32,
489-
) -> Out;
556+
fn run(&mut self, input: In, param_value: SystemParamItem<Param>) -> Out;
490557
}
491558

492559
macro_rules! impl_system_function {
@@ -496,20 +563,21 @@ macro_rules! impl_system_function {
496563
where
497564
for <'a> &'a mut Func:
498565
FnMut($($param),*) -> Out +
499-
FnMut($(<<$param as SystemParam>::Fetch as SystemParamFetch>::Item),*) -> Out, Out: 'static
566+
FnMut($(SystemParamItem<$param>),*) -> Out, Out: 'static
500567
{
501568
#[inline]
502-
unsafe fn run(&mut self, _input: (), state: &mut <($($param,)*) as SystemParam>::Fetch, system_meta: &SystemMeta, world: &World, change_tick: u32) -> Out {
503-
// Yes, this is strange, but rustc fails to compile this impl
504-
// without using this function.
569+
fn run(&mut self, _input: (), param_value: SystemParamItem< ($($param,)*)>) -> Out {
570+
// Yes, this is strange, but `rustc` fails to compile this impl
571+
// without using this function. It fails to recognise that `func`
572+
// is a function, potentially because of the multiple impls of `FnMut`
505573
#[allow(clippy::too_many_arguments)]
506574
fn call_inner<Out, $($param,)*>(
507575
mut f: impl FnMut($($param,)*)->Out,
508576
$($param: $param,)*
509577
)->Out{
510578
f($($param,)*)
511579
}
512-
let ($($param,)*) = <<($($param,)*) as SystemParam>::Fetch as SystemParamFetch>::get_param(state, system_meta, world, change_tick);
580+
let ($($param,)*) = param_value;
513581
call_inner(self, $($param),*)
514582
}
515583
}
@@ -522,7 +590,7 @@ macro_rules! impl_system_function {
522590
FnMut(In<Input>, $(<<$param as SystemParam>::Fetch as SystemParamFetch>::Item),*) -> Out, Out: 'static
523591
{
524592
#[inline]
525-
unsafe fn run(&mut self, input: Input, state: &mut <($($param,)*) as SystemParam>::Fetch, system_meta: &SystemMeta, world: &World, change_tick: u32) -> Out {
593+
fn run(&mut self, input: Input, param_value: SystemParamItem< ($($param,)*)>) -> Out {
526594
#[allow(clippy::too_many_arguments)]
527595
fn call_inner<Input, Out, $($param,)*>(
528596
mut f: impl FnMut(In<Input>, $($param,)*)->Out,
@@ -531,13 +599,15 @@ macro_rules! impl_system_function {
531599
)->Out{
532600
f(input, $($param,)*)
533601
}
534-
let ($($param,)*) = <<($($param,)*) as SystemParam>::Fetch as SystemParamFetch>::get_param(state, system_meta, world, change_tick);
602+
let ($($param,)*) = param_value;
535603
call_inner(self, In(input), $($param),*)
536604
}
537605
}
538606
};
539607
}
540608

609+
// Note that we rely on the highest impl to be <= the highest order of the tuple impls
610+
// of `SystemParam` created.
541611
all_tuples!(impl_system_function, 0, 16, F);
542612

543613
/// Used to implicitly convert systems to their default labels. For example, it will convert

crates/bevy_ecs/src/system/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,15 @@ pub use system::*;
8383
pub use system_chaining::*;
8484
pub use system_param::*;
8585

86+
/// Ensure that a given function is a system
87+
///
88+
/// This should be used when writing doc examples,
89+
/// to confirm that systems used in an example are
90+
/// valid systems
8691
pub fn assert_is_system<In, Out, Params, S: IntoSystem<In, Out, Params>>(sys: S) {
8792
if false {
8893
// Check it can be converted into a system
94+
// TODO: This should ensure that the system has no conflicting system params
8995
IntoSystem::into_system(sys);
9096
}
9197
}

0 commit comments

Comments
 (0)