Skip to content

Commit b5d7611

Browse files
committed
use fn pointer for comparisons
1 parent 3e4b487 commit b5d7611

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

crates/bevy_app/src/app.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,9 @@ impl App {
373373
stage_label: impl StageLabel,
374374
system: impl IntoSystemDescriptor<Params>,
375375
) -> &mut Self {
376-
use std::any::TypeId;
376+
let stage_label = stage_label.as_label();
377377
assert!(
378-
stage_label.type_id() != TypeId::of::<StartupStage>(),
378+
!stage_label.is::<StartupStage>(),
379379
"add systems to a startup stage using App::add_startup_system_to_stage"
380380
);
381381
self.schedule.add_system_to_stage(stage_label, system);
@@ -408,9 +408,9 @@ impl App {
408408
stage_label: impl StageLabel,
409409
system_set: SystemSet,
410410
) -> &mut Self {
411-
use std::any::TypeId;
411+
let stage_label = stage_label.as_label();
412412
assert!(
413-
stage_label.type_id() != TypeId::of::<StartupStage>(),
413+
!stage_label.is::<StartupStage>(),
414414
"add system sets to a startup stage using App::add_startup_system_set_to_stage"
415415
);
416416
self.schedule

crates/bevy_utils/src/label.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ macro_rules! define_label {
7272
$(#[$id_attr])*
7373
#[derive(Clone, Copy)]
7474
pub struct $id_name {
75-
ty: ::std::any::TypeId,
7675
data: u64,
7776
f: fn(u64, &mut ::std::fmt::Formatter) -> ::std::fmt::Result,
7877
}
@@ -89,14 +88,8 @@ macro_rules! define_label {
8988
/// Converts this type into an opaque, strongly-typed label.
9089
#[inline]
9190
fn as_label(&self) -> $id_name {
92-
let ty = self.type_id();
9391
let data = self.data();
94-
$id_name { ty, data, f: Self::fmt }
95-
}
96-
/// Returns the [`TypeId`] used to differentiate labels.
97-
#[inline]
98-
fn type_id(&self) -> ::std::any::TypeId {
99-
::std::any::TypeId::of::<Self>()
92+
$id_name { data, f: Self::fmt }
10093
}
10194
/// Returns a number used to distinguish different labels of the same type.
10295
fn data(&self) -> u64;
@@ -114,10 +107,6 @@ macro_rules! define_label {
114107
*self
115108
}
116109
#[inline]
117-
fn type_id(&self) -> ::std::any::TypeId {
118-
self.ty
119-
}
120-
#[inline]
121110
fn data(&self) -> u64 {
122111
self.data
123112
}
@@ -130,17 +119,27 @@ macro_rules! define_label {
130119
impl PartialEq for $id_name {
131120
#[inline]
132121
fn eq(&self, rhs: &Self) -> bool {
133-
self.type_id() == rhs.type_id() && self.data() == rhs.data()
122+
(self.f as usize) == (rhs.f as usize) && self.data() == rhs.data()
134123
}
135124
}
136125
impl Eq for $id_name {}
137126

138127

139128
impl std::hash::Hash for $id_name {
140129
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
141-
self.type_id().hash(state);
130+
(self.f as usize).hash(state);
142131
self.data().hash(state);
143132
}
144133
}
134+
135+
impl $id_name {
136+
/// Returns true if this label was constructed from an instance of type `L`.
137+
pub fn is<L: $label_name>(self) -> bool {
138+
// FIXME: This is potentially incorrect, due to the
139+
// compiler unifying identical functions. We'll likely
140+
// have to store some kind of hash of the TypeId.
141+
(self.f as usize) == (<L as $label_name>::fmt as usize)
142+
}
143+
}
145144
};
146145
}

0 commit comments

Comments
 (0)