diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 2bbcb75b666547..28b69c425402fd 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -373,9 +373,9 @@ impl App { stage_label: impl StageLabel, system: impl IntoSystemDescriptor, ) -> &mut Self { - use std::any::TypeId; + let stage_label = stage_label.as_label(); assert!( - stage_label.type_id() != TypeId::of::(), + !stage_label.is::(), "add systems to a startup stage using App::add_startup_system_to_stage" ); self.schedule.add_system_to_stage(stage_label, system); @@ -408,9 +408,9 @@ impl App { stage_label: impl StageLabel, system_set: SystemSet, ) -> &mut Self { - use std::any::TypeId; + let stage_label = stage_label.as_label(); assert!( - stage_label.type_id() != TypeId::of::(), + !stage_label.is::(), "add system sets to a startup stage using App::add_startup_system_set_to_stage" ); self.schedule diff --git a/crates/bevy_utils/src/label.rs b/crates/bevy_utils/src/label.rs index ca48e90b15b9d3..3b97814a5a3847 100644 --- a/crates/bevy_utils/src/label.rs +++ b/crates/bevy_utils/src/label.rs @@ -72,7 +72,6 @@ macro_rules! define_label { $(#[$id_attr])* #[derive(Clone, Copy)] pub struct $id_name { - ty: ::std::any::TypeId, data: u64, f: fn(u64, &mut ::std::fmt::Formatter) -> ::std::fmt::Result, } @@ -89,14 +88,8 @@ macro_rules! define_label { /// Converts this type into an opaque, strongly-typed label. #[inline] fn as_label(&self) -> $id_name { - let ty = self.type_id(); let data = self.data(); - $id_name { ty, data, f: Self::fmt } - } - /// Returns the [`TypeId`] used to differentiate labels. - #[inline] - fn type_id(&self) -> ::std::any::TypeId { - ::std::any::TypeId::of::() + $id_name { data, f: Self::fmt } } /// Returns a number used to distinguish different labels of the same type. fn data(&self) -> u64; @@ -114,10 +107,6 @@ macro_rules! define_label { *self } #[inline] - fn type_id(&self) -> ::std::any::TypeId { - self.ty - } - #[inline] fn data(&self) -> u64 { self.data } @@ -130,7 +119,7 @@ macro_rules! define_label { impl PartialEq for $id_name { #[inline] fn eq(&self, rhs: &Self) -> bool { - self.type_id() == rhs.type_id() && self.data() == rhs.data() + (self.f as usize) == (rhs.f as usize) && self.data() == rhs.data() } } impl Eq for $id_name {} @@ -138,9 +127,19 @@ macro_rules! define_label { impl std::hash::Hash for $id_name { fn hash(&self, state: &mut H) { - self.type_id().hash(state); + (self.f as usize).hash(state); self.data().hash(state); } } + + impl $id_name { + /// Returns true if this label was constructed from an instance of type `L`. + pub fn is(self) -> bool { + // FIXME: This is potentially incorrect, due to the + // compiler unifying identical functions. We'll likely + // have to store some kind of hash of the TypeId. + (self.f as usize) == (::fmt as usize) + } + } }; }