Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ptr::from_ref and ptr::addr_eq in macro #13081

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions crates/bevy_ecs/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,22 @@ macro_rules! define_label {
}

fn ref_eq(&self, other: &Self) -> bool {
if self.as_dyn_eq().type_id() == other.as_dyn_eq().type_id() {
(self as *const Self).cast::<()>() == (other as *const Self).cast::<()>()
} else {
false
}
use ::std::ptr;

// Test that both the type id and pointer address are equivalent.
self.as_dyn_eq().type_id() == other.as_dyn_eq().type_id()
&& ptr::addr_eq(ptr::from_ref::<Self>(self), ptr::from_ref::<Self>(other))
}

fn ref_hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
use ::std::hash::Hash;
use ::std::{hash::Hash, ptr};

// Hash the type id...
self.as_dyn_eq().type_id().hash(state);
(self as *const Self).cast::<()>().hash(state);

// ...and the pointer address.
// Cast to a unit `()` first to discard any pointer metadata.
ptr::from_ref::<Self>(self).cast::<()>().hash(state);
}
}

Expand Down