Skip to content

Commit acaf4e2

Browse files
committed
Implement System::system_metas(&self) and System::system_metas_mut(&mut self)
for system exploration and configuration Part of #16168
1 parent 2285632 commit acaf4e2

File tree

4 files changed

+174
-4
lines changed

4 files changed

+174
-4
lines changed

crates/bevy_ecs/src/system/adapter_system.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use alloc::vec::Vec;
22
use bevy_utils::prelude::DebugName;
33

4-
use super::{IntoSystem, ReadOnlySystem, RunSystemError, System, SystemParamValidationError};
4+
use super::{
5+
IntoSystem, ReadOnlySystem, RunSystemError, System, SystemMeta, SystemMetaProvider,
6+
SystemParamValidationError,
7+
};
58
use crate::{
69
schedule::InternedSystemSet,
710
system::{input::SystemInput, SystemIn},
@@ -216,3 +219,28 @@ where
216219
run_system(input).map(self)
217220
}
218221
}
222+
223+
impl<Func, S> SystemMetaProvider for AdapterSystem<Func, S>
224+
where
225+
Func: Adapt<S>,
226+
S: System + SystemMetaProvider,
227+
{
228+
fn system_metas(&self) -> Vec<&SystemMeta> {
229+
self.system.system_metas()
230+
}
231+
232+
fn system_metas_mut(&mut self) -> Vec<&mut SystemMeta> {
233+
self.system.system_metas_mut()
234+
}
235+
236+
fn extend_with_system_metas<'a>(&'a self, extendable: &mut impl Extend<&'a SystemMeta>) {
237+
self.system.extend_with_system_metas(extendable);
238+
}
239+
240+
fn extend_with_system_metas_mut<'a>(
241+
&'a mut self,
242+
extendable_mut: &mut impl Extend<&'a mut SystemMeta>,
243+
) {
244+
self.system.extend_with_system_metas_mut(extendable_mut);
245+
}
246+
}

crates/bevy_ecs/src/system/combinator.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
world::unsafe_world_cell::UnsafeWorldCell,
1212
};
1313

14-
use super::{IntoSystem, ReadOnlySystem, RunSystemError, System};
14+
use super::{IntoSystem, ReadOnlySystem, RunSystemError, System, SystemMeta, SystemMetaProvider};
1515

1616
/// Customizes the behavior of a [`CombinatorSystem`].
1717
///
@@ -258,6 +258,37 @@ where
258258
}
259259
}
260260

261+
impl<Func, A, B> SystemMetaProvider for CombinatorSystem<Func, A, B>
262+
where
263+
Func: Combine<A, B> + 'static,
264+
A: System + SystemMetaProvider,
265+
B: System + SystemMetaProvider,
266+
{
267+
fn system_metas(&self) -> Vec<&SystemMeta> {
268+
let mut vec: Vec<&SystemMeta> = Vec::new();
269+
self.a.extend_with_system_metas(&mut vec);
270+
self.b.extend_with_system_metas(&mut vec);
271+
vec
272+
}
273+
274+
fn system_metas_mut(&mut self) -> Vec<&mut SystemMeta> {
275+
let mut vec_mut: Vec<&mut SystemMeta> = Vec::new();
276+
self.a.extend_with_system_metas_mut(&mut vec_mut);
277+
self.b.extend_with_system_metas_mut(&mut vec_mut);
278+
vec_mut
279+
}
280+
281+
fn extend_with_system_metas<'a>(&'a self, vec: &mut Vec<&'a SystemMeta>) {
282+
self.a.extend_with_system_metas(vec);
283+
self.b.extend_with_system_metas(vec);
284+
}
285+
286+
fn extend_with_system_metas_mut<'a>(&'a mut self, vec_mut: &mut Vec<&'a mut SystemMeta>) {
287+
self.a.extend_with_system_metas_mut(vec_mut);
288+
self.b.extend_with_system_metas_mut(vec_mut);
289+
}
290+
}
291+
261292
/// An [`IntoSystem`] creating an instance of [`PipeSystem`].
262293
#[derive(Clone)]
263294
pub struct IntoPipeSystem<A, B> {
@@ -437,6 +468,39 @@ where
437468
}
438469
}
439470

471+
impl<A, B> SystemMetaProvider for PipeSystem<A, B>
472+
where
473+
A: System + SystemMetaProvider,
474+
B: System + SystemMetaProvider,
475+
{
476+
fn system_metas(&self) -> Vec<&SystemMeta> {
477+
let mut vec: Vec<&SystemMeta> = Vec::new();
478+
self.a.extend_with_system_metas(&mut vec);
479+
self.b.extend_with_system_metas(&mut vec);
480+
vec
481+
}
482+
483+
fn system_metas_mut(&mut self) -> Vec<&mut SystemMeta> {
484+
let mut vec_mut: Vec<&mut SystemMeta> = Vec::new();
485+
self.a.extend_with_system_metas_mut(&mut vec_mut);
486+
self.b.extend_with_system_metas_mut(&mut vec_mut);
487+
vec_mut
488+
}
489+
490+
fn extend_with_system_metas<'a>(&'a self, extendable: &mut impl Extend<&'a SystemMeta>) {
491+
self.a.extend_with_system_metas(extendable);
492+
self.b.extend_with_system_metas(extendable);
493+
}
494+
495+
fn extend_with_system_metas_mut<'a>(
496+
&'a mut self,
497+
extendable_mut: &mut impl Extend<&'a mut SystemMeta>,
498+
) {
499+
self.a.extend_with_system_metas_mut(extendable_mut);
500+
self.b.extend_with_system_metas_mut(extendable_mut);
501+
}
502+
}
503+
440504
/// SAFETY: Both systems are read-only, so any system created by piping them will only read from the world.
441505
unsafe impl<A, B> ReadOnlySystem for PipeSystem<A, B>
442506
where

