From e8a5c6ad04014e36de18f9311bb9654c9fa51f13 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 15 Mar 2022 17:19:49 -0700 Subject: [PATCH 1/6] Auto-label function systems --- crates/bevy_ecs/src/lib.rs | 1 + .../src/schedule/system_descriptor.rs | 2 +- crates/bevy_ecs/src/system/function_system.rs | 24 ++++++++++++++++++- crates/bevy_ecs/src/system/system.rs | 5 ++++ examples/ecs/system_sets.rs | 23 +++--------------- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 8d5c59adfe849..cb559641d6a16 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -35,6 +35,7 @@ pub mod prelude { system::{ Commands, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem, Local, NonSend, NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System, + SystemParamFunction, }, world::{FromWorld, Mut, World}, }; diff --git a/crates/bevy_ecs/src/schedule/system_descriptor.rs b/crates/bevy_ecs/src/schedule/system_descriptor.rs index 825b581135094..a770485b06859 100644 --- a/crates/bevy_ecs/src/schedule/system_descriptor.rs +++ b/crates/bevy_ecs/src/schedule/system_descriptor.rs @@ -105,9 +105,9 @@ pub struct ParallelSystemDescriptor { fn new_parallel_descriptor(system: BoxedSystem<(), ()>) -> ParallelSystemDescriptor { ParallelSystemDescriptor { + labels: system.default_labels(), system, run_criteria: None, - labels: Vec::new(), before: Vec::new(), after: Vec::new(), ambiguity_sets: Vec::new(), diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index d66689d956d6b..373804f47df65 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -2,6 +2,7 @@ use crate::{ archetype::{Archetype, ArchetypeComponentId, ArchetypeGeneration, ArchetypeId}, component::ComponentId, query::{Access, FilteredAccessSet}, + schedule::SystemLabel, system::{ check_system_change_tick, ReadOnlySystemParamFetch, System, SystemParam, SystemParamFetch, SystemParamState, @@ -9,7 +10,11 @@ use crate::{ world::{World, WorldId}, }; use bevy_ecs_macros::all_tuples; -use std::{borrow::Cow, marker::PhantomData}; +use std::{ + any::{Any, TypeId}, + borrow::Cow, + marker::PhantomData, +}; /// The metadata of a [`System`]. pub struct SystemMeta { @@ -421,6 +426,18 @@ where self.system_meta.name.as_ref(), ); } + fn default_labels(&self) -> Vec> { + vec![Box::new(self.func.system_label())] + } +} + +#[derive(Debug, Hash, PartialEq, Eq, Copy, Clone)] +pub struct SystemTypeIdLabel(pub(crate) TypeId); + +impl SystemLabel for SystemTypeIdLabel { + fn dyn_clone(&self) -> Box { + Box::new(*self) + } } /// A trait implemented for all functions that can be used as [`System`]s. @@ -437,6 +454,11 @@ pub trait SystemParamFunction: Send + Sync world: &World, change_tick: u32, ) -> Out; + + #[inline] + fn system_label(&self) -> SystemTypeIdLabel { + SystemTypeIdLabel(self.type_id()) + } } macro_rules! impl_system_function { diff --git a/crates/bevy_ecs/src/system/system.rs b/crates/bevy_ecs/src/system/system.rs index 28c84ecb805b3..3cf0f9e7733fe 100644 --- a/crates/bevy_ecs/src/system/system.rs +++ b/crates/bevy_ecs/src/system/system.rs @@ -4,6 +4,7 @@ use crate::{ archetype::{Archetype, ArchetypeComponentId}, component::ComponentId, query::Access, + schedule::SystemLabel, world::World, }; use std::borrow::Cow; @@ -56,6 +57,10 @@ pub trait System: Send + Sync + 'static { /// Initialize the system. fn initialize(&mut self, _world: &mut World); fn check_change_tick(&mut self, change_tick: u32); + /// The default labels for the system + fn default_labels(&self) -> Vec> { + Vec::new() + } } /// A convenience type alias for a boxed [`System`] trait object. diff --git a/examples/ecs/system_sets.rs b/examples/ecs/system_sets.rs index 26c2c025e1350..9c872a13425b5 100644 --- a/examples/ecs/system_sets.rs +++ b/examples/ecs/system_sets.rs @@ -16,14 +16,6 @@ struct PostPhysics; #[derive(Default)] struct Done(bool); -/// This is used to show that within a [`SystemSet`], individual systems can also -/// be labelled, allowing further fine tuning of run ordering. -#[derive(Clone, Hash, Debug, PartialEq, Eq, SystemLabel)] -pub enum PhysicsSystem { - UpdateVelocity, - Movement, -} - /// This example realizes the following scheme: /// /// ```none @@ -63,18 +55,9 @@ fn main() { // This criteria ensures this whole system set only runs when this system's // output says so (ShouldRun::Yes) .with_run_criteria(run_for_a_second) - .with_system( - update_velocity - // Only applied to the `update_velocity` system - .label(PhysicsSystem::UpdateVelocity), - ) - .with_system( - movement - // Only applied to the `movement` system - .label(PhysicsSystem::Movement) - // Enforce order within this system by specifying this - .after(PhysicsSystem::UpdateVelocity), - ), + .with_system(update_velocity) + // Make movement run after update_velocity + .with_system(movement.after(update_velocity.system_label())), ) .add_system_set( SystemSet::new() From 649987b1e2424d718c075c8c9bec83d9738e4667 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 15 Mar 2022 18:06:46 -0700 Subject: [PATCH 2/6] a.after(b) --- .../src/schedule/system_descriptor.rs | 25 +++++++++++-------- crates/bevy_ecs/src/system/function_system.rs | 25 ++++++++++++++----- examples/ecs/system_sets.rs | 2 +- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/system_descriptor.rs b/crates/bevy_ecs/src/schedule/system_descriptor.rs index a770485b06859..42e932f63981a 100644 --- a/crates/bevy_ecs/src/schedule/system_descriptor.rs +++ b/crates/bevy_ecs/src/schedule/system_descriptor.rs @@ -3,7 +3,10 @@ use crate::{ AmbiguitySetLabel, BoxedAmbiguitySetLabel, BoxedSystemLabel, IntoRunCriteria, RunCriteriaDescriptorOrLabel, SystemLabel, }, - system::{BoxedSystem, ExclusiveSystem, ExclusiveSystemCoerced, ExclusiveSystemFn, IntoSystem}, + system::{ + AsSystemLabel, BoxedSystem, ExclusiveSystem, ExclusiveSystemCoerced, ExclusiveSystemFn, + IntoSystem, + }, }; /// Encapsulates a system and information on when it run in a `SystemStage`. @@ -126,10 +129,10 @@ pub trait ParallelSystemDescriptorCoercion { fn label(self, label: impl SystemLabel) -> ParallelSystemDescriptor; /// Specifies that the system should run before systems with the given label. - fn before(self, label: impl SystemLabel) -> ParallelSystemDescriptor; + fn before(self, label: impl AsSystemLabel) -> ParallelSystemDescriptor; /// Specifies that the system should run after systems with the given label. - fn after(self, label: impl SystemLabel) -> ParallelSystemDescriptor; + fn after(self, label: impl AsSystemLabel) -> ParallelSystemDescriptor; /// Specifies that the system is exempt from execution order ambiguity detection /// with other systems in this set. @@ -150,13 +153,13 @@ impl ParallelSystemDescriptorCoercion<()> for ParallelSystemDescriptor { self } - fn before(mut self, label: impl SystemLabel) -> ParallelSystemDescriptor { - self.before.push(Box::new(label)); + fn before(mut self, label: impl AsSystemLabel) -> ParallelSystemDescriptor { + self.before.push(label.as_system_label()); self } - fn after(mut self, label: impl SystemLabel) -> ParallelSystemDescriptor { - self.after.push(Box::new(label)); + fn after(mut self, label: impl AsSystemLabel) -> ParallelSystemDescriptor { + self.after.push(label.as_system_label()); self } @@ -182,11 +185,11 @@ where new_parallel_descriptor(Box::new(IntoSystem::into_system(self))).label(label) } - fn before(self, label: impl SystemLabel) -> ParallelSystemDescriptor { + fn before(self, label: impl AsSystemLabel) -> ParallelSystemDescriptor { new_parallel_descriptor(Box::new(IntoSystem::into_system(self))).before(label) } - fn after(self, label: impl SystemLabel) -> ParallelSystemDescriptor { + fn after(self, label: impl AsSystemLabel) -> ParallelSystemDescriptor { new_parallel_descriptor(Box::new(IntoSystem::into_system(self))).after(label) } @@ -207,11 +210,11 @@ impl ParallelSystemDescriptorCoercion<()> for BoxedSystem<(), ()> { new_parallel_descriptor(self).label(label) } - fn before(self, label: impl SystemLabel) -> ParallelSystemDescriptor { + fn before(self, label: impl AsSystemLabel) -> ParallelSystemDescriptor { new_parallel_descriptor(self).before(label) } - fn after(self, label: impl SystemLabel) -> ParallelSystemDescriptor { + fn after(self, label: impl AsSystemLabel) -> ParallelSystemDescriptor { new_parallel_descriptor(self).after(label) } diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 373804f47df65..98be65be4d0fb 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -427,7 +427,7 @@ where ); } fn default_labels(&self) -> Vec> { - vec![Box::new(self.func.system_label())] + vec![self.func.as_system_label()] } } @@ -454,11 +454,6 @@ pub trait SystemParamFunction: Send + Sync world: &World, change_tick: u32, ) -> Out; - - #[inline] - fn system_label(&self) -> SystemTypeIdLabel { - SystemTypeIdLabel(self.type_id()) - } } macro_rules! impl_system_function { @@ -511,3 +506,21 @@ macro_rules! impl_system_function { } all_tuples!(impl_system_function, 0, 16, F); + +pub trait AsSystemLabel { + fn as_system_label(&self) -> Box; +} + +impl> + AsSystemLabel<(In, Out, Param, Marker)> for T +{ + fn as_system_label(&self) -> Box { + Box::new(SystemTypeIdLabel(self.type_id())) + } +} + +impl AsSystemLabel<()> for T { + fn as_system_label(&self) -> Box { + self.dyn_clone() + } +} diff --git a/examples/ecs/system_sets.rs b/examples/ecs/system_sets.rs index 9c872a13425b5..fffba8b1b1910 100644 --- a/examples/ecs/system_sets.rs +++ b/examples/ecs/system_sets.rs @@ -57,7 +57,7 @@ fn main() { .with_run_criteria(run_for_a_second) .with_system(update_velocity) // Make movement run after update_velocity - .with_system(movement.after(update_velocity.system_label())), + .with_system(movement.after(update_velocity)), ) .add_system_set( SystemSet::new() From 76ca7f60a34de89e363c4d32024a238e16948602 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 15 Mar 2022 18:19:45 -0700 Subject: [PATCH 3/6] fix ambiguity test --- crates/bevy_ecs/src/schedule/stage.rs | 46 ++++++++++++++++----------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/stage.rs b/crates/bevy_ecs/src/schedule/stage.rs index 0ec7312ff19f7..4503400b11b53 100644 --- a/crates/bevy_ecs/src/schedule/stage.rs +++ b/crates/bevy_ecs/src/schedule/stage.rs @@ -1583,15 +1583,25 @@ mod tests { fn ambiguity_detection() { use super::{find_ambiguities, SystemContainer}; - fn find_ambiguities_first_labels( + fn find_ambiguities_first_str_labels( systems: &[impl SystemContainer], ) -> Vec<(BoxedSystemLabel, BoxedSystemLabel)> { find_ambiguities(systems) .drain(..) .map(|(index_a, index_b, _conflicts)| { ( - systems[index_a].labels()[0].clone(), - systems[index_b].labels()[0].clone(), + systems[index_a] + .labels() + .iter() + .find(|a| (&***a).type_id() == std::any::TypeId::of::<&str>()) + .unwrap() + .clone(), + systems[index_b] + .labels() + .iter() + .find(|a| (&***a).type_id() == std::any::TypeId::of::<&str>()) + .unwrap() + .clone(), ) }) .collect() @@ -1621,7 +1631,7 @@ mod tests { .with_system(component.label("4")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("1"), Box::new("4"))) || ambiguities.contains(&(Box::new("4"), Box::new("1"))) @@ -1636,7 +1646,7 @@ mod tests { .with_system(resource.label("4")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("1"), Box::new("4"))) || ambiguities.contains(&(Box::new("4"), Box::new("1"))) @@ -1661,7 +1671,7 @@ mod tests { .with_system(resource.label("4")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("0"), Box::new("3"))) || ambiguities.contains(&(Box::new("3"), Box::new("0"))) @@ -1680,7 +1690,7 @@ mod tests { .with_system(resource.label("4").in_ambiguity_set("a")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("0"), Box::new("3"))) || ambiguities.contains(&(Box::new("3"), Box::new("0"))) @@ -1693,7 +1703,7 @@ mod tests { .with_system(component.label("2")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("0"), Box::new("1"))) || ambiguities.contains(&(Box::new("1"), Box::new("0"))) @@ -1706,7 +1716,7 @@ mod tests { .with_system(component.label("2").after("0")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("1"), Box::new("2"))) || ambiguities.contains(&(Box::new("2"), Box::new("1"))) @@ -1720,7 +1730,7 @@ mod tests { .with_system(component.label("3").after("1").after("2")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("1"), Box::new("2"))) || ambiguities.contains(&(Box::new("2"), Box::new("1"))) @@ -1734,7 +1744,7 @@ mod tests { .with_system(component.label("3").after("1").after("2")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert_eq!(ambiguities.len(), 0); let mut stage = SystemStage::parallel() @@ -1744,7 +1754,7 @@ mod tests { .with_system(component.label("3").after("1").after("2")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("1"), Box::new("2"))) || ambiguities.contains(&(Box::new("2"), Box::new("1"))) @@ -1774,7 +1784,7 @@ mod tests { ); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("1"), Box::new("2"))) || ambiguities.contains(&(Box::new("2"), Box::new("1"))) @@ -1824,7 +1834,7 @@ mod tests { ); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert_eq!(ambiguities.len(), 0); let mut stage = SystemStage::parallel() @@ -1855,7 +1865,7 @@ mod tests { ); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.parallel); + let ambiguities = find_ambiguities_first_str_labels(&stage.parallel); assert!( ambiguities.contains(&(Box::new("1"), Box::new("4"))) || ambiguities.contains(&(Box::new("4"), Box::new("1"))) @@ -1889,7 +1899,7 @@ mod tests { .with_system(empty.exclusive_system().label("6").after("2").after("5")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.exclusive_at_start); + let ambiguities = find_ambiguities_first_str_labels(&stage.exclusive_at_start); assert!( ambiguities.contains(&(Box::new("1"), Box::new("3"))) || ambiguities.contains(&(Box::new("3"), Box::new("1"))) @@ -1926,7 +1936,7 @@ mod tests { .with_system(empty.exclusive_system().label("6").after("2").after("5")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.exclusive_at_start); + let ambiguities = find_ambiguities_first_str_labels(&stage.exclusive_at_start); assert!( ambiguities.contains(&(Box::new("2"), Box::new("3"))) || ambiguities.contains(&(Box::new("3"), Box::new("2"))) @@ -1952,7 +1962,7 @@ mod tests { .with_system(empty.exclusive_system().label("3").in_ambiguity_set("a")); stage.initialize_systems(&mut world); stage.rebuild_orders_and_dependencies(); - let ambiguities = find_ambiguities_first_labels(&stage.exclusive_at_start); + let ambiguities = find_ambiguities_first_str_labels(&stage.exclusive_at_start); assert_eq!(ambiguities.len(), 0); } From a964e8efeb41e1f835d1266a81755f5931db3af4 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 15 Mar 2022 23:50:32 -0700 Subject: [PATCH 4/6] Update crates/bevy_ecs/src/system/function_system.rs Co-authored-by: Alice Cecile --- crates/bevy_ecs/src/system/function_system.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 98be65be4d0fb..f53a427a0e23a 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -432,6 +432,7 @@ where } #[derive(Debug, Hash, PartialEq, Eq, Copy, Clone)] +/// A [`SystemLabel`] that was automatically generated for a system on the basis of its `TypeId`. pub struct SystemTypeIdLabel(pub(crate) TypeId); impl SystemLabel for SystemTypeIdLabel { From ca7c674affe01d53f8a6dc3fad6e8207a020b768 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 22 Mar 2022 18:00:02 -0700 Subject: [PATCH 5/6] Make SystemTypeIdLabel ZST, remove boxing from AsSystemLabel --- .../src/schedule/system_descriptor.rs | 4 +- crates/bevy_ecs/src/system/function_system.rs | 61 ++++++++++++++----- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/system_descriptor.rs b/crates/bevy_ecs/src/schedule/system_descriptor.rs index 42e932f63981a..405c8760f3a81 100644 --- a/crates/bevy_ecs/src/schedule/system_descriptor.rs +++ b/crates/bevy_ecs/src/schedule/system_descriptor.rs @@ -154,12 +154,12 @@ impl ParallelSystemDescriptorCoercion<()> for ParallelSystemDescriptor { } fn before(mut self, label: impl AsSystemLabel) -> ParallelSystemDescriptor { - self.before.push(label.as_system_label()); + self.before.push(Box::new(label.as_system_label())); self } fn after(mut self, label: impl AsSystemLabel) -> ParallelSystemDescriptor { - self.after.push(label.as_system_label()); + self.after.push(Box::new(label.as_system_label())); self } diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index f53a427a0e23a..1693724bb69d6 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -10,11 +10,7 @@ use crate::{ world::{World, WorldId}, }; use bevy_ecs_macros::all_tuples; -use std::{ - any::{Any, TypeId}, - borrow::Cow, - marker::PhantomData, -}; +use std::{any::TypeId, borrow::Cow, fmt::Debug, hash::Hash, marker::PhantomData}; /// The metadata of a [`System`]. pub struct SystemMeta { @@ -427,15 +423,43 @@ where ); } fn default_labels(&self) -> Vec> { - vec![self.func.as_system_label()] + vec![Box::new(self.func.as_system_label())] } } -#[derive(Debug, Hash, PartialEq, Eq, Copy, Clone)] /// A [`SystemLabel`] that was automatically generated for a system on the basis of its `TypeId`. -pub struct SystemTypeIdLabel(pub(crate) TypeId); +pub struct SystemTypeIdLabel(PhantomData T>); + +impl Debug for SystemTypeIdLabel { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("SystemTypeIdLabel") + .field(&std::any::type_name::()) + .finish() + } +} +impl Hash for SystemTypeIdLabel { + fn hash(&self, _state: &mut H) { + // All SystemTypeIds of a given type are the same. + } +} +impl Clone for SystemTypeIdLabel { + fn clone(&self) -> Self { + Self(PhantomData) + } +} -impl SystemLabel for SystemTypeIdLabel { +impl Copy for SystemTypeIdLabel {} + +impl PartialEq for SystemTypeIdLabel { + #[inline] + fn eq(&self, _other: &Self) -> bool { + // All labels of a given type are equal, as they will all have the same type id + true + } +} +impl Eq for SystemTypeIdLabel {} + +impl SystemLabel for SystemTypeIdLabel { fn dyn_clone(&self) -> Box { Box::new(*self) } @@ -508,20 +532,27 @@ macro_rules! impl_system_function { all_tuples!(impl_system_function, 0, 16, F); +/// Used to implicitly convert systems to their default labels. For example, it will convert +/// "system functions" to their [`SystemTypeIdLabel`]. pub trait AsSystemLabel { - fn as_system_label(&self) -> Box; + type SystemLabel: SystemLabel; + fn as_system_label(&self) -> Self::SystemLabel; } impl> AsSystemLabel<(In, Out, Param, Marker)> for T { - fn as_system_label(&self) -> Box { - Box::new(SystemTypeIdLabel(self.type_id())) + type SystemLabel = SystemTypeIdLabel; + + fn as_system_label(&self) -> Self::SystemLabel { + SystemTypeIdLabel(PhantomData:: Self>) } } -impl AsSystemLabel<()> for T { - fn as_system_label(&self) -> Box { - self.dyn_clone() +impl AsSystemLabel<()> for T { + type SystemLabel = T; + + fn as_system_label(&self) -> Self::SystemLabel { + self.clone() } } From c39276e3c110c54524f6b0531f5307a93cf5b896 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 22 Mar 2022 18:26:55 -0700 Subject: [PATCH 6/6] clippy --- crates/bevy_ecs/src/system/function_system.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 1693724bb69d6..ba49fd5e68cda 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -10,7 +10,7 @@ use crate::{ world::{World, WorldId}, }; use bevy_ecs_macros::all_tuples; -use std::{any::TypeId, borrow::Cow, fmt::Debug, hash::Hash, marker::PhantomData}; +use std::{borrow::Cow, fmt::Debug, hash::Hash, marker::PhantomData}; /// The metadata of a [`System`]. pub struct SystemMeta {