Skip to content

Commit

Permalink
undo an optimization 😭
Browse files Browse the repository at this point in the history
MIRI is buggy so we can't use fn pointers for comparisons
  • Loading branch information
JoJoJet committed Aug 9, 2022
1 parent 8bce742 commit 239f8dc
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions crates/bevy_utils/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ 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 @@ -98,7 +99,7 @@ macro_rules! define_label {
#[inline]
fn as_label(&self) -> $id_name {
let data = self.data();
$id_name { data, f: Self::fmt }
$id_name { data, ty: ::std::any::TypeId::of::<Self>(), f: Self::fmt }
}
/// Returns a number used to distinguish different labels of the same type.
fn data(&self) -> u64;
Expand Down Expand Up @@ -128,26 +129,23 @@ macro_rules! define_label {
impl PartialEq for $id_name {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
(self.f as usize) == (rhs.f as usize) && self.data() == rhs.data()
self.ty == rhs.ty && 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.f as usize).hash(state);
self.ty.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)
self.ty == ::std::any::TypeId::of::<L>()
}
/// Attempts to downcast this label to type `L`.
///
Expand Down

0 comments on commit 239f8dc

Please sign in to comment.