crates/bevy_ecs/src/system/exclusive_function_system.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ use crate::{
1212

1313
use alloc::{borrow::Cow, vec, vec::Vec};
1414
use bevy_utils::prelude::DebugName;
15-
use core::marker::PhantomData;
15+
use core::{iter, marker::PhantomData};
1616
use variadics_please::all_tuples;
1717

1818
use super::{RunSystemError, SystemParamValidationError, SystemStateFlags};
1919

20+
use super::SystemMetaProvider;
21+
2022
/// A function system that runs with exclusive [`World`] access.
2123
///
2224
/// You get this by calling [`IntoSystem::into_system`] on a function that only accepts
@@ -205,6 +207,35 @@ where
205207
}
206208
}
207209

210+
impl<Marker, Out, F> SystemMetaProvider for ExclusiveFunctionSystem<Marker, Out, F>
211+
where
212+
Marker: 'static,
213+
F: ExclusiveSystemParamFunction<Marker>,
214+
{
215+
fn system_metas(&self) -> Vec<&SystemMeta> {
216+
let mut vec: Vec<&SystemMeta> = Vec::new();
217+
self.extend_with_system_metas(&mut vec);
218+
vec
219+
}
220+
221+
fn system_metas_mut(&mut self) -> Vec<&mut SystemMeta> {
222+
let mut vec: Vec<&mut SystemMeta> = Vec::new();
223+
self.extend_with_system_metas_mut(&mut vec);
224+
vec
225+
}
226+
227+
fn extend_with_system_metas<'a>(&'a self, extendable: &mut impl Extend<&'a SystemMeta>) {
228+
extendable.extend(iter::once(&self.system_meta));
229+
}
230+
231+
fn extend_with_system_metas_mut<'a>(
232+
&'a mut self,
233+
extendable_mut: &mut impl Extend<&'a mut SystemMeta>,
234+
) {
235+
extendable_mut.extend(iter::once(&mut self.system_meta));
236+
}
237+
}
238+
208239
/// A trait implemented for all exclusive system functions that can be used as [`System`]s.
209240
///
210241
/// This trait can be useful for making your own systems which accept other systems,

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414

1515
use alloc::{borrow::Cow, vec, vec::Vec};
1616
use bevy_utils::prelude::DebugName;
17-
use core::marker::PhantomData;
17+
use core::{iter, marker::PhantomData};
1818
use variadics_please::all_tuples;
1919

2020
#[cfg(feature = "trace")]
@@ -39,6 +39,24 @@ pub struct SystemMeta {
3939
pub(crate) commands_span: Span,
4040
}
4141

42+
/// Trait for retrieving system metas.
43+
pub trait SystemMetaProvider {
44+
/// Returns immutable references of system metas as a vector.
45+
fn system_metas(&self) -> Vec<&SystemMeta>;
46+
47+
/// Returns mutable references of system metas as a vector.
48+
fn system_metas_mut(&mut self) -> Vec<&mut SystemMeta>;
49+
50+
/// Helper method for `system_metas`.
51+
fn extend_with_system_metas<'a>(&'a self, extendable: &mut impl Extend<&'a SystemMeta>);
52+
53+
/// Helper method for `system_metas_mut`.
54+
fn extend_with_system_metas_mut<'a>(
55+
&'a mut self,
56+
extendable_mut: &mut impl Extend<&'a mut SystemMeta>,
57+
);
58+
}
59+
4260
impl SystemMeta {
4361
pub(crate) fn new<T>() -> Self {
4462
let name = DebugName::type_name::<T>();
@@ -807,6 +825,35 @@ where
807825
{
808826
}
809827

828+
impl<Marker, Out, F> SystemMetaProvider for FunctionSystem<Marker, Out, F>
829+
where
830+
Marker: 'static,
831+
F: SystemParamFunction<Marker>,
832+
{
833+
fn system_metas(&self) -> Vec<&SystemMeta> {
834+
let mut vec: Vec<&SystemMeta> = Vec::new();
835+
self.extend_with_system_metas(&mut vec);
836+
vec
837+
}
838+
839+
fn system_metas_mut(&mut self) -> Vec<&mut SystemMeta> {
840+
let mut vec: Vec<&mut SystemMeta> = Vec::new();
841+
self.extend_with_system_metas_mut(&mut vec);
842+
vec
843+
}
844+
845+
fn extend_with_system_metas<'a>(&'a self, extendable: &mut impl Extend<&'a SystemMeta>) {
846+
extendable.extend(iter::once(&self.system_meta));
847+
}
848+
849+
fn extend_with_system_metas_mut<'a>(
850+
&'a mut self,
851+
extendable_mut: &mut impl Extend<&'a mut SystemMeta>,
852+
) {
853+
extendable_mut.extend(iter::once(&mut self.system_meta));
854+
}
855+
}
856+
810857
/// A trait implemented for all functions that can be used as [`System`]s.
811858
///
812859
/// This trait can be useful for making your own systems which accept other systems,

0 commit comments

Comments
 (0)