Skip to content

Commit

Permalink
Manually implement common traits for EventId
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
konsti219 committed May 2, 2023
1 parent a29d328 commit c84091d
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<E>`] resource
/// You can conveniently access events using the [`EventReader`] and [`EventWriter`] system parameter.
///
Expand All @@ -16,7 +23,6 @@ impl<T> 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<E: Event> {
pub id: usize,
_marker: PhantomData<E>,
Expand Down Expand Up @@ -46,6 +52,32 @@ impl<E: Event> fmt::Debug for EventId<E> {
}
}

impl<E: Event> PartialEq for EventId<E> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl<E: Event> Eq for EventId<E> {}

impl<E: Event> PartialOrd for EventId<E> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.id.cmp(&other.id))
}
}

impl<E: Event> Ord for EventId<E> {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}

impl<E: Event> Hash for EventId<E> {
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(&self.id, state);
}
}

#[derive(Debug)]
struct EventInstance<E: Event> {
pub event_id: EventId<E>,
Expand Down

0 comments on commit c84091d

Please sign in to comment.