mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
Use ptr::from_ref
and ptr::addr_eq
in macro (#13081)
# Objective
- Clippy raises a few warnings on the latest nightly release. 📎
## Solution
- Use `ptr::from_ref` when possible, because it prevents you from
accidentally changing the mutability as well as its type.
- Use `ptr::addr_eq` when comparing two pointers, ignoring pointer
metadata.
This commit is contained in:
parent
d59c859a35
commit
f1d1491126
1 changed files with 12 additions and 7 deletions
|
@ -174,17 +174,22 @@ macro_rules! define_label {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ref_eq(&self, other: &Self) -> bool {
|
fn ref_eq(&self, other: &Self) -> bool {
|
||||||
if self.as_dyn_eq().type_id() == other.as_dyn_eq().type_id() {
|
use ::std::ptr;
|
||||||
(self as *const Self).cast::<()>() == (other as *const Self).cast::<()>()
|
|
||||||
} else {
|
// Test that both the type id and pointer address are equivalent.
|
||||||
false
|
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) {
|
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_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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue