Support piping exclusive systems (#7023)
# Objective Fix #5248. ## Solution Support `In<T>` parameters and allow returning arbitrary types in exclusive systems. --- ## Changelog - Exclusive systems may now be used with system piping. ## Migration Guide Exclusive systems (systems that access `&mut World`) now support system piping, so the `ExclusiveSystemParamFunction` trait now has generics for the `In`put and `Out`put types. ```rust // Before fn my_generic_system<T, Param>(system_function: T) where T: ExclusiveSystemParamFunction<Param> { ... } // After fn my_generic_system<T, In, Out, Param>(system_function: T) where T: ExclusiveSystemParamFunction<In, Out, Param> { ... } ```
This commit is contained in:
parent
c56bbcb3b0
commit
38005b0702
@ -6,7 +6,7 @@ use crate::{
|
|||||||
schedule::{SystemLabel, SystemLabelId},
|
schedule::{SystemLabel, SystemLabelId},
|
||||||
system::{
|
system::{
|
||||||
check_system_change_tick, AsSystemLabel, ExclusiveSystemParam, ExclusiveSystemParamItem,
|
check_system_change_tick, AsSystemLabel, ExclusiveSystemParam, ExclusiveSystemParamItem,
|
||||||
IntoSystem, System, SystemMeta, SystemTypeIdLabel,
|
In, InputMarker, IntoSystem, System, SystemMeta, SystemTypeIdLabel,
|
||||||
},
|
},
|
||||||
world::{World, WorldId},
|
world::{World, WorldId},
|
||||||
};
|
};
|
||||||
@ -19,7 +19,7 @@ use std::{borrow::Cow, marker::PhantomData};
|
|||||||
/// [`ExclusiveSystemParam`]s.
|
/// [`ExclusiveSystemParam`]s.
|
||||||
///
|
///
|
||||||
/// [`ExclusiveFunctionSystem`] must be `.initialized` before they can be run.
|
/// [`ExclusiveFunctionSystem`] must be `.initialized` before they can be run.
|
||||||
pub struct ExclusiveFunctionSystem<Param, Marker, F>
|
pub struct ExclusiveFunctionSystem<In, Out, Param, Marker, F>
|
||||||
where
|
where
|
||||||
Param: ExclusiveSystemParam,
|
Param: ExclusiveSystemParam,
|
||||||
{
|
{
|
||||||
@ -28,18 +28,21 @@ where
|
|||||||
system_meta: SystemMeta,
|
system_meta: SystemMeta,
|
||||||
world_id: Option<WorldId>,
|
world_id: Option<WorldId>,
|
||||||
// NOTE: PhantomData<fn()-> T> gives this safe Send/Sync impls
|
// NOTE: PhantomData<fn()-> T> gives this safe Send/Sync impls
|
||||||
marker: PhantomData<fn() -> Marker>,
|
marker: PhantomData<fn(In) -> (Out, Marker)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct IsExclusiveFunctionSystem;
|
pub struct IsExclusiveFunctionSystem;
|
||||||
|
|
||||||
impl<Param, Marker, F> IntoSystem<(), (), (IsExclusiveFunctionSystem, Param, Marker)> for F
|
impl<In, Out, Param, Marker, F> IntoSystem<In, Out, (IsExclusiveFunctionSystem, Param, Marker)>
|
||||||
|
for F
|
||||||
where
|
where
|
||||||
|
In: 'static,
|
||||||
|
Out: 'static,
|
||||||
Param: ExclusiveSystemParam + 'static,
|
Param: ExclusiveSystemParam + 'static,
|
||||||
Marker: 'static,
|
Marker: 'static,
|
||||||
F: ExclusiveSystemParamFunction<Param, Marker> + Send + Sync + 'static,
|
F: ExclusiveSystemParamFunction<In, Out, Param, Marker> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
type System = ExclusiveFunctionSystem<Param, Marker, F>;
|
type System = ExclusiveFunctionSystem<In, Out, Param, Marker, F>;
|
||||||
fn into_system(func: Self) -> Self::System {
|
fn into_system(func: Self) -> Self::System {
|
||||||
ExclusiveFunctionSystem {
|
ExclusiveFunctionSystem {
|
||||||
func,
|
func,
|
||||||
@ -53,14 +56,16 @@ where
|
|||||||
|
|
||||||
const PARAM_MESSAGE: &str = "System's param_state was not found. Did you forget to initialize this system before running it?";
|
const PARAM_MESSAGE: &str = "System's param_state was not found. Did you forget to initialize this system before running it?";
|
||||||
|
|
||||||
impl<Param, Marker, F> System for ExclusiveFunctionSystem<Param, Marker, F>
|
impl<In, Out, Param, Marker, F> System for ExclusiveFunctionSystem<In, Out, Param, Marker, F>
|
||||||
where
|
where
|
||||||
|
In: 'static,
|
||||||
|
Out: 'static,
|
||||||
Param: ExclusiveSystemParam + 'static,
|
Param: ExclusiveSystemParam + 'static,
|
||||||
Marker: 'static,
|
Marker: 'static,
|
||||||
F: ExclusiveSystemParamFunction<Param, Marker> + Send + Sync + 'static,
|
F: ExclusiveSystemParamFunction<In, Out, Param, Marker> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
type In = ();
|
type In = In;
|
||||||
type Out = ();
|
type Out = Out;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name(&self) -> Cow<'static, str> {
|
fn name(&self) -> Cow<'static, str> {
|
||||||
@ -90,7 +95,7 @@ where
|
|||||||
panic!("Cannot run exclusive systems with a shared World reference");
|
panic!("Cannot run exclusive systems with a shared World reference");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&mut self, _input: Self::In, world: &mut World) -> Self::Out {
|
fn run(&mut self, input: Self::In, world: &mut World) -> Self::Out {
|
||||||
let saved_last_tick = world.last_change_tick;
|
let saved_last_tick = world.last_change_tick;
|
||||||
world.last_change_tick = self.system_meta.last_change_tick;
|
world.last_change_tick = self.system_meta.last_change_tick;
|
||||||
|
|
||||||
@ -98,12 +103,14 @@ where
|
|||||||
self.param_state.as_mut().expect(PARAM_MESSAGE),
|
self.param_state.as_mut().expect(PARAM_MESSAGE),
|
||||||
&self.system_meta,
|
&self.system_meta,
|
||||||
);
|
);
|
||||||
self.func.run(world, params);
|
let out = self.func.run(world, input, params);
|
||||||
|
|
||||||
let change_tick = world.change_tick.get_mut();
|
let change_tick = world.change_tick.get_mut();
|
||||||
self.system_meta.last_change_tick = *change_tick;
|
self.system_meta.last_change_tick = *change_tick;
|
||||||
*change_tick = change_tick.wrapping_add(1);
|
*change_tick = change_tick.wrapping_add(1);
|
||||||
world.last_change_tick = saved_last_tick;
|
world.last_change_tick = saved_last_tick;
|
||||||
|
|
||||||
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -147,8 +154,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Param: ExclusiveSystemParam, Marker, T: ExclusiveSystemParamFunction<Param, Marker>>
|
impl<In, Out, Param, Marker, T> AsSystemLabel<(In, Out, Param, Marker, IsExclusiveFunctionSystem)>
|
||||||
AsSystemLabel<(Param, Marker, IsExclusiveFunctionSystem)> for T
|
for T
|
||||||
|
where
|
||||||
|
Param: ExclusiveSystemParam,
|
||||||
|
T: ExclusiveSystemParamFunction<In, Out, Param, Marker>,
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_system_label(&self) -> SystemLabelId {
|
fn as_system_label(&self) -> SystemLabelId {
|
||||||
@ -160,38 +170,70 @@ impl<Param: ExclusiveSystemParam, Marker, T: ExclusiveSystemParamFunction<Param,
|
|||||||
///
|
///
|
||||||
/// This trait can be useful for making your own systems which accept other systems,
|
/// This trait can be useful for making your own systems which accept other systems,
|
||||||
/// sometimes called higher order systems.
|
/// sometimes called higher order systems.
|
||||||
pub trait ExclusiveSystemParamFunction<Param: ExclusiveSystemParam, Marker>:
|
pub trait ExclusiveSystemParamFunction<In, Out, Param: ExclusiveSystemParam, Marker>:
|
||||||
Send + Sync + 'static
|
Send + Sync + 'static
|
||||||
{
|
{
|
||||||
fn run(&mut self, world: &mut World, param_value: ExclusiveSystemParamItem<Param>);
|
fn run(
|
||||||
|
&mut self,
|
||||||
|
world: &mut World,
|
||||||
|
input: In,
|
||||||
|
param_value: ExclusiveSystemParamItem<Param>,
|
||||||
|
) -> Out;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_exclusive_system_function {
|
macro_rules! impl_exclusive_system_function {
|
||||||
($($param: ident),*) => {
|
($($param: ident),*) => {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
impl<Func: Send + Sync + 'static, $($param: ExclusiveSystemParam),*> ExclusiveSystemParamFunction<($($param,)*), ()> for Func
|
impl<Out, Func: Send + Sync + 'static, $($param: ExclusiveSystemParam),*> ExclusiveSystemParamFunction<(), Out, ($($param,)*), ()> for Func
|
||||||
where
|
where
|
||||||
for <'a> &'a mut Func:
|
for <'a> &'a mut Func:
|
||||||
FnMut(&mut World, $($param),*) +
|
FnMut(&mut World, $($param),*) -> Out +
|
||||||
FnMut(&mut World, $(ExclusiveSystemParamItem<$param>),*)
|
FnMut(&mut World, $(ExclusiveSystemParamItem<$param>),*) -> Out,
|
||||||
|
Out: 'static,
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn run(&mut self, world: &mut World, param_value: ExclusiveSystemParamItem< ($($param,)*)>) {
|
fn run(&mut self, world: &mut World, _in: (), param_value: ExclusiveSystemParamItem< ($($param,)*)>) -> Out {
|
||||||
// Yes, this is strange, but `rustc` fails to compile this impl
|
// Yes, this is strange, but `rustc` fails to compile this impl
|
||||||
// without using this function. It fails to recognise that `func`
|
// without using this function. It fails to recognise that `func`
|
||||||
// is a function, potentially because of the multiple impls of `FnMut`
|
// is a function, potentially because of the multiple impls of `FnMut`
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn call_inner<$($param,)*>(
|
fn call_inner<Out, $($param,)*>(
|
||||||
mut f: impl FnMut(&mut World, $($param,)*),
|
mut f: impl FnMut(&mut World, $($param,)*) -> Out,
|
||||||
world: &mut World,
|
world: &mut World,
|
||||||
$($param: $param,)*
|
$($param: $param,)*
|
||||||
) {
|
) -> Out {
|
||||||
f(world, $($param,)*)
|
f(world, $($param,)*)
|
||||||
}
|
}
|
||||||
let ($($param,)*) = param_value;
|
let ($($param,)*) = param_value;
|
||||||
call_inner(self, world, $($param),*)
|
call_inner(self, world, $($param),*)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
impl<Input, Out, Func: Send + Sync + 'static, $($param: ExclusiveSystemParam),*> ExclusiveSystemParamFunction<Input, Out, ($($param,)*), InputMarker> for Func
|
||||||
|
where
|
||||||
|
for <'a> &'a mut Func:
|
||||||
|
FnMut(In<Input>, &mut World, $($param),*) -> Out +
|
||||||
|
FnMut(In<Input>, &mut World, $(ExclusiveSystemParamItem<$param>),*) -> Out,
|
||||||
|
Out: 'static,
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn run(&mut self, world: &mut World, input: Input, param_value: ExclusiveSystemParamItem< ($($param,)*)>) -> Out {
|
||||||
|
// Yes, this is strange, but `rustc` fails to compile this impl
|
||||||
|
// without using this function. It fails to recognise that `func`
|
||||||
|
// is a function, potentially because of the multiple impls of `FnMut`
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
fn call_inner<Input, Out, $($param,)*>(
|
||||||
|
mut f: impl FnMut(In<Input>, &mut World, $($param,)*) -> Out,
|
||||||
|
input: Input,
|
||||||
|
world: &mut World,
|
||||||
|
$($param: $param,)*
|
||||||
|
) -> Out {
|
||||||
|
f(In(input), world, $($param,)*)
|
||||||
|
}
|
||||||
|
let ($($param,)*) = param_value;
|
||||||
|
call_inner(self, input, world, $($param),*)
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// Note that we rely on the highest impl to be <= the highest order of the tuple impls
|
// Note that we rely on the highest impl to be <= the highest order of the tuple impls
|
||||||
|
|||||||
@ -316,7 +316,7 @@ where
|
|||||||
world_id: Option<WorldId>,
|
world_id: Option<WorldId>,
|
||||||
archetype_generation: ArchetypeGeneration,
|
archetype_generation: ArchetypeGeneration,
|
||||||
// NOTE: PhantomData<fn()-> T> gives this safe Send/Sync impls
|
// NOTE: PhantomData<fn()-> T> gives this safe Send/Sync impls
|
||||||
marker: PhantomData<fn() -> (In, Out, Marker)>,
|
marker: PhantomData<fn(In) -> (Out, Marker)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct IsFunctionSystem;
|
pub struct IsFunctionSystem;
|
||||||
|
|||||||
@ -433,8 +433,15 @@ pub mod adapter {
|
|||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Mocks an exclusive system that takes an input and returns an output.
|
||||||
|
fn exclusive_in_out<A, B>(_: In<A>, _: &mut World) -> B {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
assert_is_system(returning::<Result<u32, std::io::Error>>.pipe(unwrap));
|
assert_is_system(returning::<Result<u32, std::io::Error>>.pipe(unwrap));
|
||||||
assert_is_system(returning::<Option<()>>.pipe(ignore));
|
assert_is_system(returning::<Option<()>>.pipe(ignore));
|
||||||
assert_is_system(returning::<&str>.pipe(new(u64::from_str)).pipe(unwrap));
|
assert_is_system(returning::<&str>.pipe(new(u64::from_str)).pipe(unwrap));
|
||||||
|
assert_is_system(exclusive_in_out::<(), Result<(), std::io::Error>>.pipe(error));
|
||||||
|
assert_is_system(returning::<bool>.pipe(exclusive_in_out::<bool, ()>));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user