From 5da8af7d379afdd85f0bf282bc04c53639c5f38e Mon Sep 17 00:00:00 2001 From: konsti219 <37149441+konsti219@users.noreply.github.com> Date: Thu, 4 May 2023 14:22:25 +0200 Subject: [PATCH] Manually implement common traits for `EventId` (#8529) # Objective Fixes #8528 ## Solution Manually implement `PartialEq`, `Eq`, `PartialOrd`, `Ord`, and `Hash` for `bevy_ecs::event::EventId`. These new implementations do not rely on the `Event` implementing the same traits allowing `EventId` to be used in more cases. --- crates/bevy_ecs/src/event.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/event.rs b/crates/bevy_ecs/src/event.rs index f0e5f26f74..998c7174c5 100644 --- a/crates/bevy_ecs/src/event.rs +++ b/crates/bevy_ecs/src/event.rs @@ -4,7 +4,14 @@ use crate as bevy_ecs; use crate::system::{Local, Res, ResMut, Resource, SystemParam}; use bevy_utils::detailed_trace; use std::ops::{Deref, DerefMut}; -use std::{fmt, hash::Hash, iter::Chain, marker::PhantomData, slice::Iter}; +use std::{ + cmp::Ordering, + fmt, + hash::{Hash, Hasher}, + iter::Chain, + marker::PhantomData, + slice::Iter, +}; /// A type that can be stored in an [`Events`] resource /// You can conveniently access events using the [`EventReader`] and [`EventWriter`] system parameter. /// @@ -16,7 +23,6 @@ impl Event for T where T: Send + Sync + 'static {} /// /// An `EventId` can among other things be used to trace the flow of an event from the point it was /// sent to the point it was processed. -#[derive(Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct EventId { pub id: usize, _marker: PhantomData, @@ -46,6 +52,32 @@ impl fmt::Debug for EventId { } } +impl PartialEq for EventId { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + +impl Eq for EventId {} + +impl PartialOrd for EventId { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for EventId { + fn cmp(&self, other: &Self) -> Ordering { + self.id.cmp(&other.id) + } +} + +impl Hash for EventId { + fn hash(&self, state: &mut H) { + Hash::hash(&self.id, state); + } +} + #[derive(Debug)] struct EventInstance { pub event_id: EventId,