Skip to content

Commit

Permalink
use fn pointer for comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
JoJoJet committed Aug 4, 2022
1 parent 3e4b487 commit b5d7611
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
8 changes: 4 additions & 4 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ impl App {
stage_label: impl StageLabel,
system: impl IntoSystemDescriptor<Params>,
) -> &mut Self {
use std::any::TypeId;
let stage_label = stage_label.as_label();
assert!(
stage_label.type_id() != TypeId::of::<StartupStage>(),
!stage_label.is::<StartupStage>(),
"add systems to a startup stage using App::add_startup_system_to_stage"
);
self.schedule.add_system_to_stage(stage_label, system);
Expand Down Expand Up @@ -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::<StartupStage>(),
!stage_label.is::<StartupStage>(),
"add system sets to a startup stage using App::add_startup_system_set_to_stage"
);
self.schedule
Expand Down
27 changes: 13 additions & 14 deletions crates/bevy_utils/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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::<Self>()
$id_name { data, f: Self::fmt }
}
/// Returns a number used to distinguish different labels of the same type.
fn data(&self) -> u64;
Expand All @@ -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
}
Expand All @@ -130,17 +119,27 @@ 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 {}


impl std::hash::Hash for $id_name {
fn hash<H: std::hash::Hasher>(&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<L: $label_name>(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) == (<L as $label_name>::fmt as usize)
}
}
};
}

0 comments on commit b5d7611

Please sign in to comment.